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
#![no_std]
#![cfg_attr(any(feature = "alloc", feature = "std"), feature(allocator_api))]

use core::borrow::{Borrow, BorrowMut};
use core::convert::{AsRef, AsMut};
use core::cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use core::mem::MaybeUninit;
use core::num::NonZeroUsize;
use core::ops::{Deref, DerefMut};
use core::slice;

/// A slice guaranteed to contain at least `N` elements.
#[repr(C)]
pub struct SliceN<T, const N: usize> {
    pub arr: [T; N],
    pub slice: [T],
}

/// A slice guaranteed to contain at least one element.
pub type Slice1<T> = SliceN<T, 1>;

impl<T, const N: usize> SliceN<T, N> {
    /// Converts a regular slice into a `SliceN` if it is long enough.
    pub fn from_slice(s: &[T]) -> Option<&Self> {
        if s.len() >= N {
            Some(unsafe { Self::from_slice_unchecked(s) })
        } else {
            None
        }
    }

    /// Converts a regular mutable slice into a `SliceN` if it is long enough.
    pub fn from_slice_mut(s: &mut [T]) -> Option<&mut Self> {
        if s.len() >= N {
            Some(unsafe { Self::from_slice_unchecked_mut(s) })
        } else {
            None
        }
    }

    /// Converts a regular slice into a `SliceN` without checking that its length is sufficient.
    pub unsafe fn from_slice_unchecked(s: &[T]) -> &Self {
        &*(core::ptr::slice_from_raw_parts(s.as_ptr(), s.len() - N) as *const Self)
    }

    /// Converts a regular mutable slice into a `SliceN` without checking that its length is
    /// sufficient.
    pub unsafe fn from_slice_unchecked_mut(s: &mut[T]) -> &mut Self {
        &mut *(core::ptr::slice_from_raw_parts_mut(s.as_mut_ptr(), s.len() - N) as *mut Self)
    }

    pub fn as_maybe_uninit(&self) -> &SliceN<MaybeUninit<T>, N> {
        let ptr = self.as_ptr().cast::<MaybeUninit<T>>();
        unsafe { from_raw_parts_unchecked(ptr, self.len()) }
    }

    pub fn as_maybe_uninit_mut(&mut self) -> &mut SliceN<MaybeUninit<T>, N> {
        let ptr = self.as_mut_ptr().cast::<MaybeUninit<T>>();
        unsafe { from_raw_parts_unchecked_mut(ptr, self.len()) }
    }
}

// Rust does not yet support implementing these for all sizes greater than zero.
impl<T> SliceN<T, 1> {
    /// Returns the number of elements in the slice as a `NonZeroUsize`.
    pub fn len(&self) -> NonZeroUsize {
        unsafe { NonZeroUsize::new_unchecked(self.as_ref().len()) }
    }

    /// Returns the number of elements in the slice as a `usize`.
    pub fn len_(&self) -> usize {
        self.as_ref().len()
    }

    /// Returns a reference to the first element in the slice.
    pub fn first(&self) -> &T {
        &self.arr[0]
    }

    /// Returns a mutable reference to the first element in the slice.
    pub fn first_mut(&mut self) -> &mut T {
        &mut self.arr[0]
    }

    /// Returns a reference to the last element in the slice.
    pub fn last(&self) -> &T {
        self.as_ref().last().unwrap()
    }

    /// Returns a mutable reference to the last element in the slice.
    pub fn last_mut(&mut self) -> &mut T {
        self.as_mut().last_mut().unwrap()
    }

    /// Returns the first and all the rest of the elements of the slice.
    pub fn split_first(&self) -> (&T, &[T]) {
        self.as_ref().split_first().unwrap()
    }

    /// Returns the first and all the rest of the elements of the slice.
    pub fn split_first_mut(&mut self) -> (&mut T, &mut [T]) {
        self.as_mut().split_first_mut().unwrap()
    }

    /// Returns the last and all the rest of the elements of the slice.
    pub fn split_last(&self) -> (&T, &[T]) {
        self.as_ref().split_last().unwrap()
    }

    /// Returns the last and all the rest of the elements of the slice.
    pub fn split_last_mut(&mut self) -> (&mut T, &mut [T]) {
        self.as_mut().split_last_mut().unwrap()
    }
}

/// Forms a `SliceN` from a pointer and a length, without checking the length.
///
/// In addition to the length being too low, this is just as unsafe as
/// `core::slice::from_raw_parts`.
pub unsafe fn from_raw_parts_unchecked<'a, T, const N: usize>(data: *const T, len: usize) -> &'a SliceN<T, N> {
    SliceN::from_slice_unchecked(slice::from_raw_parts(data, len))
}

/// Forms a `SliceN` from a pointer and a length if it is long enough.
///
/// In addition to the length being too low, this is just as unsafe as
/// `core::slice::from_raw_parts`.
pub unsafe fn from_raw_parts<'a, T, const N: usize>(data: *const T, len: usize) -> Option<&'a SliceN<T, N>> {
    SliceN::from_slice(slice::from_raw_parts(data, len))
}

