about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-07-07 14:22:02 +0200
committerGitHub <noreply@github.com>2024-07-07 14:22:02 +0200
commitc40530d0de9ab1eca158a3113d3873e983bc1bf7 (patch)
treec56c42bb239627d545dc1d533dd111304ad19549
parent510020ad4cfa1bd8a51dec638a8b39b3f546a7b9 (diff)
parentccd8dccfc6c19329e9c82e727a877113b8af9860 (diff)
downloadrust-c40530d0de9ab1eca158a3113d3873e983bc1bf7.tar.gz
rust-c40530d0de9ab1eca158a3113d3873e983bc1bf7.zip
Rollup merge of #127354 - nicholasbishop:bishop-sized-doc, r=Nilstrieb
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).
-rw-r--r--library/core/src/mem/mod.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs
index 9054ade2d79..dd4b6e82343 100644
--- a/library/core/src/mem/mod.rs
+++ b/library/core/src/mem/mod.rs
@@ -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].