1 // Copyright (c) 2017 Gilad Naaman
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 /// Reexport for `local_inner_macros`; see
22 /// <https://doc.rust-lang.org/edition-guide/rust-2018/macros/macro-changes.html#macros-using-local_inner_macros>.
23 #[doc(hidden)]
24 #[macro_export]
25 macro_rules! _memoffset__compile_error {
26     ($($inner:tt)*) => {
27         compile_error! { $($inner)* }
28     }
29 }
30 
31 /// Produces a range instance representing the sub-slice containing the specified member.
32 ///
33 /// This macro provides 2 forms of differing functionalities.
34 ///
35 /// The first form is identical to the appearance of the `offset_of!` macro.
36 ///
37 /// ```ignore
38 /// span_of!(Struct, member)
39 /// ```
40 ///
41 /// The second form of `span_of!` returns a sub-slice which starts at one field, and ends at another.
42 /// The general pattern of this form is:
43 ///
44 /// ```ignore
45 /// // Exclusive
46 /// span_of!(Struct, member_a .. member_b)
47 /// // Inclusive
48 /// span_of!(Struct, member_a ..= member_b)
49 ///
50 /// // Open-ended ranges
51 /// span_of!(Struct, .. end)
52 /// span_of!(Struct, start ..)
53 /// ```
54 ///
55 /// *Note*:
56 /// This macro uses recursion in order to resolve the range expressions, so there is a limit to
57 /// the complexity of the expression.
58 /// In order to raise the limit, the compiler's recursion limit should be lifted.
59 ///
60 /// ## Examples
61 /// ```
62 /// #[macro_use]
63 /// extern crate memoffset;
64 ///
65 /// #[repr(C)]
66 /// struct Florp {
67 ///     a: u32
68 /// }
69 ///
70 /// #[repr(C)]
71 /// struct Blarg {
72 ///     x: [u32; 2],
73 ///     y: [u8; 56],
74 ///     z: Florp,
75 ///     egg: [[u8; 4]; 4]
76 /// }
77 ///
78 /// fn main() {
79 ///     assert_eq!(0..84,  span_of!(Blarg, ..));
80 ///     assert_eq!(0..8,   span_of!(Blarg, .. y));
81 ///     assert_eq!(0..64,  span_of!(Blarg, ..= y));
82 ///     assert_eq!(0..8,   span_of!(Blarg, x));
83 ///     assert_eq!(8..84,  span_of!(Blarg, y ..));
84 ///     assert_eq!(0..8,   span_of!(Blarg, x .. y));
85 ///     assert_eq!(0..64,  span_of!(Blarg, x ..= y));
86 /// }
87 /// ```
88 #[macro_export(local_inner_macros)]
89 macro_rules! span_of {
90     (@helper  $root:ident, [] ..=) => {
91         _memoffset__compile_error!("Expected a range, found '..='")
92     };
93     (@helper $root:ident, [] ..) => {
94         _memoffset__compile_error!("Expected a range, found '..'")
95     };
96     // No explicit begin for range.
97     (@helper $root:ident, $parent:path, [] ..) => {{
98         ($root as usize,
99          $root as usize + $crate::__priv::size_of_pointee($root))
100     }};
101     (@helper $root:ident, $parent:path, [] ..= $end:tt) => {{
102         let end = raw_field!($root, $parent, $end);
103         ($root as usize, end as usize + $crate::__priv::size_of_pointee(end))
104     }};
105     (@helper $root:ident, $parent:path, [] .. $end:tt) => {{
106         ($root as usize, raw_field!($root, $parent, $end) as usize)
107     }};
108     // Explicit begin and end for range.
109     (@helper $root:ident, $parent:path, # $begin:tt [] ..= $end:tt) => {{
110         let begin = raw_field!($root, $parent, $begin);
111         let end = raw_field!($root, $parent, $end);
112         (begin as usize, end as usize + $crate::__priv::size_of_pointee(end))
113     }};
114     (@helper $root:ident, $parent:path, # $begin:tt [] .. $end:tt) => {{
115         (raw_field!($root, $parent, $begin) as usize,
116          raw_field!($root, $parent, $end) as usize)
117     }};
118     // No explicit end for range.
119     (@helper $root:ident, $parent:path, # $begin:tt [] ..) => {{
120         (raw_field!($root, $parent, $begin) as usize,
121          $root as usize + $crate::__priv::size_of_pointee($root))
122     }};
123     (@helper $root:ident, $parent:path, # $begin:tt [] ..=) => {{
124         _memoffset__compile_error!(
125             "Found inclusive range to the end of a struct. Did you mean '..' instead of '..='?")
126     }};
127     // Just one field.
128     (@helper $root:ident, $parent:path, # $field:tt []) => {{
129         let field = raw_field!($root, $parent, $field);
130         (field as usize, field as usize + $crate::__priv::size_of_pointee(field))
131     }};
132     // Parsing.
133     (@helper $root:ident, $parent:path, $(# $begin:tt)+ [] $tt:tt $($rest:tt)*) => {{
134         span_of!(@helper $root, $parent, $(#$begin)* #$tt [] $($rest)*)
135     }};
136     (@helper $root:ident, $parent:path, [] $tt:tt $($rest:tt)*) => {{
137         span_of!(@helper $root, $parent, #$tt [] $($rest)*)
138     }};
139 
140     // Entry point.
141     ($sty:path, $($exp:tt)+) => ({
142         // Get a base pointer.
143         _memoffset__let_base_ptr!(root, $sty);
144         let base = root as usize;
145         let (begin, end) = span_of!(@helper root, $sty, [] $($exp)*);
146         begin-base..end-base
147     });
148 }
149 
150 #[cfg(test)]
151 mod tests {
152     use core::mem;
153 
154     #[test]
155     fn span_simple() {
156         #[repr(C)]
157         struct Foo {
158             a: u32,
159             b: [u8; 2],
160             c: i64,
161         }
162 
163         assert_eq!(span_of!(Foo, a), 0..4);
164         assert_eq!(span_of!(Foo, b), 4..6);
165         assert_eq!(span_of!(Foo, c), 8..8 + 8);
166     }
167 
168     #[test]
169     #[cfg_attr(miri, ignore)] // this creates unaligned references
170     fn span_simple_packed() {
171         #[repr(C, packed)]
172         struct Foo {
173             a: u32,
174             b: [u8; 2],
175             c: i64,
176         }
177 
178         assert_eq!(span_of!(Foo, a), 0..4);
179         assert_eq!(span_of!(Foo, b), 4..6);
180         assert_eq!(span_of!(Foo, c), 6..6 + 8);
181     }
182 
183     #[test]
184     fn span_forms() {
185         #[repr(C)]
186         struct Florp {
187             a: u32,
188         }
189 
190         #[repr(C)]
191         struct Blarg {
192             x: u64,
193             y: [u8; 56],
194             z: Florp,
195             egg: [[u8; 4]; 5],
196         }
197 
198         // Love me some brute force
199         assert_eq!(0..8, span_of!(Blarg, x));
200         assert_eq!(64..68, span_of!(Blarg, z));
201         assert_eq!(68..mem::size_of::<Blarg>(), span_of!(Blarg, egg));
202 
203         assert_eq!(8..64, span_of!(Blarg, y..z));
204         assert_eq!(0..64, span_of!(Blarg, x..=y));
205     }
206 
207     #[test]
208     fn ig_test() {
209         #[repr(C)]
210         struct Member {
211             foo: u32,
212         }
213 
214         #[repr(C)]
215         struct Test {
216             x: u64,
217             y: [u8; 56],
218             z: Member,
219             egg: [[u8; 4]; 4],
220         }
221 
222         assert_eq!(span_of!(Test, ..x), 0..0);
223         assert_eq!(span_of!(Test, ..=x), 0..8);
224         assert_eq!(span_of!(Test, ..y), 0..8);
225         assert_eq!(span_of!(Test, ..=y), 0..64);
226         assert_eq!(span_of!(Test, ..z), 0..64);
227         assert_eq!(span_of!(Test, ..=z), 0..68);
228         assert_eq!(span_of!(Test, ..egg), 0..68);
229         assert_eq!(span_of!(Test, ..=egg), 0..84);
230         assert_eq!(span_of!(Test, ..), 0..mem::size_of::<Test>());
231         assert_eq!(
232             span_of!(Test, x..),
233             offset_of!(Test, x)..mem::size_of::<Test>()
234         );
235         assert_eq!(
236             span_of!(Test, y..),
237             offset_of!(Test, y)..mem::size_of::<Test>()
238         );
239 
240         assert_eq!(
241             span_of!(Test, z..),
242             offset_of!(Test, z)..mem::size_of::<Test>()
243         );
244         assert_eq!(
245             span_of!(Test, egg..),
246             offset_of!(Test, egg)..mem::size_of::<Test>()
247         );
248         assert_eq!(
249             span_of!(Test, x..y),
250             offset_of!(Test, x)..offset_of!(Test, y)
251         );
252         assert_eq!(
253             span_of!(Test, x..=y),
254             offset_of!(Test, x)..offset_of!(Test, y) + mem::size_of::<[u8; 56]>()
255         );
256     }
257 }
258