Skip to content

Commit c2dc5a8

Browse files
committed
document the behavior of offset_of_slice
1 parent 6df4070 commit c2dc5a8

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

library/core/src/mem/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1263,10 +1263,10 @@ impl<T> SizedTypeProperties for T {}
12631263
///
12641264
/// # Offsets of, and in, dynamically sized types
12651265
///
1266-
/// The field’s type must be [`Sized`], but it may be located in a [dynamically sized] container.
1267-
/// If the field type is dynamically sized, then you cannot use `offset_of!` (since the field's
1268-
/// alignment, and therefore its offset, may also be dynamic) and must take the offset from an
1269-
/// actual pointer to the container instead.
1266+
/// The field’s type must have a statically known alignment. In other words, it is [`Sized`], a slice,
1267+
/// or some wrapper around a slice. Notably, this is not the case if the field is a trait object.
1268+
/// The alignment of trait objects can be different based on what the underlying type is, which can
1269+
/// affect the offset of the field. Therefore you cannot use `offset_of!`.
12701270
///
12711271
/// ```
12721272
/// # use core::mem;
@@ -1281,8 +1281,9 @@ impl<T> SizedTypeProperties for T {}
12811281
/// #[repr(C, align(4))]
12821282
/// struct Align4(u32);
12831283
///
1284-
/// assert_eq!(mem::offset_of!(Struct<dyn Debug>, a), 0); // OK — Sized field
1285-
/// assert_eq!(mem::offset_of!(Struct<Align4>, b), 4); // OK — not DST
1284+
/// assert_eq!(mem::offset_of!(Struct<Align4>, b), 4); // OK — the last field is Sized
1285+
/// assert_eq!(mem::offset_of!(Struct<[u8]>, b), 4); // OK — the last field is a slice
1286+
/// assert_eq!(mem::offset_of!(Struct<dyn Debug>, a), 0); // OK — the struct is unsized, but the field `a` is Sized
12861287
///
12871288
/// // assert_eq!(mem::offset_of!(Struct<dyn Debug>, b), 1);
12881289
/// // ^^^ error[E0277]: ... cannot be known at compilation time
@@ -1344,7 +1345,6 @@ impl<T> SizedTypeProperties for T {}
13441345
/// The following unstable features expand the functionality of `offset_of!`:
13451346
///
13461347
/// * [`offset_of_enum`] — allows `enum` variants to be traversed as if they were fields.
1347-
/// * [`offset_of_slice`] — allows getting the offset of a field of type `[T]`.
13481348
///
13491349
/// # Examples
13501350
///
@@ -1374,7 +1374,6 @@ impl<T> SizedTypeProperties for T {}
13741374
///
13751375
/// [dynamically sized]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
13761376
/// [`offset_of_enum`]: https://doc.rust-lang.org/nightly/unstable-book/language-features/offset-of-enum.html
1377-
/// [`offset_of_slice`]: https://doc.rust-lang.org/nightly/unstable-book/language-features/offset-of-slice.html
13781377
#[stable(feature = "offset_of", since = "1.77.0")]
13791378
#[allow_internal_unstable(builtin_syntax)]
13801379
pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) {

0 commit comments

Comments
 (0)