/// Forms a `SliceN` from a mutable pointer and a length, without checking the length.
///
/// In addition to the length being too low, this is just as unsafe as
/// `core::slice::from_raw_parts`.
pub unsafe fn from_raw_parts_unchecked_mut<'a, T, const N: usize>(data: *mut T, len: usize) -> &'a mut SliceN<T, N> {
    SliceN::from_slice_unchecked_mut(slice::from_raw_parts_mut(data, len))
}

/// Forms a `SliceN` from a mutable pointer and a length if it is long enough.
///
/// In addition to the length being too low, this is just as unsafe as
/// `core::slice::from_raw_parts`.
pub unsafe fn from_raw_parts_mut<'a, T, const N: usize>(data: *mut T, len: usize) -> Option<&'a mut SliceN<T, N>> {
    SliceN::from_slice_mut(slice::from_raw_parts_mut(data, len))
}

impl<T, const N: usize> AsRef<[T]> for SliceN<T, N> {
    fn as_ref(&self) -> &[T] {
        unsafe { slice::from_raw_parts((self as *const SliceN<T, N>) as *const T, N + self.slice.len()) }
    }
}

impl<T, const N: usize> AsMut<[T]> for SliceN<T, N> {
    fn as_mut(&mut self) -> &mut [T] {
        unsafe { slice::from_raw_parts_mut((self as *mut SliceN<T, N>) as *mut T, N + self.slice.len()) }
    }
}

impl<T, const N: usize> Borrow<[T]> for SliceN<T, N> {
    fn borrow(&self) -> &[T] {
        self.as_ref()
    }
}

impl<T, const N: usize> BorrowMut<[T]> for SliceN<T, N> {
    fn borrow_mut(&mut self) -> &mut [T] {
        self.as_mut()
    }
}

impl<T, const N: usize> Deref for SliceN<T, N> {
    type Target = [T];

    fn deref(&self) -> &[T] {
        self.as_ref()
    }
}

impl<T, const N: usize> DerefMut for SliceN<T, N> {
    fn deref_mut(&mut self) -> &mut [T] {
        self.as_mut()
    }
}

impl<T: Debug, const N: usize> Debug for SliceN<T, N> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        self.as_ref().fmt(f)
    }
}

impl<T, S, const N: usize, const M: usize> PartialEq<SliceN<S, M>> for SliceN<T, N> where T: PartialEq<S> {
    fn eq(&self, other: &SliceN<S, M>) -> bool {
        self.as_ref().eq(other.as_ref())
    }
}

impl<A, B, const N: usize, const M: usize> PartialEq<[A; M]> for SliceN<B, N> where
B: PartialEq<A> {
    fn eq(&self, other: &[A; M]) -> bool {
        self.as_ref().eq(other)
    }
}

impl<'a, A, B, const N: usize, const M: usize> PartialEq<[A; M]> for &'a SliceN<B, N> where
B: PartialEq<A> {
    fn eq(&self, other: &[A; M]) -> bool {
        self.as_ref().eq(other)
    }
}

impl<'a, A, B, const N: usize, const M: usize> PartialEq<[A; M]> for &'a mut SliceN<B, N> where
B: PartialEq<A> {
    fn eq(&self, other: &[A; M]) -> bool {
        self.as_ref().eq(other)
    }
}

#[cfg(any(feature = "alloc", feature = "std"))]
mod alloc_impls {
    use maybe_std::{
        alloc::Allocator,
        vec::Vec,
    };

    use super::*;

    impl<T, U, A, const N: usize> PartialEq<Vec<U, A>> for SliceN<T, N> where
    T: PartialEq<U>, A: Allocator {
        fn eq(&self, other: &Vec<U, A>) -> bool {
            self.as_ref().eq(other)
        }
    }

    impl<'a, T, U, A, const N: usize> PartialEq<Vec<U, A>> for &'a SliceN<T, N> where
    T: PartialEq<U>, A: Allocator {
        fn eq(&self, other: &Vec<U, A>) -> bool {
            self.as_ref().eq(other)
        }
    }

    impl<'a, T, U, A, const N: usize> PartialEq<Vec<U, A>> for &'a mut SliceN<T, N> where
    T: PartialEq<U>, A: Allocator {
        fn eq(&self, other: &Vec<U, A>) -> bool {
            self.as_ref().eq(other)
        }
    }
}

impl<T, const N: usize> Eq for SliceN<T, N> where T: Eq {}

impl<T, const N: usize> PartialOrd for SliceN<T, N> where T: PartialOrd {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.as_ref().partial_cmp(other.as_ref())
    }

    fn lt(&self, other: &Self) -> bool {
        self.as_ref().lt(other.as_ref())
    }

    fn le(&self, other: &Self) -> bool {
        self.as_ref().le(other.as_ref())
    }

    fn gt(&self, other: &Self) -> bool {
        self.as_ref().gt(other.as_ref())
    }

    fn ge(&self, other: &Self) -> bool {
        self.as_ref().ge(other.as_ref())
    }
}

impl<T, const N: usize> Ord for SliceN<T, N> where T: Ord {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_ref().cmp(other)
    }
}

impl<T, const N: usize> Hash for SliceN<T, N> where T: Hash {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.as_ref().hash(state)
    }
}