8000 Describe Sized requirements for mem::offset_of by nicholasbishop · Pull Request #127354 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Describe Sized requirements for mem::offset_of #127354

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

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Changes from all 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
Describe Sized requirements for mem::offset_of
The container doesn't have to be sized, but the field must be sized (at
least until #126151 is stable).
  • Loading branch information
nicholasbishop committed Jul 5, 2024
commit ccd8dccfc6c19329e9c82e727a877113b8af9860
14 changes: 14 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,20 @@ impl<T> SizedTypeProperties for T {}
/// // ^^^ error[E0616]: field `private` of struct `Struct` is private
/// ```
///
/// Only [`Sized`] fields are supported, but the container may be unsized:
/// ```
/// # use core::mem;
/// #[repr(C)]
/// pub struct Struct {
/// a: u8,
/// b: [u8],
/// }
///
/// assert_eq!(mem::offset_of!(Struct, a), 0); // OK
/// // assert_eq!(mem::offset_of!(Struct, b), 1);
/// // ^^^ error[E0277]: doesn't have a size known at compile-time
/// ```
///
/// Note that type layout is, in general, [subject to change and
/// platform-specific](https://doc.rust-lang.org/reference/type-layout.html). If
/// layout stability is required, consider using an [explicit `repr` attribute].
Expand Down
Loading
0