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

macro optfield

A macro that generates structs with optional fields

3 releases (breaking)

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

#86 in #optional

Download history 581/week @ 2024-05-22 473/week @ 2024-05-29 519/week @ 2024-06-05 316/week @ 2024-06-12 421/week @ 2024-06-19 393/week @ 2024-06-26 326/week @ 2024-07-03 327/week @ 2024-07-10 384/week @ 2024-07-17 301/week @ 2024-07-24 460/week @ 2024-07-31 773/week @ 2024-08-07 698/week @ 2024-08-14 855/week @ 2024-08-21 1450/week @ 2024-08-28 2028/week @ 2024-09-04

5,194 downloads per month
Used in 3 crates

MIT/Apache

60KB
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.56.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

~255–700KB
~17K SLoC