1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use super::{Iter, IterMut, TypedIndex};
use std::{
    marker::PhantomData,
    ops::{Index, IndexMut},
};

/// This is a dynamically-sized slice
/// that can only be indexed by the
/// correct index type.
#[derive(Debug)]
pub struct SliceMap<K, V>
where
    K: TypedIndex,
{
    _marker: PhantomData<K>,
    slice: [V],
}

impl<K, V> SliceMap<K, V>
where
    K: TypedIndex,
{
    /// Gets a reference to the value at the given index.
    pub fn get(&self, index: K) -> Option<&V> {
        self.slice.get(index.index())
    }

    /// Gets a mutable reference to the value at the given index.
    pub fn get_mut(&mut self, index: K) -> Option<&mut V> {
        self.slice.get_mut(index.index())
    }

    /// Gets the length of this slice map.
    pub fn len(&self) -> usize {
        self.slice.len()
    }

    /// Returns an iterator for this slice map.
    pub fn iter(&self) -> Iter<K, V> {
        Iter::new(self.slice.iter())
    }

    /// Returns a mutable iterator for this slice map.
    pub fn iter_mut(&mut self) -> IterMut<K, V> {
        IterMut::new(self.slice.iter_mut())
    }

    /// Gets a pointer to the `SliceMap`.
    pub fn as_ptr(&self) -> *const V {
        self as *const SliceMap<K, V> as *const V
    }

    /// Gets a mutable pointer to the `SliceMap`.
    pub fn as_mut_ptr(&mut self) -> *mut V {
        self as *mut SliceMap<K, V> as *mut V
    }
}

impl<K, V> Index<K> for SliceMap<K, V>
where
    K: TypedIndex,
{
    type Output = V;
    fn index(&self, index: K) -> &V {
        &self.slice[index.index()]
    }
}

impl<K, V> IndexMut<K> for SliceMap<K, V>
where
    K: TypedIndex,
{
    fn index_mut(&mut self, index: K) -> &mut V {
        &mut self.slice[index.index()]
    }
}