From 1862054af885d5f612690f789fe91c85d202a3d9 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Fri, 5 Jul 2024 01:55:01 -0400 Subject: [PATCH] Describe Sized requirements for mem::offset_of The container doesn't have to be sized, but the field must be sized (at least until https://github.com/rust-lang/rust/issues/126151 is stable). --- core/src/mem/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/src/mem/mod.rs b/core/src/mem/mod.rs index 9054ade2d7968..dd4b6e823434e 100644 --- a/core/src/mem/mod.rs +++ b/core/src/mem/mod.rs @@ -1266,6 +1266,20 @@ impl 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].