https://github.com/modprog/non-exhaustive
https://github.com/modprog/non-exhaustive
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/modprog/non-exhaustive
- Owner: ModProg
- License: apache-2.0
- Created: 2024-02-24T14:42:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-25T23:10:16.000Z (over 1 year ago)
- Last Synced: 2025-02-08T14:22:22.460Z (5 months ago)
- Language: Rust
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# non-exhaustive
[](https://github.com/ModProg/non-exhaustive/actions/workflows/test.yaml)
[](https://crates.io/crates/non-exhaustive)
[](https://docs.rs/non-exhaustive)
[](https://modprog.github.io/non-exhaustive/non_exhaustive/)Macro to create non_exhaustive structs and structs with private fields with the [functional update syntax](https://doc.rust-lang.org/reference/expressions/struct-expr.html#functional-update-syntax), i.e., using `..Default::default()`.
Given the foreign structs:
```rust
#[non_exhaustive]
#[derive(Default)]
pub struct NonExhaustive {
pub field: usize
}#[derive(Default)]
pub struct PrivateFields {
pub pub_field: usize,
private_field: usize
}
```The following is not possible:
```rust
NonExhaustive {
field: 1,
..Default::default()
};PrivateFields {
pub_field: 1,
..Default::default()
};
```[`non_exhaustive!`] remedies that:
```rust
use non_exhaustive::non_exhaustive;
# mod module {#[derive(Default)] pub struct NonExhaustive {pub field: usize, _hack: usize} #[derive(Default)] pub struct PrivateFields { pub pub_field: usize, private_field: usize}} use module::*;non_exhaustive! {NonExhaustive {
field: 1,
..Default::default()
}};non_exhaustive! {PrivateFields {
pub_field: 1,
..Default::default()
}};
```For the common case of using [`Default::default()`], [`non_exhaustive!`] allows omitting the `..expression`:
```rust
use non_exhaustive::non_exhaustive;
# mod module {#[derive(Default)] pub struct NonExhaustive {pub field: usize, _hack: usize} #[derive(Default)] pub struct PrivateFields { pub pub_field: usize, private_field: usize}} use module::*;non_exhaustive!(NonExhaustive { field: 1 });
non_exhaustive!(PrivateFields { pub_field: 1 });
```Under the hood, [`non_exhaustive!`] is extremely simple, it expands to:
```rust
# mod module {#[derive(Default)] pub struct NonExhaustive {pub field: usize, _hack: usize}} use module::*;{
let mut value: NonExhaustive = Default::default();
value.field = 1;
value
};
```[`non_exhaustive!`]: https://docs.rs/non-exhaustive/latest/non_exhaustive/macro.non_exhaustive.html
[`Default::default()`]: https://doc.rust-lang.org/std/default/trait.Default.html