num-derive icon indicating copy to clipboard operation
num-derive copied to clipboard

Documentation inaccurate?

Open ruckc opened this issue 5 years ago • 1 comments

I'm trying to use num_derive, but the way I got it working doesn't align to the documentation.

Enum definition: https://github.com/ruckc/mcr/blob/num_derive/src/igmp/types.rs#L4

Seemingly required use: https://github.com/ruckc/mcr/blob/num_derive/src/igmp/header.rs#L3

For some reason in the files i'm trying to use the from_u8 function, i have to use crate::num::FromPrimitive and depend on num = "0.2.0" in Cargo.toml`

Without the num crate or use crate::num::FromPrimitive I get this error:

error[E0599]: no variant or associated item named `from_u8` found for type `igmp::types::IgmpType` in the current scope
  --> src/igmp/header.rs:13:19
   |
13 |         IgmpType::from_u8(self.itype)
   |                   ^^^^^^^ variant or associated item not found in `igmp::types::IgmpType`
   |
  ::: src/igmp/types.rs:5:1
   |
5  | pub enum IgmpType {
   | ----------------- variant or associated item `from_u8` not found here
   |
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope, perhaps add a `use` for it:
           `use crate::num::FromPrimitive;`

error: aborting due to previous error

ruckc avatar Aug 04 '19 20:08 ruckc

You do need to import the trait into scope where you want to call its methods, but it should be fine to just use num_traits::FromPrimitive. I don't know why the compiler would suggest using the re-export from num instead -- maybe that's already added elsewhere in your dependency tree, and it's getting confused about what path should be the canonical suggestion.

cuviper avatar Aug 05 '19 18:08 cuviper