Skip to main content

stack_array/
references.rs

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