pub trait ENum: Sized {
fn try_from_num(num: usize) -> Option<Self>;
fn to_num(&self) -> usize;
fn from_num(num: usize) -> Self { ... }
}
Required Methods§
sourcefn try_from_num(num: usize) -> Option<Self>
fn try_from_num(num: usize) -> Option<Self>
The error-handling counterpart of from_num()
.
Use this if you’re not sure whether or not the number you’re passing to the function is valid.
Examples
if let Some(val) = A::try_from_num(sketchy_number) {
// handle val
} else {
// handle error
}
Provided Methods§
sourcefn from_num(num: usize) -> Self
fn from_num(num: usize) -> Self
Parse a number into the type.
If you’re impl
ing ENum
yourself, you don’t need to
define this function, there is a default implementation
that uses the output from try_from_num()
.
Examples
let a = A::from_num(num);
assert!(match a {
A::B => true,
_ => false,
});
Panics
This function should panic if it cannot parse the number into
its type; e.g. you should only pass to this function the output
of .to_num()
. If you want to handle a parsing error, use try_from_num()
.