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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//!Static vector
//!
use core::{mem, ptr, slice};

///Static array with `Vec`-like interface
pub struct Array<T, const C: usize> {
    inner: mem::MaybeUninit<[T; C]>,
    len: usize,
}

impl<T, const C: usize> Array<T, C> {
    #[inline]
    ///Creates new empty instance
    pub const fn new() -> Self {
        Self {
            inner: mem::MaybeUninit::uninit(),
            len: 0,
        }
    }

    #[inline]
    ///Returns length of vector.
    pub const fn len(&self) -> usize {
        self.len
    }

    #[inline(always)]
    ///Returns pointer to first element in underlying buffer.
    pub const fn as_ptr(&self) -> *const T {
        &self.inner as *const _ as *const _
    }


    #[inline(always)]
    ///Returns pointer to first element in underlying buffer.
    pub fn as_mut_ptr(&mut self) -> *mut T {
        &mut self.inner as *mut _ as *mut _
    }

    #[inline(always)]
    fn as_elem(&self, pos: usize) -> *const T {
        let ptr = self.as_ptr();
        unsafe {
            ptr.add(pos)
        }
    }

    #[inline(always)]
    fn as_mut_elem(&mut self, pos: usize) -> *mut T {
        let ptr = self.as_mut_ptr();
        unsafe {
            ptr.add(pos)
        }
    }

    #[inline]
    ///Retrieves reference to element without checking boundaries.
    pub unsafe fn get_unchecked(&self, index: usize) -> &T {
        &*self.as_elem(index)
    }

    #[inline]
    ///Retrieves mutable reference to element without checking boundaries.
    pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
        &mut *self.as_mut_elem(index)
    }

    #[inline]
    ///Returns immutable slice with current elements
    pub fn as_slice(&self) -> &[T] {
        unsafe {
            slice::from_raw_parts(self.as_elem(0), self.len)
        }
    }

    #[inline]
    ///Returns mutable slice with current elements
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        unsafe {
            slice::from_raw_parts_mut(self.as_mut_elem(0), self.len)
        }
    }

    fn inner_truncate(&mut self, len: usize) {
        if mem::needs_drop::<T>() {
            loop {
                unsafe {
                    ptr::drop_in_place(self.as_mut_elem(self.len - 1));
                }
                self.len -= 1;

                if self.len == len {
                    break;
                }
            }
        } else {
            self.len = len;
        }
    }

    ///Shortens vector, keeping the first `len` elements.
    ///
    ///Does nothing if `len` is greater or equal to vector length.
    pub fn truncate(&mut self, len: usize) {
        if len >= self.len {
            return;
        }
        self.inner_truncate(len);
    }

    ///Returns whether vector is empty.
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    ///Returns vector capacity.
    pub const fn capacity(&self) -> usize {
        C
    }

    ///Sets new length of vector.
    ///
    ///# Notes:
    ///
    ///Panics in debug mode only when `new_len` is greater than CAPACITY.
    pub unsafe fn set_len(&mut self, new_len: usize) {
        debug_assert!(new_len <= self.capacity());
        self.len = new_len;
    }

    #[inline]
    ///Removes all elements from vector
    pub fn clear(&mut self) {
        self.truncate(0);
    }

    #[inline]
    ///Appends element at the end, without checking capacity
    pub unsafe fn push_unchecked(&mut self, value: T) {
        ptr::write(self.as_mut_elem(self.len), value);
        self.len += 1;
    }

    #[must_use]
    ///Appends element at the end.
    ///
    ///Returns `Some(T)` on capacity overflow
    pub fn push(&mut self, value: T) -> Option<T> {
        match self.len == self.capacity() {
            true => Some(value),
            false => unsafe {
                self.push_unchecked(value);
                None
            },
        }
    }

    #[inline]
    ///Unconditionally retrieves element from vector.
    pub unsafe fn pop_unchecked(&mut self) -> T {
        let result = ptr::read(self.as_elem(self.len - 1));

        self.len -= 1;

        result
    }

    ///Pops element out of vector.
    pub fn pop(&mut self) -> Option<T> {
        match self.len {
            0 => None,
            _ => unsafe {
                Some(self.pop_unchecked())
            }
        }
    }

    ///Removes element at `index` by swapping it with last element, and popping out.
    pub unsafe fn swap_remove_unchecked(&mut self, index: usize) -> T {
        ptr::swap(self.as_mut_elem(index), self.as_mut_elem(self.len - 1));
        self.pop_unchecked()
    }

    ///Removes element at `index` by swapping it with last element, and popping out.
    ///
    ///## Note:
    ///
    ///Panics when `index` is out of bounds
    pub fn swap_remove(&mut self, index: usize) -> T {
        assert!(index < self.len);
        unsafe {
            self.swap_remove_unchecked(index)
        }
    }

    ///Resizes vector with provided `value`
    ///
    ///If `new_len` is greater than `len`, the `Array` is extended by the difference, with each
    ///additional slot filled with value. If `new_len` is less than `len`, the `Array` is simply
    ///truncated.
    pub unsafe fn resize_unchecked(&mut self, new_len: usize, value: T) where T: Clone {
        match new_len > self.len() {
            true => while self.len() < new_len {
                self.push_unchecked(value.clone());
            },
            false => self.truncate(new_len),
        }
    }

    #[inline]
    ///Resizes vector with provided `value`
    ///
    ///If `new_len` is greater than `len`, the `Array` is extended by the difference, with each
    ///additional slot filled with value. If `new_len` is less than `len`, the `Array` is simply
    ///truncated.
    ///
    ///## Note:
    ///
    ///Panics if `new_len` is greater than `CAPACITY`
    pub fn resize(&mut self, new_len: usize, value: T) where T: Clone {
        assert!(new_len <= self.capacity());
        unsafe {
            self.resize_unchecked(new_len, value);
        }
    }

    ///Resizes vector with default values.
    ///
    ///If `new_len` is greater than `len`, the `Array` is extended by the difference, with each
    ///additional slot filled with value. If `new_len` is less than `len`, the `Array` is simply
    ///truncated.
    pub unsafe fn resize_default_unchecked(&mut self, new_len: usize) where T: Default {
        match new_len > self.len() {
            true => while self.len() < new_len {
                self.push_unchecked(T::default());
            },
            false => self.truncate(new_len),
        }
    }

    #[inline]
    ///Resizes vector with default values.
    ///
    ///If `new_len` is greater than `len`, the `Array` is extended by the difference, with each
    ///additional slot filled with value. If `new_len` is less than `len`, the `Array` is simply
    ///truncated.
    ///
    ///## Note:
    ///
    ///Panics if `new_len` is greater than `CAPACITY`
    pub fn resize_default(&mut self, new_len: usize) where T: Default {
        assert!(new_len <= self.capacity());
        unsafe {
            self.resize_default_unchecked(new_len);
        }
    }
}

