#struct-fields #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

#47 in #struct-fields

Download history 94/week @ 2023-11-20 65/week @ 2023-11-27 58/week @ 2023-12-04 44/week @ 2023-12-11 207/week @ 2023-12-18 77/week @ 2023-12-25 70/week @ 2024-01-01 54/week @ 2024-01-08 66/week @ 2024-01-15 122/week @ 2024-01-22 49/week @ 2024-01-29 185/week @ 2024-02-05 368/week @ 2024-02-12 600/week @ 2024-02-19 1220/week @ 2024-02-26 1988/week @ 2024-03-04

4,212 downloads per month
Used in dateless

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

~325–780KB
~19K SLoC