no_std_collections/traits/slice_owner/
mod.rs

1pub unsafe trait SliceOwner {
2    type Item;
3
4    fn len(&self) -> usize;
5
6    fn as_ptr(&self) -> *const Self::Item;
7
8    #[inline]
9    fn as_mut_ptr(&mut self) -> *mut Self::Item {
10        self.as_ptr() as *mut Self::Item
11    }
12
13    #[inline]
14    fn as_slice(&self) -> &[Self::Item] {
15        unsafe { core::slice::from_raw_parts(self.as_ptr(), self.len()) }
16    }
17
18    #[inline]
19    fn as_mut_slice(&mut self) -> &mut [Self::Item] {
20        unsafe { core::slice::from_raw_parts_mut(self.as_mut_ptr(), self.len()) }
21    }
22}
23
24unsafe impl<T, const N: usize> SliceOwner for [T; N] {
25    type Item = T;
26    #[inline]
27    fn as_ptr(&self) -> *const Self::Item {
28        self as *const T
29    }
30
31    #[inline]
32    fn len(&self) -> usize {
33        N
34    }
35
36    #[inline]
37    fn as_mut_ptr(&mut self) -> *mut Self::Item {
38        self as *mut T
39    }
40
41    #[inline]
42    fn as_slice(&self) -> &[Self::Item] {
43        self.as_slice()
44    }
45
46    #[inline]
47    fn as_mut_slice(&mut self) -> &mut [Self::Item] {
48        self.as_mut_slice()
49    }
50}
51
52#[cfg(feature = "std")]
53unsafe impl<T> SliceOwner for Vec<T> {
54    type Item = T;
55    #[inline]
56    fn len(&self) -> usize {
57        self.len()
58    }
59
60    #[inline]
61    fn as_ptr(&self) -> *const Self::Item {
62        self.as_ptr()
63    }
64
65    #[inline]
66    fn as_mut_ptr(&mut self) -> *mut Self::Item {
67        self.as_mut_ptr()
68    }
69}
70
71#[cfg(feature = "std")]
72unsafe impl<T> SliceOwner for Box<[T]> {    
73    type Item = T;
74
75    #[inline]
76    fn len(&self) -> usize {
77        (&**self).len()
78    }
79
80    #[inline]
81    fn as_ptr(&self) -> *const Self::Item {
82        (&**self).as_ptr()
83    }
84
85    #[inline]
86    fn as_mut_ptr(&mut self) -> *mut Self::Item {
87        (&mut **self).as_mut_ptr()
88    }
89
90    fn as_slice(&self) -> &[Self::Item] {
91        &**self
92    }
93
94    fn as_mut_slice(&mut self) -> &mut [Self::Item] {
95        &mut **self
96    }
97}