slice_trait/
as_slice.rs

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