stdx/
non_empty_vec.rs

1//! See [`NonEmptyVec`].
2
3/// A [`Vec`] that is guaranteed to at least contain one element.
4pub struct NonEmptyVec<T> {
5    first: T,
6    rest: Vec<T>,
7}
8
9impl<T> NonEmptyVec<T> {
10    #[inline]
11    pub const fn new(first: T) -> Self {
12        Self { first, rest: Vec::new() }
13    }
14
15    #[inline]
16    pub fn last_mut(&mut self) -> &mut T {
17        self.rest.last_mut().unwrap_or(&mut self.first)
18    }
19
20    #[inline]
21    pub fn pop(&mut self) -> Option<T> {
22        self.rest.pop()
23    }
24
25    #[inline]
26    pub fn push(&mut self, value: T) {
27        self.rest.push(value);
28    }
29
30    #[inline]
31    pub fn len(&self) -> usize {
32        1 + self.rest.len()
33    }
34
35    #[inline]
36    pub fn into_last(mut self) -> T {
37        self.rest.pop().unwrap_or(self.first)
38    }
39}