Skip to main content

libutils_array/
references.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> SUPER
6use super::Array;
7
8//> HEAD -> CORE
9use core::{
10    slice::{
11        from_raw_parts as fat,
12        from_raw_parts_mut as mutfat
13    },
14    borrow::{
15        Borrow,
16        BorrowMut
17    },
18    ops::{
19        Deref,
20        DerefMut,
21        DerefPure
22    }
23};
24
25
26//^
27//^ DEREF
28//^
29
30//> DEREF -> SLICE
31const impl<Type, const N: usize> Deref for Array<Type, N> {
32    type Target = [Type];
33    #[inline]
34    fn deref(&self) -> &Self::Target {return unsafe {fat(self.data.as_ptr() as *const Type, self.length)}}
35}
36
37//> DEREF -> MUTABLE SLICE
38const impl<Type, const N: usize> DerefMut for Array<Type, N> {
39    #[inline]
40    fn deref_mut(&mut self) -> &mut Self::Target {return unsafe {mutfat(self.pointer().as_ptr(), self.length)}}
41}
42
43//> DEREF -> PURE
44unsafe impl<Type, const N: usize> DerefPure for Array<Type, N> {}
45
46
47//^
48//^ REFERENCES
49//^
50
51//> REFERENCES -> SLICE
52const impl<Type, const N: usize> AsRef<[Type]> for Array<Type, N> {
53    #[inline]
54    fn as_ref(&self) -> &[Type] {return self.deref()}
55}
56
57//> REFERENCES -> MUTABLE SLICE
58const impl<Type, const N: usize> AsMut<[Type]> for Array<Type, N> {
59    #[inline]
60    fn as_mut(&mut self) -> &mut [Type] {return self.deref_mut()}
61}
62
63
64//^
65//^ BORROW
66//^
67
68//> BORROW -> CONSTANT
69const impl<Type, const N: usize> Borrow<[Type]> for Array<Type, N> {
70    #[inline]
71    fn borrow(&self) -> &[Type] {self.as_ref()}
72}
73
74//> BORROW -> MUTABLE
75const impl<Type, const N: usize> BorrowMut<[Type]> for Array<Type, N> {
76    #[inline]
77    fn borrow_mut(&mut self) -> &mut [Type] {self.as_mut()}
78}