#proc-macro #derive #options #automation #generator #update #struct

macro wopt

A procedural macro that automatically generates an Option-wrapped version of a struct, reducing boilerplate for optional updates

18 unstable releases (3 breaking)

Uses new Rust 2024

new 0.4.0 Jul 9, 2025
0.3.9 Jun 8, 2025
0.3.4 May 23, 2025
0.2.9 May 17, 2025
0.1.5 Mar 31, 2025

#2303 in Rust patterns

Download history 410/week @ 2025-03-28 144/week @ 2025-04-04 8/week @ 2025-04-11 526/week @ 2025-05-02 753/week @ 2025-05-09 560/week @ 2025-05-16 204/week @ 2025-05-23 225/week @ 2025-05-30 322/week @ 2025-06-06 32/week @ 2025-06-13 8/week @ 2025-06-20 9/week @ 2025-06-27 151/week @ 2025-07-04

204 downloads per month

MIT license

38KB
792 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(Clone, Copy, Debug, Default, PartialEq, WithOpt)]
#[wopt(derive(Debug, Default, PartialEq))]
struct Example {
    a: u8,
    b: f32,
    c: i16,
}

fn main() {
    // original struct
    let mut ex = Example {
        a: 1,
        b: 2.0,
        c: -3,
    };

    // the original's optional struct
    let mut ex_opt = ExampleOpt {
        a: None,
        b: Some(420.0), // this will patch `ex.b`
        c: None,
    };
    // currently has a modification (b)
    assert!(ex_opt.is_modified());

    // "patch" the original with the optional struct.
    ex.patch(&mut ex_opt);

    // patching mutably "takes" the optional struct, meaning,
    // after patching it no longer has any modifications.
    assert!(!ex_opt.is_modified());

    assert_eq!(
        ex,
        Example {
            a: 1,
            b: 420.0, // the only field to change
            c: -3
        }
    )
}

Struct Attributes

Name Description
derive(...) Specified derivations for the optional struct.
no_serde When provided, does not generate ser/de methods for original struct.

Field Attributes

For more information on how to use these attributes, refer to the structures in tests\common\mod.rs.

Name Description
optional Force the optional version of the current struct to use the optional version of the current field.
ser/de Specify methods of serialization/deserialization (if specified, both are required).
serde Force the generated serialize/deserialize methods of the field (must derive WithOpt) to be used (usually paired with optional).
required Does not wrap the specified field with an Option.
skip Does not include the current field.

Optional Feature(s)

Name Description
bytemuck Serialize/Deserialize using bytemuck.
unchecked Disable unwrap checks.

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.6–1.1MB
~21K SLoC