-
Couldn't load subscription status.
- Fork 13.9k
Stabilize offset_of_slice
#139673
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
base: master
Are you sure you want to change the base?
Stabilize offset_of_slice
#139673
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1263,10 +1263,10 @@ impl<T> SizedTypeProperties for T {} | |
| /// | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate this item, mark one as |
||
| /// # Offsets of, and in, dynamically sized types | ||
| /// | ||
| /// The field’s type must be [`Sized`], but it may be located in a [dynamically sized] container. | ||
| /// If the field type is dynamically sized, then you cannot use `offset_of!` (since the field's | ||
| /// alignment, and therefore its offset, may also be dynamic) and must take the offset from an | ||
| /// actual pointer to the container instead. | ||
| /// The field’s type must have a statically known alignment. In other words, it is [`Sized`], a slice, | ||
| /// or some wrapper around a slice. Notably, this is not the case if the field is a trait object. | ||
| /// The alignment of trait objects can be different based on what the underlying type is, which can | ||
| /// affect the offset of the field. Therefore you cannot use `offset_of!`. | ||
| /// | ||
| /// ``` | ||
| /// # use core::mem; | ||
|
|
@@ -1281,8 +1281,9 @@ impl<T> SizedTypeProperties for T {} | |
| /// #[repr(C, align(4))] | ||
| /// struct Align4(u32); | ||
| /// | ||
| /// assert_eq!(mem::offset_of!(Struct<dyn Debug>, a), 0); // OK — Sized field | ||
| /// assert_eq!(mem::offset_of!(Struct<Align4>, b), 4); // OK — not DST | ||
| /// assert_eq!(mem::offset_of!(Struct<Align4>, b), 4); // OK — the last field is Sized | ||
| /// assert_eq!(mem::offset_of!(Struct<[u8]>, b), 1); // OK — the last field is a slice | ||
| /// assert_eq!(mem::offset_of!(Struct<dyn Debug>, a), 0); // OK — the struct is unsized, but the field `a` is Sized | ||
| /// | ||
| /// // assert_eq!(mem::offset_of!(Struct<dyn Debug>, b), 1); | ||
| /// // ^^^ error[E0277]: ... cannot be known at compilation time | ||
|
|
@@ -1344,7 +1345,6 @@ impl<T> SizedTypeProperties for T {} | |
| /// The following unstable features expand the functionality of `offset_of!`: | ||
| /// | ||
| /// * [`offset_of_enum`] — allows `enum` variants to be traversed as if they were fields. | ||
| /// * [`offset_of_slice`] — allows getting the offset of a field of type `[T]`. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
|
|
@@ -1374,7 +1374,6 @@ impl<T> SizedTypeProperties for T {} | |
| /// | ||
| /// [dynamically sized]: https://doc.rust-lang.org/reference/dynamically-sized-types.html | ||
| /// [`offset_of_enum`]: https://doc.rust-lang.org/nightly/unstable-book/language-features/offset-of-enum.html | ||
| /// [`offset_of_slice`]: https://doc.rust-lang.org/nightly/unstable-book/language-features/offset-of-slice.html | ||
| #[stable(feature = "offset_of", since = "1.77.0")] | ||
| #[allow_internal_unstable(builtin_syntax)] | ||
| pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) { | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //@run-pass | ||
| #![feature(offset_of_slice)] | ||
|
|
||
| use std::mem::offset_of; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a comment onto
require_type_has_static_alignmentthat notes that changing the behavior will extend the functionality ofoffset_ofand is insta-stable?