4 releases
Uses new Rust 2024
0.1.8 | Apr 4, 2025 |
---|---|
0.1.7 | Apr 1, 2025 |
0.1.5 | Mar 31, 2025 |
#2495 in Rust patterns
561 downloads per month
12KB
197 lines
wopt (with-options)
Description
A procedural macro that automatically generates an Option-wrapped version of a struct, reducing boilerplate for optional updates.
Example
use wopt::*;
#[derive(WithOpt)]
#[wopt(derive(Debug, Default, PartialEq))]
struct Example {
a: u8,
#[wopt(required)]
b: f32,
c: String,
}
fn main() {
let b = 420.0;
let mut ex_opt = ExampleOpt::default();
ex_opt.b = b;
assert_eq!(
ex_opt,
ExampleOpt {
a: None,
b,
c: None
},
)
}
Field Attributes
Name | Description |
---|---|
required |
Does not wrap the specified field with an Option . |
skip |
Does not include the specified field. |
Optional Features
Name | Description |
---|---|
rkyv | Serialize/Deserialize using rkyv . |
serde | Seriailze/Deserialize using serde . |
Additional Notes
The automatically generated optional-struct does not come with any trait/derivation implementations. The fields are publicized, however, it may be helpful to specify the Default
trait:
#[derive(WithOpt)]
#[wopt(derive(Default))] // attempts to implement `Default`
struct ExampleWithDefault(u8);
Dependencies
~0.2–1MB
~22K SLoC