slice_trait/
as_slice.rs

1/// A trait for obtaining a slice `[Self::Item]`
2pub const trait AsSlice
3{
4    type Elem: Sized;
5
6    /// Yields slice from generic
7    fn as_slice(&self) -> &[Self::Elem];
8
9    /// Yields mutable slice from generic
10    fn as_mut_slice(&mut self) -> &mut [Self::Elem];
11}
12
13impl<T> const AsSlice for [T]
14{
15    type Elem = T;
16
17    fn as_slice(&self) -> &[Self::Elem]
18    {
19        self
20    }
21
22    fn as_mut_slice(&mut self) -> &mut [Self::Elem]
23    {
24        self
25    }
26}
27
28impl<T, const N: usize> const AsSlice for [T; N]
29{
30    type Elem = T;
31
32    fn as_slice(&self) -> &[Self::Elem]
33    {
34        self.as_slice()
35    }
36
37    fn as_mut_slice(&mut self) -> &mut [Self::Elem]
38    {
39        self.as_mut_slice()
40    }
41}
42
43#[cfg(feature = "alloc")]
44impl<T, A> const AsSlice for alloc::vec::Vec<T, A>
45where
46    A: core::alloc::Allocator
47{
48    type Elem = T;
49
50    fn as_slice(&self) -> &[Self::Elem]
51    {
52        self.as_slice()
53    }
54
55    fn as_mut_slice(&mut self) -> &mut [Self::Elem]
56    {
57        self.as_mut_slice()
58    }
59}
60
61#[cfg(feature = "alloc")]
62impl<T, A> const AsSlice for alloc::boxed::Box<T, A>
63where
64    A: core::alloc::Allocator,
65    T: ~const AsSlice + ?Sized
66{
67    type Elem = T::Elem;
68
69    fn as_slice(&self) -> &[Self::Elem]
70    {
71        (**self).as_slice()
72    }
73
74    fn as_mut_slice(&mut self) -> &mut [Self::Elem]
75    {
76        (**self).as_mut_slice()
77    }
78}