pub trait Ordinal: Sized {
const ORDINAL_SIZE: usize;
// Required methods
fn ordinal(&self) -> usize;
fn from_ordinal(ordinal: usize) -> Option<Self>;
// Provided method
fn all_values() -> OrdinalValues<Self> ⓘ { ... }
}Expand description
Two-way map from a type to usize range from 0..ORDINAL_SIZE.
Two way ordinal mapping means:
- the type has
ORDINAL_SIZEpossible values - each value has a unique ordinal number in the range
0..ORDINAL_SIZE - each ordinal number corresponds to a unique value
This type is implemented for
- small integer types
- tuples
- some builtin types like
OptionandResult - and it can be derived with the
#[derive(Ordinal)]attribute for structs and enums
§Relation to Ord and PartialOrd
Implementations provided in this crate and generated with #[derive(Ordinal)]
are compatible with Ord and PartialOrd, meaning a < b <=> a.ordinal() < b.ordinal()
with the exception of derive on enums with explicit discriminants
(derive ignores them and assigns ordinal numbers in order of declaration).
This is not enforced by the trait itself, but it is a good practice to follow.
§Derive
#[derive(Ordinal)] works for arbitrary structs and enums.
Generated implementation is compatible with #[derive(PartialOrd)].
use ordinal_map::Ordinal;
#[derive(Ordinal)]
enum Color {
Red,
Cyan,
Blue,
}
#[derive(Ordinal)]
struct Bright(bool);
#[derive(Ordinal)]
enum MaybeColor {
Default,
Colored(Color, Bright),
Invisible,
}
assert_eq!(0, MaybeColor::Default.ordinal());
assert_eq!(1, MaybeColor::Colored(Color::Red, Bright(false)).ordinal());
assert_eq!(2, MaybeColor::Colored(Color::Red, Bright(true)).ordinal());
assert_eq!(3, MaybeColor::Colored(Color::Cyan, Bright(false)).ordinal());
assert_eq!(4, MaybeColor::Colored(Color::Cyan, Bright(true)).ordinal());§See also
Required Associated Constants§
Sourceconst ORDINAL_SIZE: usize
const ORDINAL_SIZE: usize
Number of possible values.
It is compile-time error if the number of possible values is greater than usize::MAX.
Required Methods§
Sourcefn ordinal(&self) -> usize
fn ordinal(&self) -> usize
Index of the ordinal.
§Example
use std::num::NonZeroI16;
use ordinal_map::Ordinal;
assert_eq!(0, None::<u16>.ordinal());
assert_eq!(1, Some::<u16>(0).ordinal());
assert_eq!(2, Some::<u16>(1).ordinal());Sourcefn from_ordinal(ordinal: usize) -> Option<Self>
fn from_ordinal(ordinal: usize) -> Option<Self>
Returns the ordinal from the index.
§Example
use ordinal_map::Ordinal;
assert_eq!(i16::MIN, i16::from_ordinal(0).unwrap());
assert_eq!(i16::MIN + 1, i16::from_ordinal(1).unwrap());Provided Methods§
Sourcefn all_values() -> OrdinalValues<Self> ⓘ
fn all_values() -> OrdinalValues<Self> ⓘ
Iterate over all possible values.
Values are returned in order of their ordinal numbers.
§Example
use ordinal_map::Ordinal;
let mut iter = i16::all_values();
assert_eq!(Some(i16::MIN), iter.next());
assert_eq!(Some(i16::MAX), iter.next_back());Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl Ordinal for Infallible
impl Ordinal for Infallible
Source§impl Ordinal for NonZeroI16
impl Ordinal for NonZeroI16
Source§impl Ordinal for NonZeroI32
impl Ordinal for NonZeroI32
Source§impl Ordinal for NonZeroI64
impl Ordinal for NonZeroI64
Source§impl Ordinal for NonZeroIsize
impl Ordinal for NonZeroIsize
Source§impl Ordinal for NonZeroU16
impl Ordinal for NonZeroU16
Source§impl Ordinal for NonZeroU32
impl Ordinal for NonZeroU32
Source§impl Ordinal for NonZeroU64
It is compile-time error to use this impl on 32-bit platforms.
impl Ordinal for NonZeroU64
It is compile-time error to use this impl on 32-bit platforms.