strum icon indicating copy to clipboard operation
strum copied to clipboard

EnumDiscriminants: can't infer type

Open wiiznokes opened this issue 7 months ago • 2 comments

I don't understand why I can't convert my enum into a discriminant. Here is the code:

#[derive(Debug, Clone, EnumDiscriminants)]
pub enum NodeType {
    Control(Control),
    Fan(Fan),
    Temp(Temp),
    CustomTemp(CustomTemp),
    Graph(Graph),
    Flat(Flat),
    Linear(Linear, LinearCache),
    Target(Target, TargetCache),
}

if node.node_type.into() == NodeTypeDiscriminants::Control {
   return Err(UpdateError::InvalidControl);
}
error[E0282]: type annotations needed
   --> data\src\update.rs:153:47
    |
153 | ...                   if node.node_type.into() == NodeTypeDiscriminants::Control {
    |                                         ^^^^
    |
help: try using a fully qualified path to specify the expected types
    |
153 |                             if <NodeType as Into<T>>::into(node.node_type) == NodeTypeDiscriminants::Control {
    |                                ++++++++++++++++++++++++++++              ~

For more information about this error, try `rustc --explain E0282`.

So, why not make an helper function, like node_type.to_discriminant() ? This will be a little more descriptive than into(), and will provide a similar alternative to into() in case of compiler problems.

wiiznokes avatar Dec 03 '23 18:12 wiiznokes

Moreover, into to take ownership of the variable while it seems unnecessary.

wiiznokes avatar Dec 03 '23 18:12 wiiznokes

I'm forced to call NodeTypeDiscriminants::from(&node.node_type)

wiiznokes avatar Dec 03 '23 18:12 wiiznokes

Hey @wiiznokes, naming things is hard so I try to avoid inherent methods on types. There's an infinite number of valid/useful names, I try to use the built-in traits from the standard library and then allow people to add their own inherent methods on top of it if they want to. It's not ideal, but it's the best compromise I can find between flexibility and causing a combinatorial explosion in the api surface of this crate.

Peternator7 avatar Jan 28 '24 01:01 Peternator7