wopt

Crates.iowopt
lib.rswopt
version
sourcesrc
created_at2025-03-31 00:12:19.451705+00
updated_at2025-05-20 22:45:03.693276+00
descriptionA procedural macro that automatically generates an Option-wrapped version of a struct, reducing boilerplate for optional updates.
homepage
repositoryhttps://github.com/splurf/wopt
max_upload_size
id1612894
Cargo.toml error:TOML parse error at line 23, column 1 | 23 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Evan Schwartzentruber (eschwart)

documentation

README

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
bytemuck Serialize/Deserialize using bytemuck.
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);
Commit count: 0

cargo fmt