Trait e_num::ENum

source ·
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§

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
}

Convert self to a serializable number.

Examples
let num = A::B.to_num();
// later...
let a = A::from_num(num);
assert!(match a {
  A::B => true,
  _ => false,
});

Provided Methods§

Parse a number into the type.

If you’re impling 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().

Implementations on Foreign Types§

Implementors§