Skip to content

Commit c76182b

Browse files
committed
Auto merge of rust-lang#114208 - GKFX:offset_of_enum, r=wesleywiser
Support enum variants in offset_of! This MR implements support for navigating through enum variants in `offset_of!`, placing the enum variant name in the second argument to `offset_of!`. The RFC placed it in the first argument, but I think it interacts better with nested field access in the second, as you can then write things like ```rust offset_of!(Type, field.Variant.field) ``` Alternatively, a syntactic distinction could be made between variants and fields (e.g. `field::Variant.field`) but I'm not convinced this would be helpful. [RFC 3308 # Enum Support](https://rust-lang.github.io/rfcs/3308-offset_of.html#enum-support-offset_ofsomeenumstructvariant-field_on_variant) Tracking Issue rust-lang#106655.
2 parents 207f828 + 7eaa179 commit c76182b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

core/src/mem/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,11 +1292,15 @@ impl<T> SizedTypeProperties for T {}
12921292

12931293
/// Expands to the offset in bytes of a field from the beginning of the given type.
12941294
///
1295-
/// Only structs, unions and tuples are supported.
1295+
/// Structs, enums, unions and tuples are supported.
12961296
///
12971297
/// Nested field accesses may be used, but not array indexes like in `C`'s `offsetof`.
12981298
///
1299-
/// Note that the output of this macro is not stable, except for `#[repr(C)]` types.
1299+
/// Enum variants may be traversed as if they were fields. Variants themselves do
1300+
/// not have an offset.
1301+
///
1302+
/// Note that type layout is, in general, [subject to change and
1303+
/// platform-specific](https://doc.rust-lang.org/reference/type-layout.html).
13001304
///
13011305
/// # Examples
13021306
///
@@ -1324,6 +1328,9 @@ impl<T> SizedTypeProperties for T {}
13241328
/// struct NestedB(u8);
13251329
///
13261330
/// assert_eq!(mem::offset_of!(NestedA, b.0), 0);
1331+
///
1332+
/// # #[cfg(not(bootstrap))]
1333+
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
13271334
/// ```
13281335
#[unstable(feature = "offset_of", issue = "106655")]
13291336
#[allow_internal_unstable(builtin_syntax, hint_must_use)]

0 commit comments

Comments
 (0)