Trait contiguous_map::Key[][src]

pub trait Key where
    Self: Sized + Clone + Ord + Eq
{ fn add_one(&self) -> Option<Self>;
fn difference(&self, smaller: &Self) -> Option<usize>;
fn add_usize(&self, num: usize) -> Option<Self>; }
Expand description

Trait that must be implemented for all key types used in a ContiguousMap.

See the blanket implementation for an alternative way of implementing this trait using the ToIndex and TryFromIndex traits.

Required methods

Gets the next adjacent key. Returns None if there is no adjacent key due to self being the max key.

Gets the difference between this key and another one. Returns None if the difference does not fit in a usize.

Gets the key that is num steps after this key. Returns None if this overflows the key type.

Implementations on Foreign Types

Implementors

Blanket implementation of the Key trait for any type that implements ToIndex, TryFromIndex, and the basic trait requirements of Key.

Self and it’s index type must have the same ordering relationship. This means that x.cmp(&y) must always be equivalent to x.to_index().cmp(&y.to_index()).

All indexes for a type must be a contiguous group of the values of the index type. There is no requirement for what values this contiguous group starts and stops at.

Example

use contiguous_map::cmap;

#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
enum MyKey {
    One = 1,
    Two = 2,
    Three = 3,
}

impl contiguous_map::ToIndex for MyKey {
    type Index = u8;
    fn to_index(&self) -> Self::Index {
        *self as Self::Index
    }
}

impl contiguous_map::TryFromIndex for MyKey {
    fn try_from_index(index: Self::Index) -> Option<Self> {
        match index {
            1 => Some(Self::One),
            2 => Some(Self::Two),
            3 => Some(Self::Three),
            _ => None
        }
    }
}

let map = cmap!(MyKey::One => 10, 12);
assert_eq!(Some(&10), map.get(MyKey::One));
assert_eq!(Some(&12), map.get(MyKey::Two));
assert_eq!(None, map.get(MyKey::Three));