repr_offset/struct_field_offset/
repr_offset_ext_impls.rs

1use crate::{
2    alignment::{Aligned, Unaligned},
3    ext::{ROExtAcc, ROExtOps, ROExtRawAcc, ROExtRawMutAcc, ROExtRawMutOps, ROExtRawOps},
4    FieldOffset,
5};
6
7//////////////////////////////////////////////////////////////////////////////
8
9unsafe impl<S> ROExtAcc for S {
10    #[inline(always)]
11    fn f_get<F>(&self, offset: FieldOffset<Self, F, Aligned>) -> &F {
12        unsafe { impl_fo!(fn get<S, F, Aligned>(offset, self)) }
13    }
14    #[inline(always)]
15    fn f_get_mut<F>(&mut self, offset: FieldOffset<Self, F, Aligned>) -> &mut F {
16        unsafe { impl_fo!(fn get_mut<S, F, Aligned>(offset, self)) }
17    }
18
19    #[inline(always)]
20    fn f_get_ptr<F, A>(&self, offset: FieldOffset<Self, F, A>) -> *const F {
21        unsafe { impl_fo!(fn get_ptr<S, F, A>(offset, self)) }
22    }
23
24    #[inline(always)]
25    fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<Self, F, A>) -> *mut F {
26        unsafe { impl_fo!(fn get_mut_ptr<S, F, A>(offset, self)) }
27    }
28}
29
30macro_rules! impl_ROExtOps {
31    ($A:ident) => {
32
33        unsafe impl<S> ROExtOps<$A> for S {
34            #[inline(always)]
35            fn f_replace<F>(&mut self, offset: FieldOffset<Self, F, $A>, value: F) -> F{
36                unsafe{ impl_fo!(fn replace_mut<S, F, $A>(offset, self, value)) }
37            }
38
39            #[inline(always)]
40            fn f_swap<F>(&mut self, offset: FieldOffset<Self, F, $A>, right: &mut S){
41                unsafe{ impl_fo!(fn swap_mut<S, F, $A>(offset, self, right)) }
42
43            }
44
45            #[inline(always)]
46            fn f_get_copy<F>(&self, offset: FieldOffset<Self, F, $A>) -> F
47            where
48                F: Copy
49            {
50                unsafe{ impl_fo!(fn get_copy<S, F, $A>(offset, self)) }
51            }
52        }
53    };
54}
55
56impl_ROExtOps! {Aligned}
57impl_ROExtOps! {Unaligned}
58
59//////////////////////////////////////////////////////////////////////////////
60
61macro_rules! impl_ROExtRaw {
62    ($($ptr:tt)*)=>{
63        impl_ROExtRawOps! {Aligned, [$($ptr)*]}
64        impl_ROExtRawOps! {Unaligned, [$($ptr)*]}
65
66        unsafe impl<S> ROExtRawAcc for $($ptr)* S {
67            #[inline(always)]
68            unsafe fn f_raw_get<F, A>(self, offset: FieldOffset<Self::Target, F, A>) -> *const F {
69                impl_fo!(fn raw_get<Self::Target, F, A>(offset, self))
70            }
71        }
72    }
73}
74
75macro_rules! impl_ROExtRawMut {
76    ($($ptr:tt)*)=>{
77        impl_ROExtRawMutOps! {Aligned, [$($ptr)*]}
78        impl_ROExtRawMutOps! {Unaligned, [$($ptr)*]}
79
80        unsafe impl<S> ROExtRawMutAcc for $($ptr)* S {
81            #[inline(always)]
82            unsafe fn f_raw_get_mut<F, A>(self, offset: FieldOffset<Self::Target, F, A>) -> *mut F {
83                impl_fo!(fn raw_get_mut<Self::Target, F, A>(offset, self))
84            }
85        }
86    }
87}
88
89macro_rules! impl_ROExtRawOps {
90    ($A:ident, [$($ptr:tt)*])=>{
91        unsafe impl<S> ROExtRawOps<$A> for $($ptr)* S {
92            #[inline(always)]
93            unsafe fn f_read_copy<F>(self, offset: FieldOffset<Self::Target, F, $A>) -> F
94            where
95                F: Copy
96            {
97                impl_fo!(fn read_copy<Self::Target, F, $A>(offset, self))
98            }
99
100            #[inline(always)]
101            unsafe fn f_read<F>(self, offset: FieldOffset<Self::Target, F, $A>) -> F {
102                impl_fo!(fn read<Self::Target, F, $A>(offset, self))
103            }
104        }
105    };
106}
107
108macro_rules! impl_ROExtRawMutOps {
109    ($A:ident, [$($ptr:tt)*])=>{
110        unsafe impl<S> ROExtRawMutOps<$A> for $($ptr)* S {
111            #[inline(always)]
112            unsafe fn f_write<F>(self, offset: FieldOffset<Self::Target, F, $A>, value: F) {
113                impl_fo!(fn write<Self::Target, F, $A>(offset, self, value))
114            }
115
116            #[inline(always)]
117            unsafe fn f_copy_from<F>(
118                self,
119                offset: FieldOffset<Self::Target, F, $A>,
120                source: *const Self::Target,
121            ) {
122                impl_fo!(fn copy<Self::Target, F, $A>(offset, source, self))
123            }
124
125            #[inline(always)]
126            unsafe fn f_copy_from_nonoverlapping<F>(
127                self,
128                offset: FieldOffset<Self::Target, F, $A>,
129                source: *const Self::Target,
130            ) {
131                impl_fo!(fn copy_nonoverlapping<Self::Target, F, $A>(offset, source, self))
132            }
133
134            #[inline(always)]
135            unsafe fn f_replace_raw<F>(
136                self,
137                offset: FieldOffset<Self::Target, F, $A>,
138                value: F,
139            ) -> F {
140                impl_fo!(fn replace<Self::Target, F, $A>(offset, self, value))
141            }
142
143            #[inline(always)]
144            unsafe fn f_swap_raw<F>(
145                self,
146                offset: FieldOffset<Self::Target, F, $A>,
147                right: *mut Self::Target,
148            ) {
149                impl_fo!(fn swap<Self::Target, F, $A>(offset, self, right))
150            }
151
152            #[inline(always)]
153            unsafe fn f_swap_nonoverlapping<F>(
154                self,
155                offset: FieldOffset<Self::Target, F, $A>,
156                right: *mut Self::Target
157            ) {
158                impl_fo!(fn swap_nonoverlapping<Self::Target, F, $A>(offset, self, right))
159            }
160        }
161    }
162}
163
164impl_ROExtRaw! {*const}
165impl_ROExtRaw! {*mut}
166
167impl_ROExtRawMut! {*mut}