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    }
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 {return unsafe {fat(self.data.as_ptr().cast(), self.length)}}
33}
34
35//> DEREF -> MUTABLE
36const impl<Type, const N: usize> DerefMut for Array<Type, N> {
37    fn deref_mut(&mut self) -> &mut Self::Target {return unsafe {mutfat(self.data.as_mut_ptr().cast(), self.length)}}
38}
39
40
41//^
42//^ REFERENCES
43//^
44
45//> REFERENCES -> SLICE
46const impl<Type, const N: usize> AsRef<[Type]> for Array<Type, N> {
47    fn as_ref(&self) -> &[Type] {return self.deref()}
48}
49
50//> REFERENCES -> MUTABLE SLICE
51const impl<Type, const N: usize> AsMut<[Type]> for Array<Type, N> {
52    fn as_mut(&mut self) -> &mut [Type] {return self.deref_mut()}
53}
54
55
56//^
57//^ BORROW
58//^
59
60//> BORROW -> CONSTANT
61const impl<Type, const N: usize> Borrow<[Type]> for Array<Type, N> {
62    fn borrow(&self) -> &[Type] {self.as_ref()}
63}
64
65//> BORROW -> MUTABLE
66const impl<Type, const N: usize> BorrowMut<[Type]> for Array<Type, N> {
67    fn borrow_mut(&mut self) -> &mut [Type] {self.as_mut()}
68}