Kolsky / late_init

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

late_init

Partially initialize fields with the rest filled as defaults.

Provides a LateInit derive macro which brings in scope a struct NameLateInit created from struct Name definition.

Then it can be used as follows:

#[derive(Defailt)]
struct HasDefault;
struct NoDefault;

#[derive(LateInit)]
struct Name {
    a: HasDefault,
    b: NoDefault,
    c: HasDefault,
    /*insert multiple field defs similar to a*/
    x: HasDefault,
    y: HasDefault,
    z: NoDefault,
}

fn create_name() -> Name {
    NameLateInit::default()
        .b(NoDefault) // You have to provide fields which do not have default impl
        .x(HasDefault) // These are optional
        .z(NoDefault)
        .finish()
}

May as well serve as an alternative to derive-new.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

License:Apache License 2.0


Languages

Language:Rust 100.0%