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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::*;
use core::convert::TryFrom;
use core::ops::Index;
use core::{fmt, ops};

// === constructors ===

impl<T, const N: usize> Default for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<T, const N: usize> TryFrom<[T; N]> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    type Error = ([T; N], self::errors::IsSentinel<T>);
    fn try_from(arr: [T; N]) -> Result<Self, Self::Error> {
        Self::from_array(arr)
    }
}

impl<T, const N: usize> core::iter::FromIterator<T> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = T>,
    {
        let mut v = Arrav::new();
        v.extend(iter);
        v
    }
}

impl<T, const N: usize> core::iter::Extend<T> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>,
    {
        let start = self.len();
        let iter = iter.into_iter();
        for (i, t) in iter.enumerate() {
            if i == self.capacity() {
                panic!("iterator does not fit in Arrav<_, {}>", N)
            } else if t == T::SENTINEL {
                panic!("iterator produced sentinel value")
            } else {
                self.ts[start + i] = t;
            }
        }
    }
}

// === printers ===

impl<T: fmt::Debug, const N: usize> fmt::Debug for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

// === derefs ===

impl<T, const N: usize> AsRef<[T]> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn as_ref(&self) -> &[T] {
        let len = self.len();
        &self.ts[..len]
    }
}

impl<T, const N: usize> core::ops::Deref for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    type Target = [T];
    fn deref(&self) -> &Self::Target {
        let len = Self::len(self);
        &self.ts[..len]
    }
}

// NOTE: we cannot safely implement DerefMut or IndexMut, since the user might override
// the value with the sentinel, which would invalidate the data structure's integrity!

impl<T, const N1: usize, const N2: usize> PartialEq<Arrav<T, N2>> for Arrav<T, N1>
where
    T: PartialEq + Copy + Sentinel,
    [T; N1]: core::array::LengthAtMost32,
    [T; N2]: core::array::LengthAtMost32,
{
    fn eq(&self, rhs: &Arrav<T, N2>) -> bool {
        let mut rhs = rhs.iter();
        for t in self.iter() {
            match rhs.next() {
                Some(rhs) if rhs == t => {}
                _ => return false,
            }
        }
        rhs.next().is_none()
    }
}

impl<T, const N: usize> Eq for Arrav<T, N>
where
    T: Eq + Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
}

impl<T, const N1: usize, const N2: usize> PartialOrd<Arrav<T, N2>> for Arrav<T, N1>
where
    T: PartialOrd + Copy + Sentinel,
    [T; N1]: core::array::LengthAtMost32,
    [T; N2]: core::array::LengthAtMost32,
{
    fn partial_cmp(&self, other: &Arrav<T, N2>) -> Option<core::cmp::Ordering> {
        self.iter().partial_cmp(other.iter())
    }
}

impl<T, const N: usize> Ord for Arrav<T, N>
where
    T: Ord + Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.iter().cmp(other.iter())
    }
}

impl<T, const N: usize> PartialEq<[T]> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn eq(&self, rhs: &[T]) -> bool {
        if rhs.len() > N {
            // can't possibly be ==
            return false;
        }

        let mut rhs = rhs.iter();
        for t in self.iter() {
            match rhs.next() {
                Some(rhs) if rhs == &t => {}
                _ => return false,
            }
        }
        rhs.next().is_none()
    }
}

impl<T, const N: usize, const AN: usize> PartialEq<[T; AN]> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    fn eq(&self, rhs: &[T; AN]) -> bool {
        self == &rhs[..]
    }
}

// === accessors ===

impl<T, const N: usize> Index<usize> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    type Output = T;
    fn index(&self, idx: usize) -> &Self::Output {
        self.get(idx)
            .unwrap_or_else(move || errors::index_len_fail(idx, self.len()))
    }
}

impl<T, const N: usize> Index<ops::Range<usize>> for Arrav<T, N>
where
    T: Copy + Sentinel,
    [T; N]: core::array::LengthAtMost32,
{
    type Output = [T];
    fn index(&self, idx: ops::Range<usize>) -> &Self::Output {
        if idx.start > idx.end {
            errors::index_order_fail(idx.start, idx.end)
        } else if idx.end > self.capacity()
            || (idx.end != 0 && *unsafe { self.get_unchecked(idx.end - 1) } == T::SENTINEL)
        {
            errors::index_len_fail(idx.end, self.len())
        }
        unsafe { self.get_unchecked_range(idx) }
    }
}