Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/modprog/non-exhaustive
https://github.com/modprog/non-exhaustive
Last synced: about 1 month 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 (11 months ago)
- Default Branch: main
- Last Pushed: 2024-02-25T23:10:16.000Z (11 months ago)
- Last Synced: 2024-10-28T12:36:01.454Z (3 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
[![CI Status](https://github.com/ModProg/non-exhaustive/actions/workflows/test.yaml/badge.svg)](https://github.com/ModProg/non-exhaustive/actions/workflows/test.yaml)
[![Crates.io](https://img.shields.io/crates/v/non-exhaustive)](https://crates.io/crates/non-exhaustive)
[![Docs.rs](https://img.shields.io/crates/v/template?color=informational&label=docs.rs)](https://docs.rs/non-exhaustive)
[![Documentation for `main`](https://img.shields.io/badge/docs-main-informational)](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