#struct-fields #struct #optional #field #options

macro optfield

A macro that generates structs with optional fields

4 releases (breaking)

0.4.0 Dec 6, 2024
0.3.0 May 15, 2023
0.2.0 May 6, 2021
0.1.0 Apr 10, 2020

#2975 in Rust patterns

Download history 1327/week @ 2024-08-26 1858/week @ 2024-09-02 3174/week @ 2024-09-09 2971/week @ 2024-09-16 3100/week @ 2024-09-23 3647/week @ 2024-09-30 8556/week @ 2024-10-07 9589/week @ 2024-10-14 10121/week @ 2024-10-21 9447/week @ 2024-10-28 5830/week @ 2024-11-04 7552/week @ 2024-11-11 8010/week @ 2024-11-18 6456/week @ 2024-11-25 8541/week @ 2024-12-02 8665/week @ 2024-12-09

32,013 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

61KB
1.5K SLoC

optfield

crates.io Released API docs Minimum rustc version CI

optfield is a macro that, given a struct, generates another struct with the same fields, but wrapped in Option<T>.

Minimum rustc version: 1.61.0

Install

cargo add optfield

Use

optfield takes the opt struct name as its first argument:

use optfield::optfield;

#[optfield(Opt)]
struct MyStruct<T> {
    text: String,
    number: i32,
    generic: T,
}

This will generate another struct that looks like:

struct Opt<T> {
    text: Option<String>,
    number: Option<i32>,
    generic: Option<T>,
}

optfield supports defining visibility, documentation, attributes and merge methods. For more details and examples check its documentation.

License

Licensed under either of Apache License, Version 2.0 or MIT at your option.

Dependencies

~225–660KB
~16K SLoC