-
Notifications
You must be signed in to change notification settings - Fork 533
unions have no active field #478
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
Changes from 1 commit
c3d23c2
20922ac
224303d
3135240
3793103
f1995cd
f85beb5
9f9313f
9ecb58d
7276ea7
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 |
---|---|---|
|
@@ -29,18 +29,22 @@ struct types, except that it must specify exactly one field: | |
let u = MyUnion { f1: 1 }; | ||
``` | ||
|
||
The expression above creates a value of type `MyUnion` with active field `f1`. | ||
Active field of a union can be accessed using the same syntax as struct fields: | ||
The expression above creates a value of type `MyUnion` and initializes the | ||
storage using field `f1`. The union can be accessed using the same syntax as | ||
struct fields: | ||
|
||
```rust,ignore | ||
let f = u.f1; | ||
``` | ||
|
||
Inactive fields can be accessed as well (using the same syntax) if they are | ||
sufficiently layout compatible with the current value kept by the union. | ||
Reading incompatible fields results in undefined behavior. However, the active | ||
field is not generally known statically, so all reads of union fields have to | ||
be placed in `unsafe` blocks. | ||
Unions have no notion of an "active field". Instead, every union access just | ||
interprets the storage at the type of the field used for the access. The effect | ||
of reading a union with a different field than it was written to is that of | ||
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. "field ... it was written to" here looks suspiciously similar to the "active field" that's intended to be removed. 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. The change compared to the previous semantics, I think, is that the "active field"/"field it was written to" doesn't necessarily exist with the new rules. 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. I tried to improve the wording to make it more clear that there is nothing remembering which field was written to, just re-interpreting storage at different types. Does that help? |
||
calling [`transmute`]. Reading data at a bad type results in undefined behavior | ||
(for example, reading the value `3` at type `bool`). | ||
|
||
However, which fields are safe to read and which not is generally not known | ||
statically, so all reads of union fields have to be placed in `unsafe` blocks. | ||
|
||
```rust | ||
# union MyUnion { f1: u32, f2: f32 } | ||
|
@@ -65,9 +69,9 @@ Commonly, code using unions will provide safe wrappers around unsafe union | |
field accesses. | ||
|
||
Another way to access union fields is to use pattern matching. Pattern matching | ||
on union fields uses the same syntax as struct patterns, except that the | ||
pattern must specify exactly one field. Since pattern matching accesses | ||
potentially inactive fields it has to be placed in `unsafe` blocks as well. | ||
on union fields uses the same syntax as struct patterns, except that the pattern | ||
must specify exactly one field. Since pattern matching is like reading the union | ||
with a particular field, it has to be placed in `unsafe` blocks as well. | ||
|
||
```rust | ||
# union MyUnion { f1: u32, f2: f32 } | ||
|
@@ -149,3 +153,4 @@ in [RFC 1897 "Unions v1.2"](https://github.com/rust-lang/rfcs/pull/1897). | |
[_Generics_]: items/generics.html | ||
[_WhereClause_]: items/generics.html#where-clauses | ||
[_StructFields_]: items/structs.html | ||
[`transmute`]: ../../std/mem/fn.transmute.html |
Uh oh!
There was an error while loading. Please reload this page.