copy_stack_vec/vec/array/
slice.rs

1// This file is part of copy-stack-vec.
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4// Crate imports
5use crate::vec::CopyStackVec;
6
7impl<T: Copy, const N: usize> CopyStackVec<T, N> {
8    /// Returns the initialized prefix as a shared slice (`&self.buf[..len]`).
9    #[inline]
10    pub fn as_slice(&self) -> &[T] {
11        &self.buf[..self.len]
12    }
13
14    /// Returns the initialized prefix as a mutable slice (`&mut self.buf[..len]`).
15    #[inline]
16    pub fn as_mut_slice(&mut self) -> &mut [T] {
17        let len = self.len;
18        &mut self.buf[..len]
19    }
20}
21
22impl<T: Copy + Default, const N: usize> CopyStackVec<T, N> {
23    /// Constructs from at most `N` elements of `src`, truncating if necessary.
24    #[inline]
25    pub fn from_slice_truncated(src: &[T]) -> Self {
26        let mut v = Self::default();
27        let _ = v.extend_from_slice_truncated(src);
28        v
29    }
30
31    /// Constructs from at most `N` elements of `src`, truncating if necessary.
32    ///
33    /// Convenience wrapper over [`from_slice_truncated`] for arrays.
34    #[inline]
35    pub fn from_array_truncated<const M: usize>(src: &[T; M]) -> Self {
36        Self::from_slice_truncated(&src[..])
37    }
38}