-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #45394 - davidtwco:rfc-2008, r=petrochenkov
RFC 2008: Future-proofing enums/structs with #[non_exhaustive] attribute This work-in-progress pull request contains my changes to implement [RFC 2008](rust-lang/rfcs#2008). The related tracking issue is #44109. As of writing, enum-related functionality is not included and there are some issues related to tuple/unit structs. Enum related tests are currently ignored. WIP PR requested by @nikomatsakis [in Gitter](https://gitter.im/rust-impl-period/WG-compiler-middle?at=59e90e6297cedeb0482ade3e).
- Loading branch information
Showing
28 changed files
with
769 additions
and
9 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/doc/unstable-book/src/language-features/non-exhaustive.md
This file contains 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,75 @@ | ||
# `non_exhaustive` | ||
|
||
The tracking issue for this feature is: [#44109] | ||
|
||
[#44109]: https://github.com/rust-lang/rust/issues/44109 | ||
|
||
------------------------ | ||
|
||
The `non_exhaustive` gate allows you to use the `#[non_exhaustive]` attribute | ||
on structs and enums. When applied within a crate, users of the crate will need | ||
to use the `_` pattern when matching enums and use the `..` pattern when | ||
matching structs. Structs marked as `non_exhaustive` will not be able to be | ||
created normally outside of the defining crate. This is demonstrated below: | ||
|
||
```rust,ignore (pseudo-Rust) | ||
use std::error::Error as StdError; | ||
#[non_exhaustive] | ||
pub enum Error { | ||
Message(String), | ||
Other, | ||
} | ||
impl StdError for Error { | ||
fn description(&self) -> &str { | ||
// This will not error, despite being marked as non_exhaustive, as this | ||
// enum is defined within the current crate, it can be matched | ||
// exhaustively. | ||
match *self { | ||
Message(ref s) => s, | ||
Other => "other or unknown error", | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```rust,ignore (pseudo-Rust) | ||
use mycrate::Error; | ||
// This will not error as the non_exhaustive Error enum has been matched with | ||
// a wildcard. | ||
match error { | ||
Message(ref s) => ..., | ||
Other => ..., | ||
_ => ..., | ||
} | ||
``` | ||
|
||
```rust,ignore (pseudo-Rust) | ||
#[non_exhaustive] | ||
pub struct Config { | ||
pub window_width: u16, | ||
pub window_height: u16, | ||
} | ||
// We can create structs as normal within the defining crate when marked as | ||
// non_exhaustive. | ||
let config = Config { window_width: 640, window_height: 480 }; | ||
// We can match structs exhaustively when within the defining crate. | ||
if let Ok(Config { window_width, window_height }) = load_config() { | ||
// ... | ||
} | ||
``` | ||
|
||
```rust,ignore (pseudo-Rust) | ||
use mycrate::Config; | ||
// We cannot create a struct like normal if it has been marked as | ||
// non_exhaustive. | ||
let config = Config { window_width: 640, window_height: 480 }; | ||
// By adding the `..` we can match the config as below outside of the crate | ||
// when marked non_exhaustive. | ||
let &Config { window_width, window_height, .. } = config; | ||
``` | ||
|
This file contains 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 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 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 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 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 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 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 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
Oops, something went wrong.