Struct 如何将traits泛型类型链接到实现类型中的其他元素
我正在尝试创建一个包含泛型字段的结构,该字段是基于另一个字段指定的类型。这个类型是我定义的几个结构之一 基本上,我在这里试图做的是创建一个模块,负责设备配置。这意味着要处理一系列不同的设备类型。需要为所有设备配置某些规范,并且我已经创建了一个结构,在OO范例中,它将是一个父类(这里表示为DeviceCfg)。然后,特定于设备类型的规范将表示子类。(表示为RaspberryPiCfg、Esp8266Cfg等) 我正在尝试构建这样的结构,即我可以引用一个对象,该对象合并了通用规范(适用于所有设备)和设备类型特定规范Struct 如何将traits泛型类型链接到实现类型中的其他元素,struct,rust,traits,Struct,Rust,Traits,我正在尝试创建一个包含泛型字段的结构,该字段是基于另一个字段指定的类型。这个类型是我定义的几个结构之一 基本上,我在这里试图做的是创建一个模块,负责设备配置。这意味着要处理一系列不同的设备类型。需要为所有设备配置某些规范,并且我已经创建了一个结构,在OO范例中,它将是一个父类(这里表示为DeviceCfg)。然后,特定于设备类型的规范将表示子类。(表示为RaspberryPiCfg、Esp8266Cfg等) 我正在尝试构建这样的结构,即我可以引用一个对象,该对象合并了通用规范(适用于所有设备)和
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范例中,它将是一个父类
Rust不是一种面向对象的语言,所以应用OO范例是个坏主意。也没有理由使用特质。锈病特征是用来定义普通行为,而不是普通数据
我认为你应该使用组合而不是继承
<代码>枚举设备规格CCFG{
树莓,
Esp8266(Esp8266Cfg),
// ...
}
结构设备FG{
//公共字段。。。
特定于设备的\u cfg:DeviceSpecificCcfg
}
请查看如何创建一个,然后查看您的问题以将其包括在内。重点强调最小(这太长)和完整(这不是有效的Rust语法,不会产生您声称的错误)。尝试在一个全新的货运项目中重现您的错误。您可以使用以下方法来减少原始代码,以便在此处发布。请在将来花一些时间调查rustfmt和clippy,这两种方法都将使您的代码更加地道。地道的Rust使用
snake\u case
来表示变量、方法、宏和字段<代码>大写用于类型和枚举变量;对于静力学和常数,SNAKE\u案例
。请改用set\u specs
。我已经把你的文章和代码读了好几遍了,我仍然不知道你在问什么。一个超聚焦的MCVE真的会帮你解决这个问题(在任何情况下,99%的编程问题都是如此)。我不确定,但我认为你想做一些事情,一件事情的类型取决于另一件事情的价值。这将是困难的或不可能的生锈。Rust中的类型只是编译时的事情;不能有多个类型的值,不能查询其类型的值,也不能有仅依赖于运行时已知的条件的类型。你可以用任何做一些反射式的东西,但我怀疑这会解决你的问题(不管是什么)。Rust不是一种面向对象的语言-这是一个激烈争论的问题,通常取决于你认为“面向对象”是什么。例如,我知道Rust是OO,因为您创建的对象具有关联的数据和方法面向对象编程(OOP)是一种基于“对象”概念的编程范式,其中可能包含字段形式的数据,通常称为属性;代码,以过程的形式,通常被称为methods,因为Rust没有继承性,所以它不是OO。谢谢,我相信这就是我想要的。这次讨论很有启发性。