nrc / derive-new

derive simple constructor functions for Rust structs
MIT License
525 stars 35 forks source link

Add the capability of 'init-only' fields. #59

Open chilabot opened 1 year ago

chilabot commented 1 year ago

I'd be nice to have 'init-only' fields, so you can use them in field initialization:

#[derive(new)]
struct Test
{
    #[new(init)] // <---
    active: bool,

    #[new(value = "Other::new(active)")]
    other: Other
}

Like https://docs.python.org/3/library/dataclasses.html#init-only-variables

I don't think I'd add too much complexity and I'd be really useful. The workaround for this is to declare the init-field after 'other' and to store it in the struct, which also forces a copy if the initialized class takes the parameter by value.