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
// This file is part of copy-stack-vec.
// SPDX-License-Identifier: MIT OR Apache-2.0
// Crate imports
use crate::vec::CopyStackVec;
impl<T: Copy, const N: usize> CopyStackVec<T, N> {
/// Returns the initialized prefix as a shared slice (`&self.buf[..len]`).
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.buf[..self.len]
}
/// Returns the initialized prefix as a mutable slice (`&mut self.buf[..len]`).
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T] {
let len = self.len;
&mut self.buf[..len]
}
}
impl<T: Copy + Default, const N: usize> CopyStackVec<T, N> {
/// Constructs from at most `N` elements of `src`, truncating if necessary.
#[inline]
pub fn from_slice_truncated(src: &[T]) -> Self {
let mut v = Self::default();
let _ = v.extend_from_slice_truncated(src);
v
}
/// Constructs from at most `N` elements of `src`, truncating if necessary.
///
/// Convenience wrapper over [`from_slice_truncated`] for arrays.
#[inline]
pub fn from_array_truncated<const M: usize>(src: &[T; M]) -> Self {
Self::from_slice_truncated(&src[..])
}
}