我想创建一个包含通用字段,它是基于另一个字段中键入指定的一个结构。这种类型是我已经定义了几种结构之一。
从本质上讲什么,我想在这里做的是创建一个模块这需要设备配置的照顾。这是为了处理很多不同的设备类型。某些规范将需要配置为所有设备我创建了,在OO模式将是一个父类(这里称为DeviceCfg)一个结构。然后,设备类型特定的规格将代表子类。 (这些被表示为RaspberryPiCfg,Esp8266Cfg,ECT)。
我试图结构的东西,使得我可以引用其合并两者的通用规格(适用于所有的设备)的单个对象,以及所述设备类型特定的规格。
use std::net::{Ipv4Addr};
use std::any::Any;
//this struct is made to show the device-type-specific configurable items
struct RaspberryPiCfg {
led_pin : u8,
//GPIO pin connected to the LED strip pixels (must support PWM)
led_freq_hz : u32,
//LED signal frequency in Hz (usually 800kHz)
led_dma : u8,
//DMA channel used for generating PWM signal (try 5)
brightness : u8,
//Brightness of LED strip between 0 and 255"
led_invert : bool,
//Set True if using an inverting logic level converter
software_gamma_correction : bool
//Set to True because Raspberry Pi doesn't use hardware dithering
}
//this creates a default function which sets all configurable items
impl Default for RaspberryPiCfg {
fn default() -> RaspberryPiCfg {
RaspberryPiCfg {
led_pin : 18,
led_freq_hz : 800_000,
led_dma : 5,
brightness : 255,
led_invert : true,
software_gamma_correction : true
}
}
}
//this struct is made to show the device-type-specific configurable items
struct Esp8266Cfg {
udp_ip : Ipv4Addr,
//IP address of the ESP8266. Must match IP in ws2812_controller.ino
udp_port : u16,
//Port number used for socket communication between Python and ESP8266"
software_gamma_correction : bool
//Set to True because Raspberry Pi doesn't use hardware dithering
}
//this creates a default function which sets all configurable items
impl Default for Esp8266Cfg {
fn default() -> Esp8266Cfg {
Esp8266Cfg {
udp_ip : Ipv4use std::net::{Ipv4Addr};
udp_port : 7777,
software_gamma_correction : false
}
}
}
//this struct is made to show the device-type-specific configurable items
struct BlinkstickCfg {
software_gamma_correction : bool
//Set to True because BlinkstickCfg doesn't use hardware dithering
}
//this creates a default function which sets all configurable items
impl Default for BlinkstickCfg {
fn default() -> BlinkstickCfg {
BlinkstickCfg {
software_gamma_correction : true
}
}
}
enum DeviceType {
ESP8266,
RASPBERRY_PI,
BLINKSTICK
}
enum StatusType {
ERROR,
OK
}
//this struct is made to show the configurable items that are relevant
//for all devices
struct Devicecfg {
use_gui : bool,
//Whether or not to display a PyQtGraph GUI plot of visualization
display_fps : bool,
//Whether to display the FPS when running (can reduce performance)
pixel_num : u8,
//Number of pixels in the LED strip (must match ESP8266 firmware)
gamma_table_path : String,
//Location of the gamma correction table"
mic_rate : u32,
//Sampling frequency of the microphone in Hz
fps : u8,
//Desired refresh rate of the visualization (frames per second)
min_led_fps : u32,
//Frequencies below this value will be removed during audio processing
max_led_fps : u32,
//Frequencies above this value will be removed during audio processing
device_type : DeviceType//,
//device_cfg : T
}
//I am trying to create a trait to implement which I could use to create
//an object which when referenced represents the configuration of
//both a devices device-type-specific specs and the specs that are not
//specific to the type of device being configured.
trait DeviceSpec {
type specs;
fn setSpecs(&self);
}
impl DeviceSpec for Devicecfg {
fn setSpecs(&self) {
match self.device_type {
DeviceType::ESP8266 => {
self.specs = Esp8266Cfg::default();
}
DeviceType::RASPBERRY_PI => {
self.specs = Ra\spberryPiCfg::default();
}
DeviceType::BLINKSTICK => {
self.specs = BlinkstickCfg::default();
}
}
}
}
impl Default for Devicecfg {
fn default() -> Devicecfg {
Devicecfg {
use_gui : true,
display_fps : true,
pixel_num : 65,
gamma_table_path : "directory".to_string(),
mic_rate : 44_100,
fps : 60,
min_led_fps : 200,
max_led_fps : 12_000,
device_type : DeviceType::ESP8266,
device_cfg : self.setSpecificCfg();
}
}
}
感谢所有帮助。我仍然在锈非常学习者。
某些规范将需要配置为所有设备我创建了,在OO模式将是一个父类结构
锈是不是一个面向对象的语言,所以它是坏主意,申请OO范式。没有理由使用特征了。防锈特点是确定共同行为,而不是常见的数据。
我相信你应该使用成分,而不是继承的。
enum DeviceSpecificCfg {
RaspberryPi(RaspberryPiCfg),
Esp8266(Esp8266Cfg),
// ...
}
struct Devicecfg {
// common fields ...
device_specific_cfg: DeviceSpecificCfg
}