pub struct ArrayMap<K: ArrayFinite<V>, V>(/* private fields */);Expand description
A complete mapping from keys of type K to values of type V, implemented using an array
indexed by Finite::index_of of the key.
§Example
use cantor::{Finite, ArrayMap};
// Define key type
#[derive(Finite, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
enum MyType {
A,
B(bool),
C(bool, bool)
};
// Initialize map
let mut map = ArrayMap::new(|x: MyType| match x {
MyType::A => false,
MyType::B(a) => a,
MyType::C(a, _) => a,
});
// Use map
map[MyType::C(true, true)] = false;
assert_eq!(map[MyType::A], false);
assert_eq!(map[MyType::B(true)], true);
assert_eq!(map[MyType::C(true, true)], false);Implementations§
Source§impl<K: ArrayFinite<V>, V> ArrayMap<K, V>
impl<K: ArrayFinite<V>, V> ArrayMap<K, V>
Sourcepub fn new(f: impl FnMut(K) -> V) -> Self
pub fn new(f: impl FnMut(K) -> V) -> Self
Constructs a new ArrayMap with initial values populated using the given function.
Sourcepub fn from(array: K::Array) -> Self
pub fn from(array: K::Array) -> Self
Constructs a new ArrayMap from an array of values, each corresponding to the key
determined by Finite::nth.
§Example
use cantor::*;
let map = ArrayMap::from([1, 3]);
assert_eq!(map[false], 1);
assert_eq!(map[true], 3);Sourcepub fn map_with_key<N>(&self, f: impl FnMut(K, &V) -> N) -> ArrayMap<K, N>where
K: ArrayFinite<N>,
pub fn map_with_key<N>(&self, f: impl FnMut(K, &V) -> N) -> ArrayMap<K, N>where
K: ArrayFinite<N>,
Applies a mapping function the values of this map.
Trait Implementations§
Source§impl<K: ArrayFinite<V>, V> Ord for ArrayMap<K, V>where
K::Array: Ord,
impl<K: ArrayFinite<V>, V> Ord for ArrayMap<K, V>where
K::Array: Ord,
Source§impl<K: ArrayFinite<V>, V> PartialOrd for ArrayMap<K, V>where
K::Array: PartialOrd,
impl<K: ArrayFinite<V>, V> PartialOrd for ArrayMap<K, V>where
K::Array: PartialOrd,
impl<K: ArrayFinite<V>, V> Copy for ArrayMap<K, V>where
K::Array: Copy,
impl<K: ArrayFinite<V>, V> Eq for ArrayMap<K, V>where
K::Array: Eq,
Auto Trait Implementations§
impl<K, V> Freeze for ArrayMap<K, V>where
<K as ArrayFinite<V>>::Array: Freeze,
impl<K, V> RefUnwindSafe for ArrayMap<K, V>where
<K as ArrayFinite<V>>::Array: RefUnwindSafe,
impl<K, V> Send for ArrayMap<K, V>where
<K as ArrayFinite<V>>::Array: Send,
impl<K, V> Sync for ArrayMap<K, V>where
<K as ArrayFinite<V>>::Array: Sync,
impl<K, V> Unpin for ArrayMap<K, V>where
<K as ArrayFinite<V>>::Array: Unpin,
impl<K, V> UnwindSafe for ArrayMap<K, V>where
<K as ArrayFinite<V>>::Array: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more