Debian Codegraph @

src/offset_of.rs

raw symbols SCIP
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 /// Macro to create a local `base_ptr` raw pointer of the given type, avoiding UB as
22 /// much as is possible currently.
23 #[cfg(maybe_uninit)]
24 #[macro_export]
25 #[doc(hidden)]
26 macro_rules! _memoffset__let_base_ptrrust-analyzer cargo memoffset 0.8.0 offset_of/_memoffset__let_base_ptr! {
27 ($name:ident, $type:ty) => {
28 // No UB here, and the pointer does not dangle, either.
29 // But we have to make sure that `uninit` lives long enough,
30 // so it has to be in the same scope as `$name`. That's why
31 // `let_base_ptr` declares a variable (several, actually)
32 // instead of returning one.
33 let uninit = $crate::__priv::mem::MaybeUninit::<$type>::uninit();
34 let $name: *const $type = uninit.as_ptr();
35 };
36 }
37 #[cfg(not(maybe_uninit))]
38 #[macro_export]
39 #[doc(hidden)]
40 macro_rules! _memoffset__let_base_ptr {
41 ($name:ident, $type:ty) => {
42 // No UB right here, but we will later dereference this pointer to
43 // offset into a field, and that is UB because the pointer is dangling.
44 let $name = $crate::__priv::mem::align_of::<$type>() as *const $type;
45 };
46 }
47
48 /// Macro to compute the distance between two pointers.
49 #[cfg(any(feature = "unstable_const", stable_const))]
50 #[macro_export]
51 #[doc(hidden)]
52 macro_rules! _memoffset_offset_from_unsaferust-analyzer cargo memoffset 0.8.0 offset_of/_memoffset_offset_from_unsafe! {
53 ($field:expr, $base:expr) => {{
54 let field = $field; // evaluate $field outside the `unsafe` block
55 let base = $base; // evaluate $base outside the `unsafe` block
56 // Compute offset, with unstable `offset_from` for const-compatibility.
57 // (Requires the pointers to not dangle, but we already need that for `raw_field!` anyway.)
58 unsafe { (field as *const u8).offset_from(base as *const u8) as usize }
59 }};
60 }
61 #[cfg(not(any(feature = "unstable_const", stable_const)))]
62 #[macro_export]
63 #[doc(hidden)]
64 macro_rules! _memoffset_offset_from_unsafe {
65 ($field:expr, $base:expr) => {
66 // Compute offset.
67 ($field as usize) - ($base as usize)
68 };
69 }
70
71 /// Calculates the offset of the specified field from the start of the named struct.
72 ///
73 /// ## Examples
74 /// ```
75 /// use memoffset::offset_of;
76 ///
77 /// #[repr(C, packed)]
78 /// struct Foo {
79 /// a: u32,
80 /// b: u64,
81 /// c: [u8; 5]
82 /// }
83 ///
84 /// fn main() {
85 /// assert_eq!(offset_of!(Foo, a), 0);
86 /// assert_eq!(offset_of!(Foo, b), 4);
87 /// }
88 /// ```
89 ///
90 /// ## Notes
91 /// Rust's ABI is unstable, and [type layout can be changed with each
92 /// compilation](https://doc.rust-lang.org/reference/type-layout.html).
93 ///
94 /// Using `offset_of!` with a `repr(Rust)` struct will return the correct offset of the
95 /// specified `field` for a particular compilation, but the exact value may change
96 /// based on the compiler version, concrete struct type, time of day, or rustc's mood.
97 ///
98 /// As a result, the value should not be retained and used between different compilations.
99 #[macro_export(local_inner_macros)]
100 macro_rules! offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of! {
101 ($parent:path, $field:tt) => {{
102 // Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
103 _memoffset__let_base_ptr!(base_ptr, $parent);
104 // Get field pointer.
105 let field_ptr = raw_field!(base_ptr, $parent, $field);
106 // Compute offset.
107 _memoffset_offset_from_unsafe!(field_ptr, base_ptr)
108 }};
109 }
110
111 /// Calculates the offset of the specified field from the start of the tuple.
112 ///
113 /// ## Examples
114 /// ```
115 /// use memoffset::offset_of_tuple;
116 ///
117 /// fn main() {
118 /// assert!(offset_of_tuple!((u8, u32), 1) >= 0, "Tuples do not have a defined layout");
119 /// }
120 /// ```
121 #[cfg(tuple_ty)]
122 #[macro_export(local_inner_macros)]
123 macro_rules! offset_of_tuplerust-analyzer cargo memoffset 0.8.0 offset_of/offset_of_tuple! {
124 ($parent:ty, $field:tt) => {{
125 // Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
126 _memoffset__let_base_ptr!(base_ptr, $parent);
127 // Get field pointer.
128 let field_ptr = raw_field_tuple!(base_ptr, $parent, $field);
129 // Compute offset.
130 _memoffset_offset_from_unsafe!(field_ptr, base_ptr)
131 }};
132 }
133
134 /// Calculates the offset of the specified union member from the start of the union.
135 ///
136 /// ## Examples
137 /// ```
138 /// use memoffset::offset_of_union;
139 ///
140 /// #[repr(C, packed)]
141 /// union Foo {
142 /// foo32: i32,
143 /// foo64: i64,
144 /// }
145 ///
146 /// fn main() {
147 /// assert!(offset_of_union!(Foo, foo64) == 0);
148 /// }
149 /// ```
150 ///
151 /// ## Note
152 /// Due to macro_rules limitations, this macro will accept structs with a single field as well as unions.
153 /// This is not a stable guarantee, and future versions of this crate might fail
154 /// on any use of this macro with a struct, without a semver bump.
155 #[macro_export(local_inner_macros)]
156 macro_rules! offset_of_unionrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of_union! {
157 ($parent:path, $field:tt) => {{
158 // Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
159 _memoffset__let_base_ptr!(base_ptr, $parent);
160 // Get field pointer.
161 let field_ptr = raw_field_union!(base_ptr, $parent, $field);
162 // Compute offset.
163 _memoffset_offset_from_unsafe!(field_ptr, base_ptr)
164 }};
165 }
166
167 #[cfg(test)]
168 mod testsrust-analyzer cargo memoffset 0.8.0 offset_of/tests/ {
169 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
170 fn offset_simplerust-analyzer cargo memoffset 0.8.0 offset_of/tests/offset_simple().() {
171 #[repr(C)]
172 struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
173 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
174 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
175 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
176 }
177
178 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.), 0);
179 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.), 4);
180 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.), 8);
181 }
182
183 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
184 #[cfg_attr(miri, ignore)] // this creates unaligned references
185 fn offset_simple_packedrust-analyzer cargo memoffset 0.8.0 offset_of/tests/offset_simple_packed().() {
186 #[repr(C, packed)]
187 struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
188 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
189 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
190 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
191 }
192
193 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.), 0);
194 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.), 4);
195 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.), 6);
196 }
197
198 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
199 fn tuple_structrust-analyzer cargo memoffset 0.8.0 offset_of/tests/tuple_struct().() {
200 #[repr(C)]
201 struct Tuprust-analyzer cargo memoffset 0.8.0 offset_of/tests/Tup#(i32, i32);
202
203 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Tuprust-analyzer cargo memoffset 0.8.0 offset_of/tests/Tup#, 0rust-analyzer cargo memoffset 0.8.0 offset_of/tests/Tup#0.), 0);
204 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Tuprust-analyzer cargo memoffset 0.8.0 offset_of/tests/Tup#, 1rust-analyzer cargo memoffset 0.8.0 offset_of/tests/Tup#1.), 4);
205 }
206
207 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
208 fn offset_unionrust-analyzer cargo memoffset 0.8.0 offset_of/tests/offset_union().() {
209 // Since we're specifying repr(C), all fields are supposed to be at offset 0
210 #[repr(C)]
211 union Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
212 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
213 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
214 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
215 }
216
217 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_of_unionrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of_union!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.), 0);
218 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_of_unionrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of_union!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.), 0);
219 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_of_unionrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of_union!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.), 0);
220 }
221
222 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
223 fn pathrust-analyzer cargo memoffset 0.8.0 offset_of/tests/path().() {
224 mod subrust-analyzer cargo memoffset 0.8.0 offset_of/tests/sub/ {
225 #[repr(C)]
226 pub struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/sub/Foo# {
227 pub xrust-analyzer cargo memoffset 0.8.0 offset_of/tests/sub/Foo#x.: u32,
228 }
229 }
230
231 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(subrust-analyzer cargo memoffset 0.8.0 offset_of/tests/sub/::Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/sub/Foo#, xrust-analyzer cargo memoffset 0.8.0 offset_of/tests/sub/Foo#x.), 0);
232 }
233
234 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
235 fn inside_generic_methodrust-analyzer cargo memoffset 0.8.0 offset_of/tests/inside_generic_method().() {
236 struct Pairrust-analyzer cargo memoffset 0.8.0 offset_of/tests/Pair#<T, U>(T, U);
237
238 fn foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/foo().<T, U>(_: Pairrust-analyzer cargo memoffset 0.8.0 offset_of/tests/Pair#<T, U>) -> usize {
239 offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Pairrust-analyzer cargo memoffset 0.8.0 offset_of/tests/Pair#<T, U>, 1rust-analyzer cargo memoffset 0.8.0 offset_of/tests/Pair#1.)
240 }
241
242 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/foo().(Pairrust-analyzer cargo memoffset 0.8.0 offset_of/tests/Pair#(0, 0)), 4);
243 }
244
245 #[cfg(tuple_ty)]
246 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
247 fn test_tuple_offsetrust-analyzer cargo memoffset 0.8.0 offset_of/tests/test_tuple_offset().() {
248 let f = (0i32, 0.0f32, 0u8);
249 let f_ptr = &f as *const _;
250 let f1_ptr = &f.1 as *const _;
251
252 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
253 f1_ptr as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). f_ptr as usize,
254 offset_of_tuplerust-analyzer cargo memoffset 0.8.0 offset_of/offset_of_tuple!!((i32, f32, u8), 1)
255 );
256 }
257
258 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
259 fn test_raw_fieldrust-analyzer cargo memoffset 0.8.0 offset_of/tests/test_raw_field().() {
260 #[repr(C)]
261 struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
262 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
263 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
264 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
265 }
266
267 let f: Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# = Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
268 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: 0,
269 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [0, 0],
270 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: 0,
271 };
272 let f_ptr = &f as *const _;
273 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f_ptr as usize +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 0, raw_fieldrust-analyzer cargo memoffset 0.8.0 raw_field/raw_field!!(f_ptr, Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.) as usize);
274 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f_ptr as usize +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 4, raw_fieldrust-analyzer cargo memoffset 0.8.0 raw_field/raw_field!!(f_ptr, Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.) as usize);
275 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f_ptr as usize +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 8, raw_fieldrust-analyzer cargo memoffset 0.8.0 raw_field/raw_field!!(f_ptr, Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.) as usize);
276 }
277
278 #[cfg(tuple_ty)]
279 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
280 fn test_raw_field_tuplerust-analyzer cargo memoffset 0.8.0 offset_of/tests/test_raw_field_tuple().() {
281 let t = (0u32, 0u8, false);
282 let t_ptr = &t as *const _;
283 let t_addr = t_ptr as usize;
284
285 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
286 &t.0 as *const _ as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). t_addr,
287 raw_field_tuplerust-analyzer cargo memoffset 0.8.0 raw_field/raw_field_tuple!!(t_ptr, (u32, u8, bool), 0) as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). t_addr
288 );
289 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
290 &t.1 as *const _ as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). t_addr,
291 raw_field_tuplerust-analyzer cargo memoffset 0.8.0 raw_field/raw_field_tuple!!(t_ptr, (u32, u8, bool), 1) as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). t_addr
292 );
293 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
294 &t.2 as *const _ as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). t_addr,
295 raw_field_tuplerust-analyzer cargo memoffset 0.8.0 raw_field/raw_field_tuple!!(t_ptr, (u32, u8, bool), 2) as usize -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). t_addr
296 );
297 }
298
299 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
300 fn test_raw_field_unionrust-analyzer cargo memoffset 0.8.0 offset_of/tests/test_raw_field_union().() {
301 #[repr(C)]
302 union Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
303 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
304 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
305 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
306 }
307
308 let f = Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# { arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: 0 };
309 let f_ptr = &f as *const _;
310 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f_ptr as usize +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 0, raw_field_unionrust-analyzer cargo memoffset 0.8.0 raw_field/raw_field_union!!(f_ptr, Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.) as usize);
311 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f_ptr as usize +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 0, raw_field_unionrust-analyzer cargo memoffset 0.8.0 raw_field/raw_field_union!!(f_ptr, Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.) as usize);
312 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(f_ptr as usize +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). 0, raw_field_unionrust-analyzer cargo memoffset 0.8.0 raw_field/raw_field_union!!(f_ptr, Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.) as usize);
313 }
314
315 #[cfg(any(feature = "unstable_const", stable_const))]
316 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
317 fn const_offsetrust-analyzer cargo memoffset 0.8.0 offset_of/tests/const_offset().() {
318 #[repr(C)]
319 struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
320 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
321 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
322 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
323 }
324
325 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!([0; offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.)].lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 4);
326 }
327
328 #[cfg(feature = "unstable_const")]
329 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
330 fn const_offset_interior_mutablerust-analyzer cargo memoffset 0.8.0 offset_of/tests/const_offset_interior_mutable().() {
331 #[repr(C)]
332 struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
333 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
334 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<u32>,
335 }
336
337 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!([0; offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.)].lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 4);
338 }
339
340 #[cfg(any(feature = "unstable_const", stable_const))]
341 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
342 fn const_fn_offsetrust-analyzer cargo memoffset 0.8.0 offset_of/tests/const_fn_offset().() {
343 const fn test_fnrust-analyzer cargo memoffset 0.8.0 offset_of/tests/test_fn().() -> usize {
344 #[repr(C)]
345 struct Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo# {
346 arust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#a.: u32,
347 brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.: [u8; 2],
348 crust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#c.: i64,
349 }
350
351 offset_ofrust-analyzer cargo memoffset 0.8.0 offset_of/offset_of!!(Foorust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#, brust-analyzer cargo memoffset 0.8.0 offset_of/tests/Foo#b.)
352 }
353
354 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!([0; test_fnrust-analyzer cargo memoffset 0.8.0 offset_of/tests/test_fn().()].lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 4);
355 }
356 }