-
Notifications
You must be signed in to change notification settings - Fork 533
Add non_exhaustive
to reference.
#609
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# Type system attributes | ||
|
||
The following [attributes] are used for changing how a type can be used. | ||
|
||
## The `non_exhaustive` attribute | ||
|
||
The *`non_exhaustive` attribute* indicates that a type or variant may have | ||
more fields or variants added in the future. It can be applied to | ||
[`struct`s][struct], [`enum`s][enum], and `enum` variants. | ||
|
||
The `non_exhaustive` attribute uses the [_MetaWord_] syntax and thus does not | ||
take any inputs. | ||
|
||
Within the defining crate, `non_exhaustive` has no effect. | ||
|
||
```rust | ||
#[non_exhaustive] | ||
pub struct Config { | ||
pub window_width: u16, | ||
pub window_height: u16, | ||
} | ||
|
||
#[non_exhaustive] | ||
pub enum Error { | ||
Message(String), | ||
Other, | ||
} | ||
|
||
pub enum Message { | ||
#[non_exhaustive] Send { from: u32, to: u32, contents: String }, | ||
#[non_exhaustive] Reaction(u32), | ||
#[non_exhaustive] Quit, | ||
} | ||
|
||
// Non-exhaustive structs can be constructed as normal within the defining crate. | ||
let config = Config { window_width: 640, window_height: 480 }; | ||
|
||
// Non-exhaustive structs can be matched on exhaustively within the defining crate. | ||
if let Config { window_width, window_height } = config { | ||
// ... | ||
} | ||
|
||
let error = Error::Other; | ||
let message = Message::Reaction(3); | ||
|
||
// Non-exhaustive enums can be matched on exhaustively within the defining crate. | ||
match error { | ||
Error::Message(ref s) => { }, | ||
Error::Other => { }, | ||
} | ||
|
||
match message { | ||
// Non-exhaustive variants can be matched on exhaustively within the defining crate. | ||
Message::Send { from, to, contents } => { }, | ||
Message::Reaction(id) => { }, | ||
Message::Quit => { }, | ||
} | ||
``` | ||
|
||
Outside of the defining crate, types annotated with `non_exhaustive` have limitations that | ||
preserve backwards compatibility when new fields or variants are added. | ||
|
||
Non-exhaustive types cannot be constructed outside of the defining crate: | ||
|
||
- Non-exhaustive variants ([`struct`][struct] or [`enum` variant][enum]) cannot be constructed | ||
with a [_StructExpression_] \(including with [functional update syntax]). | ||
- [`enum`][enum] instances can be constructed in an [_EnumerationVariantExpression_]. | ||
|
||
```rust,ignore (requires multiple crates) | ||
// `Config`, `Error`, and `Message` are types defined in an upstream crate that have been | ||
// annotated as `#[non_exhaustive]`. | ||
use upstream::{Config, Error, Message}; | ||
|
||
// Cannot construct an instance of `Config`, if new fields were added in | ||
// a new version of `upstream` then this would fail to compile, so it is | ||
// disallowed. | ||
let config = Config { window_width: 640, window_height: 480 }; | ||
|
||
// Can construct an instance of `Error`, new variants being introduced would | ||
// not result in this failing to compile. | ||
let error = Error::Message("foo".to_string()); | ||
|
||
// Cannot construct an instance of `Message::Send` or `Message::Reaction`, | ||
// if new fields were added in a new version of `upstream` then this would | ||
// fail to compile, so it is disallowed. | ||
let message = Message::Send { from: 0, to: 1, contents: "foo".to_string(), }; | ||
let message = Message::Reaction(0); | ||
|
||
// Cannot construct an instance of `Message::Quit`, if this were converted to | ||
// a tuple-variant `upstream` then this would fail to compile. | ||
let message = Message::Quit; | ||
``` | ||
|
||
There are limitations when matching on non-exhaustive types outside of the defining crate: | ||
|
||
- When pattern matching on a non-exhaustive variant ([`struct`][struct] or [`enum` variant][enum]), | ||
a [_StructPattern_] must be used which must include a `..`. Tuple variant constructor visibility | ||
is lowered to `min($vis, pub(crate))`. | ||
- When pattern matching on a non-exhaustive [`enum`][enum], matching on a variant does not | ||
contribute towards the exhaustiveness of the arms. | ||
|
||
```rust, ignore (requires multiple crates) | ||
// `Config`, `Error`, and `Message` are types defined in an upstream crate that have been | ||
// annotated as `#[non_exhaustive]`. | ||
use upstream::{Config, Error, Message}; | ||
|
||
// Cannot match on a non-exhaustive enum without including a wildcard arm. | ||
match error { | ||
Error::Message(ref s) => { }, | ||
Error::Other => { }, | ||
// would compile with: `_ => {},` | ||
} | ||
|
||
// Cannot match on a non-exhaustive struct without a wildcard. | ||
if let Ok(Config { window_width, window_height }) = config { | ||
// would compile with: `..` | ||
} | ||
|
||
match message { | ||
// Cannot match on a non-exhaustive struct enum variant without including a wildcard. | ||
Message::Send { from, to, contents } => { }, | ||
// Cannot match on a non-exhaustive tuple or unit enum variant. | ||
Message::Reaction(type) => { }, | ||
Message::Quit => { }, | ||
} | ||
``` | ||
|
||
Non-exhaustive types are always considered inhabited in downstream crates. | ||
|
||
[_EnumerationVariantExpression_]: ../expressions/enum-variant-expr.md | ||
[_MetaWord_]: ../attributes.md#meta-item-attribute-syntax | ||
[_StructExpression_]: ../expressions/struct-expr.md | ||
[_StructPattern_]: ../patterns.md#struct-patterns | ||
[_TupleStructPattern_]: ../patterns.md#tuple-struct-patterns | ||
[`if let`]: ../expressions/if-expr.md#if-let-expressions | ||
[`match`]: ../expressions/match-expr.md | ||
[attributes]: ../attributes.md | ||
[enum]: ../items/enumerations.md | ||
[functional update syntax]: ../expressions/struct-expr.md#functional-update-syntax | ||
[struct]: ../items/structs.md |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.