strum
strum copied to clipboard
EnumDiscriminants: can't infer type
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.
Moreover, into
to take ownership of the variable while it seems unnecessary.
I'm forced to call NodeTypeDiscriminants::from(&node.node_type)
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.