Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the closest I can get to discriminating an enum by a char?

Tags:

enums

rust

I've written this question out many times, and have finally realized that my biggest problem is that I don't know how I want to represent this data, and that's making it really hard to reason about the rest of the code.

The way the data is represented in Python:

class LSP():
    C_MASK_MAP={
        "A":"Ch A",
        "B":"Ch B",
        "C":"Ch C",
        "D":"Ch D",
        "T":"Tmpr",
        "Y":"Batt",
        "L":"Acc"
    }

    ADC_CHANS= (
        "Ch A",
        "Ch B",
        "Ch C",
        "Ch D",
        "Tmpr",
        "Batt"
    )

    ADC_MAJORS = (
        "Ch A",
        "Ch B",
        "Ch C",
    )

My imaginary Rust code (I realize the names will need updating but are the same here for clarity):

enum C_MASK_MAP {
    Ch_A = 'A',
    Ch_B = 'B',
    Ch_C = 'C',
    Ch_D = 'D',
    Tmpr = 'T',
    Batt = 'Y',
    Acc  = 'L'
}
//...
let ADC_CHANS = [
    C_MASK_MAP::Ch_A,
    C_MASK_MAP::Ch_B,
    C_MASK_MAP::Ch_C,
    C_MASK_MAP::Ch_D,
    C_MASK_MAP::Tmpr,
    C_MASK_MAP::Batt
];

ADC_MAJORS = [
    C_MASK_MAP::Ch_A,
    C_MASK_MAP::Ch_B,
    C_MASK_MAP::Ch_C,
];

I've considered making C_MASK_MAP a HashMap<char, &'static str>, but then I ran into a huge mess trying not to make a million copies of the strs everywhere and dealing with lifetimes while avoiding making Strings, and the syntactic mess that is a reference to a static str (&&'static str or something).

I think there'd be a real benefit to being able to use an enum (or similar) because the values wouldn't be as big and are more easily interchanged C_MASK_MAP.get(key).expect("invalid key") vs just casting.

like image 539
Camden Narzt Avatar asked Feb 09 '16 04:02

Camden Narzt


People also ask

Can enum have char value?

First of all, YES, we can assign an Enum to something else, a char !

How to parse string into enum?

Parse() method parse the specified string to enum member. However, it throws an exception if the specified string does not match with any enum member name. In the above example, the Enum. Parse() method converts the string value day1 to enumeration type and returns the result as an enumeration object.

How to use enum parse?

Parse<TEnum>(String, Boolean)Converts the string representation of the name or numeric value of one or more enumerated constants specified by TEnum to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

How do you find the value of an enum?

Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.


2 Answers

Your strings are sentinel values; this is a common pattern in Python, but is not how things should be done in Rust: enums are what such things should be: you’re encoding the legal values in the type system.

You could end up with something like this:

#[derive(Clone, Copy)]
#[repr(u8)]
pub enum Mask {
    ChA = b'A',
    ChB = b'B',
    ChC = b'C',
    ChD = b'D',
    Tmpr = b'T',
    Batt = b'Y',
    Acc  = b'L',
}

// e.g. Mask::ChA.into() == 'A'
impl Into<char> for Mask {
    fn into(self) -> char {
        self as u8 as char
    }
}

impl Mask {
    // e.g. Mask::from('A') == Ok(Mask::ChA)
    pub fn from(c: char) -> Result<Mask, ()> {
        match c {
            'A' => Ok(Mask::ChA),
            'B' => Ok(Mask::ChB),
            'C' => Ok(Mask::ChC),
            'D' => Ok(Mask::ChD),
            'T' => Ok(Mask::Tmpr),
            'Y' => Ok(Mask::Batt),
            'L' => Ok(Mask::Acc),
            _ => Err(()),
        }
    }

    // e.g. Mask::ChA.is_chan() == true
    pub fn is_chan(&self) -> bool {
        match *self {
            Mask::ChA | Mask::ChB | Mask::ChC | Mask::ChD | Mask::Tmpr | Mask::Batt => true,
            Mask::Acc => false,
        }
    }

    // e.g. Mask::ChD.is_major() == false
    pub fn is_major(&self) -> bool {
        match *self {
            Mask::ChA | Mask::ChB | Mask::ChC => true,
            Mask::ChD | Mask::Tmpr | Mask::Batt | Mask::Acc => false,
        }
    }
}

If you wanted you could implement std::str::FromStr for Mask as well, which would allow "A".parse() == Ok(Mask::ChA):

impl FromStr for Mask {
    type Err = ();

    fn from_str(s: &str) -> Result<Mask, ()> {
        match s {
            "A" => Ok(Mask::ChA),
            "B" => Ok(Mask::ChB),
            "C" => Ok(Mask::ChC),
            "D" => Ok(Mask::ChD),
            "T" => Ok(Mask::Tmpr),
            "Y" => Ok(Mask::Batt),
            "L" => Ok(Mask::Acc),
            _ => Err(()),
        }
    }
}

I suspect that is_chan et al. may be more suitable than ADC_CHANS et al., but if you do actually need them, they work fine (you could do [Mask; 6] too, but if you need to add new elements it’d change the type which is an API compatibility break if public):

pub static ADC_CHANS: &'static [Mask] = &[
    Mask::ChA,
    Mask::ChB,
    Mask::ChC,
    Mask::ChD,
    Mask::Tmpr,
    Mask::Batt,
];

pub static ADC_MAJORS: &'static [Mask] = &[
    Mask::ChA,
    Mask::ChB,
    Mask::ChC,
];
like image 144
Chris Morgan Avatar answered Oct 30 '22 08:10

Chris Morgan


Copying a &'static str (i.e. copying the reference only) has no cost. A deep copy of the string would be a clone and would be typed as a String.

If &'static str is too verbose for you, you can always define a type alias.

type Str = &'static str;

HashMap<char, &'static str> corresponds nicely to your original map. However, if you don't need the full range of char for the key and you don't actually need to have the value typed as a char anywhere besides indexing the map, you should use an enum instead, as that will restrict the legal values that can be used as keys.

like image 41
Francis Gagné Avatar answered Oct 30 '22 07:10

Francis Gagné