Peternator7 / strum

A small rust library for adding custom derives to enums
https://crates.io/crates/strum
MIT License
1.8k stars 152 forks source link

EnumDiscriminants: can't infer type #317

Closed wiiznokes closed 10 months ago

wiiznokes commented 12 months ago

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 commented 12 months ago

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

wiiznokes commented 12 months ago

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

Peternator7 commented 10 months ago

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.