impl<T, const C: usize> Drop for Array<T, C> {
    #[inline]
    fn drop(&mut self) {
        self.clear();
    }
}

impl<T, const C: usize> core::ops::Deref for Array<T, C> {
    type Target = [T];

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<T, const C: usize> core::ops::DerefMut for Array<T, C> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_slice()
    }
}

impl<T, const C: usize> AsRef<Array<T, C>> for Array<T, C> {
    #[inline]
    fn as_ref(&self) -> &Self {
        self
    }
}

impl<T, const C: usize> AsMut<Array<T, C>> for Array<T, C> {
    #[inline]
    fn as_mut(&mut self) -> &mut Self {
        self
    }
}

impl<T, const C: usize> AsRef<[T]> for Array<T, C> {
    #[inline]
    fn as_ref(&self) -> &[T] {
        self
    }
}

impl<T, const C: usize> AsMut<[T]> for Array<T, C> {
    #[inline]
    fn as_mut(&mut self) -> &mut [T] {
        self
    }
}

impl<T: core::fmt::Debug, const C: usize> core::fmt::Debug for Array<T, C> {
    #[inline(always)]
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        f.debug_list().entries(self.as_slice().iter()).finish()
    }
}

impl<T: Clone, const C: usize> Clone for Array<T, C> {
    fn clone(&self) -> Self {
        let mut result = Self {
            inner: mem::MaybeUninit::uninit(),
            len: self.len,

        };

        unsafe {
            self.inner.as_ptr().copy_to_nonoverlapping(result.inner.as_mut_ptr(), 1);
        }
        result
    }
}

impl<T: PartialEq, const C: usize> PartialEq for Array<T, C> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<T: PartialEq, const C: usize> PartialEq<[T]> for Array<T, C> {
    #[inline]
    fn eq(&self, other: &[T]) -> bool {
        self.as_slice() == other
    }
}

impl<T: PartialEq, const C: usize> PartialEq<&'_ [T]> for Array<T, C> {
    #[inline]
    fn eq(&self, other: &&[T]) -> bool {
        self.as_slice() == *other
    }
}

impl<T: Eq, const C: usize> Eq for Array<T, C> {
}

#[cfg(feature = "std")]
impl<const C: usize> std::io::Write for Array<u8, C> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let write_len = core::cmp::min(self.capacity() - self.len(), buf.len());
        let dest = self.as_mut_elem(self.len);
        let src = buf.as_ptr();
        unsafe {
            ptr::copy_nonoverlapping(src, dest, write_len);
        }
        self.len += write_len;

        Ok(write_len)
    }

    #[inline]
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

pub struct ArrayConsumer<T, const C: usize> {
    inner: Array<T, C>,
    cursor: usize,
}

impl<T, const C: usize> Iterator for ArrayConsumer<T, C> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        if self.cursor < self.inner.len() {
            let result = unsafe {
                ptr::read(self.inner.as_elem(self.cursor))
            };
            self.cursor += 1;
            Some(result)
        } else {
            None
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let size = self.inner.len() - self.cursor;
        (size, Some(size))
    }
}

impl<T, const C: usize> Drop for ArrayConsumer<T, C> {
    fn drop(&mut self) {
        while let Some(_) = self.next() {
        }
        unsafe {
            self.inner.set_len(0);
        }
    }
}

impl<T, const C: usize> IntoIterator for Array<T, C> {
    type Item = T;
    type IntoIter = ArrayConsumer<T, C>;

    fn into_iter(self) -> Self::IntoIter {
        ArrayConsumer {
            inner: self,
            cursor: 0,
        }
    }
}