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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::{
    misc::{ BufferTooSmall, RangeBoundsExt },
    traits::{ ArrayRef, ArrayMut, ArrayAlloc, CanAlloc },
    std::{
        cmp::Ordering, ops::RangeBounds,
        fmt::{ self, Debug, Formatter },
        hash::{ Hash, Hasher },
        slice::{ Iter as SliceIter, IterMut as SliceIterMut }
    }
};


/// A wrapper for array types that exposes checked APIs only
pub struct Array<Wrapped> {
    /// The wrapped element
    wrapped: Wrapped
}
impl<Wrapped> Array<Wrapped> {
    /// Wraps an `array`
    pub const fn new(array: Wrapped) -> Self {
        Self { wrapped: array }
    }

    /// Returns the wrapped array
    #[inline(always)]
    pub fn into_inner(self) -> Wrapped {
        self.wrapped
    }
}
impl<Wrapped> AsRef<Array<Wrapped>> for Array<Wrapped> {
    fn as_ref(&self) -> &Array<Wrapped> {
        self
    }
}
impl<T, Wrapped> ArrayRef<T> for Array<Wrapped> where Wrapped: AsRef<[T]> {
    fn as_slice(&self) -> &[T] {
        self.wrapped.as_ref()
    }
    fn len(&self) -> usize {
        self.as_slice().len()
    }
    fn is_empty(&self) -> bool {
        self.as_slice().is_empty()
    }

    fn get(&self, index: usize) -> Option<&T> {
        self.as_slice().get(index)
    }
    fn get_n<Range>(&self, range: Range) -> Option<Array<&[T]>> where Range: RangeBounds<usize> {
        let slice = self.as_slice();
        let range = range.into_absolute(0, slice.len())?;
        slice.get(range).map(Array::new)
    }

    fn first(&self) -> Option<&T> {
        self.as_slice().first()
    }
    fn last(&self) -> Option<&T> {
        self.as_slice().last()
    }

    fn iter(&self) -> SliceIter<T> {
        self.as_slice().iter()
    }

    fn clone_to<Source>(&self, dest: &mut Source) -> Result<(), BufferTooSmall> where Source: ArrayMut<T>, T: Clone {
        // Validate length
        if self.len() > dest.len() {
            Err(BufferTooSmall)?;
        }

        // Clone the source elements to dest
        dest.iter_mut().zip(self.iter()).for_each(|(t, e)| *t = e.clone());
        Ok(())
    }
}
impl<T, Wrapped> ArrayMut<T> for Array<Wrapped> where Wrapped: AsRef<[T]> + AsMut<[T]> {
    fn as_slice_mut(&mut self) -> &mut [T] {
        self.wrapped.as_mut()
    }

    fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        self.as_slice_mut().get_mut(index)
    }
    fn get_n_mut<Range>(&mut self, range: Range) -> Option<Array<&mut [T]>> where Range: RangeBounds<usize> {
        let slice = self.as_slice_mut();
        let range = range.into_absolute(0, slice.len())?;
        slice.get_mut(range).map(Array::new)
    }

    fn first_mut(&mut self) -> Option<&mut T> {
        self.as_slice_mut().first_mut()
    }
    fn last_mut(&mut self) -> Option<&mut T> {
        self.as_slice_mut().last_mut()
    }

    fn iter_mut(&mut self) -> SliceIterMut<T> {
        self.as_slice_mut().iter_mut()
    }

    fn rotate_left(&mut self, count: usize) {
        // Avoid division by zero
        if self.is_empty() {
            return;
        }

        let count = count % self.len();
        self.as_slice_mut().rotate_left(count);
    }
    fn rotate_right(&mut self, count: usize) {
        // Avoid division by zero
        if self.is_empty() {
            return;
        }

        let count = count % self.len();
        self.as_slice_mut().rotate_right(count);
    }
    fn reverse(&mut self) {
        self.as_slice_mut().reverse()
    }
}
impl<T, Wrapped> ArrayAlloc<T> for Array<Wrapped> where Wrapped: AsRef<[T]> + AsMut<[T]> + CanAlloc<T> {
    type Error = Wrapped::Error;
    
