• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // ANDROID: use std instead of core, since we're not building wiht no-std.
153     use std::mem;
154 
155     #[test]
span_simple()156     fn span_simple() {
157         #[repr(C)]
158         struct Foo {
159             a: u32,
160             b: [u8; 2],
161             c: i64,
162         }
163 
164         assert_eq!(span_of!(Foo, a), 0..4);
165         assert_eq!(span_of!(Foo, b), 4..6);
166         assert_eq!(span_of!(Foo, c), 8..8 + 8);
167     }
168 
169     #[test]
170     #[cfg_attr(miri, ignore)] // this creates unaligned references
span_simple_packed()171     fn span_simple_packed() {
172         #[repr(C, packed)]
173         struct Foo {
174             a: u32,
175             b: [u8; 2],
176             c: i64,
177         }
178 
179         assert_eq!(span_of!(Foo, a), 0..4);
180         assert_eq!(span_of!(Foo, b), 4..6);
181         assert_eq!(span_of!(Foo, c), 6..6 + 8);
182     }
183 
184     #[test]
span_forms()185     fn span_forms() {
186         #[repr(C)]
187         struct Florp {
188             a: u32,
189         }
190 
191         #[repr(C)]
192         struct Blarg {
193             x: u64,
194             y: [u8; 56],
195             z: Florp,
196             egg: [[u8; 4]; 5],
197         }
198 
199         // Love me some brute force
200         assert_eq!(0..8, span_of!(Blarg, x));
201         assert_eq!(64..68, span_of!(Blarg, z));
202         assert_eq!(68..mem::size_of::<Blarg>(), span_of!(Blarg, egg));
203 
204         assert_eq!(8..64, span_of!(Blarg, y..z));
205         assert_eq!(0..64, span_of!(Blarg, x..=y));
206     }
207 
208     #[test]
ig_test()209     fn ig_test() {
210         #[repr(C)]
211         struct Member {
212             foo: u32,
213         }
214 
215         #[repr(C)]
216         struct Test {
217             x: u64,
218             y: [u8; 56],
219             z: Member,
220             egg: [[u8; 4]; 4],
221         }
222 
223         assert_eq!(span_of!(Test, ..x), 0..0);
224         assert_eq!(span_of!(Test, ..=x), 0..8);
225         assert_eq!(span_of!(Test, ..y), 0..8);
226         assert_eq!(span_of!(Test, ..=y), 0..64);
227         assert_eq!(span_of!(Test, ..z), 0..64);
228         assert_eq!(span_of!(Test, ..=z), 0..68);
229         assert_eq!(span_of!(Test, ..egg), 0..68);
230         assert_eq!(span_of!(Test, ..=egg), 0..84);
231         assert_eq!(span_of!(Test, ..), 0..mem::size_of::<Test>());
232         assert_eq!(
233             span_of!(Test, x..),
234             offset_of!(Test, x)..mem::size_of::<Test>()
235         );
236         assert_eq!(
237             span_of!(Test, y..),
238             offset_of!(Test, y)..mem::size_of::<Test>()
239         );
240 
241         assert_eq!(
242             span_of!(Test, z..),
243             offset_of!(Test, z)..mem::size_of::<Test>()
244         );
245         assert_eq!(
246             span_of!(Test, egg..),
247             offset_of!(Test, egg)..mem::size_of::<Test>()
248         );
249         assert_eq!(
250             span_of!(Test, x..y),
251             offset_of!(Test, x)..offset_of!(Test, y)
252         );
253         assert_eq!(
254             span_of!(Test, x..=y),
255             offset_of!(Test, x)..offset_of!(Test, y) + mem::size_of::<[u8; 56]>()
256         );
257     }
258 }
259