Support enum variants in offset_of!

This commit is contained in:
George Bateman 2023-08-15 20:10:45 +01:00
parent 9d83ac2179
commit e936416a8d
No known key found for this signature in database
GPG key ID: C417AA9C4039EFCF
27 changed files with 472 additions and 75 deletions

View file

@ -514,6 +514,7 @@ E0791: include_str!("./error_codes/E0791.md"),
E0792: include_str!("./error_codes/E0792.md"),
E0793: include_str!("./error_codes/E0793.md"),
E0794: include_str!("./error_codes/E0794.md"),
E0795: include_str!("./error_codes/E0795.md"),
}
// Undocumented removed error codes. Note that many removed error codes are kept in the list above

View file

@ -0,0 +1,28 @@
Invalid argument for the `offset_of!` macro.
Erroneous code example:
```compile_fail,E0795
#![feature(offset_of)]
let x = std::mem::offset_of!(Option<u8>, Some);
```
The `offset_of!` macro gives the offset of a field within a type. It can
navigate through enum variants, but the final component of its second argument
must be a field and not a variant.
The offset of the contained `u8` in the `Option<u8>` can be found by specifying
the field name `0`:
```
#![feature(offset_of)]
let x: usize = std::mem::offset_of!(Option<u8>, Some.0);
```
The discriminant of an enumeration may be read with `core::mem::discriminant`,
but this is not always a value physically present within the enum.
Further information about enum layout may be found at
https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html.