@@ -1263,10 +1263,10 @@ impl<T> SizedTypeProperties for T {}
1263
1263
///
1264
1264
/// # Offsets of, and in, dynamically sized types
1265
1265
///
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!` .
1270
1270
///
1271
1271
/// ```
1272
1272
/// # use core::mem;
@@ -1281,8 +1281,9 @@ impl<T> SizedTypeProperties for T {}
1281
1281
/// #[repr(C, align(4))]
1282
1282
/// struct Align4(u32);
1283
1283
///
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
1286
1287
///
1287
1288
/// // assert_eq!(mem::offset_of!(Struct<dyn Debug>, b), 1);
1288
1289
/// // ^^^ error[E0277]: ... cannot be known at compilation time
@@ -1344,7 +1345,6 @@ impl<T> SizedTypeProperties for T {}
1344
1345
/// The following unstable features expand the functionality of `offset_of!`:
1345
1346
///
1346
1347
/// * [`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]`.
1348
1348
///
1349
1349
/// # Examples
1350
1350
///
@@ -1374,7 +1374,6 @@ impl<T> SizedTypeProperties for T {}
1374
1374
///
1375
1375
/// [dynamically sized]: https://doc.rust-lang.org/reference/dynamically-sized-types.html
1376
1376
/// [`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
1378
1377
#[ stable( feature = "offset_of" , since = "1.77.0" ) ]
1379
1378
#[ allow_internal_unstable( builtin_syntax) ]
1380
1379
pub macro offset_of ( $Container: ty, $( $fields: expr) + $( , ) ?) {
0 commit comments