Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand mem::offset_of! docs #117512

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,13 +1294,64 @@
///
/// Structs, enums, unions and tuples are supported.
///
/// Nested field accesses may be used, but not array indexes like in `C`'s `offsetof`.
/// Nested field accesses may be used, but not array indexes.
///
/// Enum variants may be traversed as if they were fields. Variants themselves do
/// not have an offset.
///
/// Visibility is respected - all types and fields must be visible to the call site:
///
/// ```
/// #![feature(offset_of)]
///
/// mod nested {
/// #[repr(C)]
/// pub struct Struct {
/// private: u8,
/// }
/// }
///
/// // assert_eq!(mem::offset_of!(nested::Struct, private), 0);
/// // ^^^ error[E0616]: field `private` of struct `Struct` is private
/// ```
///
/// Note that type layout is, in general, [subject to change and
/// platform-specific](https://doc.rust-lang.org/reference/type-layout.html).
/// platform-specific](https://doc.rust-lang.org/reference/type-layout.html). If
/// layout stability is required, consider using an [explicit `repr` attribute].
///
/// Rust guarantees that the offset of a given field within a given type will not
/// change over the lifetime of the program. However, two different compilations of
/// the same program may result in different layouts. Also, even within a single
/// program execution, no guarantees are made about types which are *similar* but
/// not *identical*, e.g.:
///
/// ```
/// #![feature(offset_of)]
///
/// use std::mem;

Check failure on line 1331 in library/core/src/mem/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

unused import: `std::mem`
/// struct Wrapper<T, U>(T, U);
///
/// type A = Wrapper<u8, u8>;
/// type B = Wrapper<u8, i8>;
///
/// // Not necessarily identical even though `u8` and `i8` have the same layout!
/// // assert!(mem::offset_of!(A, 1), mem::offset_of!(B, 1));
///
/// #[repr(transparent)]
/// struct U8(u8);
///
/// type C = Wrapper<u8, U8>;
///
/// // Not necessarily identical even though `u8` and `U8` have the same layout!
/// // assert!(mem::offset_of!(A, 1), mem::offset_of!(C, 1));
///
/// struct Empty<T>(PhantomData<T>);

Check failure on line 1348 in library/core/src/mem/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

cannot find type `PhantomData` in this scope
///
/// // Not necessarily identical even though `PhantomData` always has the same layout!
/// // assert!(mem::offset_of!(Empty<u8>, 0), mem::offset_of!(Empty<i8>, 0));
/// ```
///
/// [explicit `repr` attribute]: https://doc.rust-lang.org/reference/type-layout.html#representations
///
/// # Examples
///
Expand Down Expand Up @@ -1329,6 +1380,15 @@
///
/// assert_eq!(mem::offset_of!(NestedA, b.0), 0);
///
/// #[repr(u8)]
/// enum Enum {
/// A(u8, u16),
/// B { one: u8, two: u16 },
/// }
///
/// assert_eq!(mem::offset_of!(Enum, A.0), 1);

Check failure on line 1389 in library/core/src/mem/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

no field `A` on type `Enum`
joshlf marked this conversation as resolved.
Show resolved Hide resolved
/// assert_eq!(mem::offset_of!(Enum, B.two), 2);

Check failure on line 1390 in library/core/src/mem/mod.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check

no field `B` on type `Enum`
///
/// # #[cfg(not(bootstrap))]
/// assert_eq!(mem::offset_of!(Option<&u8>, Some.0), 0);
/// ```
Expand Down
Loading