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

use {
    std::{
        convert::TryFrom,
        num::NonZeroUsize,
        ops::{
            Deref,
            Index,
            IndexMut,
        },
        slice,
    },
};

#[derive(Debug, Clone)]
pub struct NotEnoughElementsError;

/// a mostly costless wrapping of a vec, ensuring there's always at least one element.
///
/// Follow the semantics of Vec (differing methods have a different name).
///
#[derive(Debug, Clone)]
pub struct NonEmptyVec<T> {
    vec: Vec<T>,
}

impl<T> NonEmptyVec<T> {

    #[inline]
    pub fn len(&self) -> NonZeroUsize {
        unsafe {
            NonZeroUsize::new_unchecked(self.vec.len())
        }
    }

    #[inline]
    pub fn has_len(&self, len: usize) -> bool {
        self.vec.len() == len
    }

    #[inline]
    pub fn first(&self) -> &T {
        unsafe {
            self.vec.get_unchecked(0)
        }
    }

    #[inline]
    pub fn first_mut(&mut self) -> &mut T {
        unsafe {
            self.vec.get_unchecked_mut(0)
        }
    }

    #[inline]
    pub fn last(&self) -> &T {
        unsafe {
            self.vec.get_unchecked(self.vec.len() - 1)
        }
    }

    #[inline]
    pub fn last_mut(&mut self) -> &mut T {
        let idx = self.vec.len() - 1;
        unsafe {
            self.vec.get_unchecked_mut(idx)
        }
    }

    /// take the first item, discard the rest
    #[inline]
    pub fn take(mut self) -> T {
        self.vec.drain(..).next().unwrap()
    }

    #[inline]
    pub fn push(&mut self, value: T) {
        self.vec.push(value);
    }

    #[inline]
    pub fn insert(&mut self, insertion_idx: usize, value: T) {
        self.vec.insert(insertion_idx, value);
    }

    /// Removes the last element from a vector and returns it, or [`None`] if it
    /// contains only one element
    #[inline]
    pub fn pop(&mut self) -> Option<T> {
        if self.vec.len() == 1 {
            None
        } else {
            self.vec.pop()
        }
    }

    #[inline]
    pub fn as_slice(&self) -> &[T] {
        &self.vec
    }

    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.vec
    }

    #[inline]
    pub fn remove(&mut self, idx: usize) -> Result<T, NotEnoughElementsError> {
        if self.vec.len() == 1 {
            Err(NotEnoughElementsError)
        } else {
            Ok(self.vec.remove(idx))
        }
    }

    #[inline]
    pub fn swap_remove(&mut self, idx: usize) -> Result<T, NotEnoughElementsError> {
        if self.vec.len() == 1 {
            Err(NotEnoughElementsError)
        } else {
            Ok(self.vec.swap_remove(idx))
        }
    }

}

impl<T> TryFrom<Vec<T>> for NonEmptyVec<T> {
    type Error = NotEnoughElementsError;
    #[inline]
    fn try_from(vec: Vec<T>) -> Result<Self, Self::Error> {
        if vec.is_empty() {
            Err(NotEnoughElementsError)
        } else {
            Ok(Self {
                vec,
            })
        }
    }
}

impl<T> From<T> for NonEmptyVec<T> {
    #[inline]
    fn from(value: T) -> Self {
        Self {
            vec: vec![value],
        }
    }
}

impl<T> Deref for NonEmptyVec<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        self.vec.deref()
    }
}

impl<T, I: slice::SliceIndex<[T]>> Index<I> for NonEmptyVec<T> {
    type Output = I::Output;
    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        Index::index(self.as_slice(), index)
    }
}

impl<T, I: slice::SliceIndex<[T]>> IndexMut<I> for NonEmptyVec<T> {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        IndexMut::index_mut(self.as_mut_slice(), index)
    }
}

impl<'a, T> IntoIterator for &'a mut NonEmptyVec<T> {
    type Item = &'a mut T;
    type IntoIter = slice::IterMut<'a, T>;
    #[inline]
    fn into_iter(self) -> slice::IterMut<'a, T> {
        self.vec.iter_mut()
    }
}

impl<'a, T> IntoIterator for &'a NonEmptyVec<T> {
    type Item = &'a T;
    type IntoIter = slice::Iter<'a, T>;
    #[inline]
    fn into_iter(self) -> slice::Iter<'a, T> {
        self.vec.iter()
    }
}

#[cfg(test)]
mod non_empty_vec_tests {

    use {
        super::*,
        std::convert::TryInto,
    };

    #[test]
    fn test_pop_push() {
        let mut vec: NonEmptyVec<usize> = vec![1, 2].try_into().unwrap();
        assert_eq!(vec.pop(), Some(2));
        assert_eq!(vec.pop(), None);
        assert_eq!(vec[0], 1);
        vec[0] = 0;
        assert_eq!(*vec.first(), 0);
        let first: &mut usize = vec.first_mut();
        *first = 4;
        assert_eq!(vec[0], 4);
    }
}