Reginald Marr
Reginald Marr

Reputation: 417

How to link a traits generic type to some other element in the implemented type

I am trying to create a struct that contains a generic field which is type-specified based on another field. This type is to be one of several structs I have defined.

Essentially what I am trying to do here is create a module which takes care of device configuration. This is meant to handle a bunch of different device types. Certain specifications will need to be configured for all devices and I have created a struct which, in an OO paradigm would be a parent class (here denoted as DeviceCfg). The device-type-specific specs would then represent child classes. (These are denoted as RaspberryPiCfg,Esp8266Cfg, ect).

I am trying to structure things such that I can reference a single object which merges both the generic specs (that apply to all devices) as well as the device-type-specific specs.

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();
        }
    }
}

Thanks for all the help. I'm still very much a learner in rust.

Upvotes: 0

Views: 90

Answers (1)

Laney
Laney

Reputation: 1649

Certain specifications will need to be configured for all devices and I have created a struct which, in an OO paradigm would be a parent class

Rust is not an object-oriented language, so it is bad idea to apply OO paradigms. There's no reason to use traits too. Rust traits are for defining common behavior, not common data.

I believe you should use composition instead of inheritance.

enum DeviceSpecificCfg {
    RaspberryPi(RaspberryPiCfg),
    Esp8266(Esp8266Cfg),
    // ...
}

struct Devicecfg {
    // common fields ...
    device_specific_cfg: DeviceSpecificCfg
}

Upvotes: 1

Related Questions