    fn alloc_new() -> Result<Self, Self::Error> {
        Ok(Self::new(Wrapped::alloc_new()?))
    }
    fn alloc_clone<Source>(elements: &Source) -> Result<Self, Self::Error> where Source: ArrayRef<T>, T: Clone {
        let mut this = Self::alloc_new()?;
        this.push_n_back(elements)?;
        Ok(this)
    }
    
    fn grow_with(&mut self, len: usize, mut init: impl FnMut() -> T) -> Result<(), Self::Error> {
        for _ in 0 .. len.saturating_sub(self.len()) {
            self.push_back(init())?;
        }
        Ok(())
    }
    fn grow(&mut self, len: usize) -> Result<(), Self::Error> where T: Default {
        self.grow_with(len, T::default)
    }
    fn shrink(&mut self, len: usize) -> Result<(), Self::Error> {
        for _ in 0 .. self.len().saturating_sub(len) {
            let _ = self.wrapped.pop()?;
        }
        Ok(())
    }

    fn push_front(&mut self, element: T) -> Result<(), Self::Error> {
        self.push_back(element)?;
        self.rotate_right(1);
        Ok(())
    }
    fn push_n_front<Source>(&mut self, elements: &Source) -> Result<(), Self::Error>
        where Source: ArrayRef<T>, T: Clone
    {
        self.push_n_back(elements)?;
        self.rotate_right(elements.len());
        Ok(())
    }
    fn push_back(&mut self, element: T) -> Result<(), Self::Error> {
        self.wrapped.push(element)
    }
    fn push_n_back<Source>(&mut self, elements: &Source) -> Result<(), Self::Error>
        where Source: ArrayRef<T>, T: Clone
    {
        elements.iter().cloned().try_for_each(|e| self.push_back(e))
    }

    fn pop_front(&mut self) -> Result<Option<T>, Self::Error> {
        self.rotate_left(1);
        self.pop_back()
    }
    fn pop_n_front(&mut self, len: usize) -> Result<Option<Self>, Self::Error> {
        self.rotate_left(len);
        self.pop_n_back(len)
    }
    fn pop_back(&mut self) -> Result<Option<T>, Self::Error> {
        self.wrapped.pop()
    }
    fn pop_n_back(&mut self, len: usize) -> Result<Option<Self>, Self::Error> {
        // Don't mutate `self` if the operation cannot succeed
        if self.len() < len {
            return Ok(None)
        }

        // Move element by element into the new array
        let mut popped = Self::alloc_new()?;
        for _ in 0 .. len {
            let element = self.pop_back()?.expect("Failed to pop existing element?!");
            popped.push_back(element)?;
        }

        // Reverse the order in the new array and return it
        popped.reverse();
        Ok(Some(popped))
    }
}
// - MARK: Propagate common trait implementations
impl<Wrapped> Debug for Array<Wrapped> where Wrapped: Debug {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.wrapped.fmt(f)
    }
}
impl<Wrapped> Default for Array<Wrapped> where Wrapped: Default {
    fn default() -> Self {
        Self { wrapped: Wrapped::default() }
    }
}
impl<Wrapped> Copy for Array<Wrapped> where Wrapped: Copy {
    /* Copy is an intrinsic marker trait; no implementation required */
}
impl<Wrapped> Clone for Array<Wrapped> where Wrapped: Clone {
    fn clone(&self) -> Self {
        Self { wrapped: self.wrapped.clone() }
    }
}
impl<Wrapped> PartialEq for Array<Wrapped> where Wrapped: PartialEq {
    fn eq(&self, other: &Self) -> bool {
        self.wrapped.eq(&other.wrapped)
    }
}
impl<Wrapped> Eq for Array<Wrapped> where Wrapped: Eq {
    /* Copy is a marker trait; no implementation required */
}
impl<Wrapped> PartialOrd for Array<Wrapped> where Wrapped: PartialOrd {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.wrapped.partial_cmp(&other.wrapped)
    }
}
impl<Wrapped> Ord for Array<Wrapped> where Wrapped: Ord {
    fn cmp(&self, other: &Self) -> Ordering {
        self.wrapped.cmp(&other.wrapped)
    }
}
impl<Wrapped> Hash for Array<Wrapped> where Wrapped: Hash {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.wrapped.hash(state)
    }
}
impl<Wrapped> IntoIterator for Array<Wrapped> where Wrapped: IntoIterator {
    type Item = Wrapped::Item;
    type IntoIter = Wrapped::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.wrapped.into_iter()
    }
}