flatten_structs

Crates.ioflatten_structs
lib.rsflatten_structs
version0.1.0
created_at2025-06-16 03:10:13.531642+00
updated_at2025-06-16 03:10:13.531642+00
descriptionAllows inlining fields into another struct.
homepage
repositoryhttps://github.com/LukaOber/flatten_structs
max_upload_size
id1713869
size18,209
Luka (LukaOber)

documentation

README

flatten_structs

Allows inlining fields into another struct. This derive processes #[flatten] attributes and inlines the field definitions for those types, the names of flattened fields are ignored. Note that this macro generates a new macro with the same name as the derived type that is exported with pub(crate). This generated macro can be used to inspect the field definitions of the derived type. This mechanism is what allows flattening types so any types that will later be flattened needs to also use this derive macro. Due to declarative macro limitations any #[flatten] attributes need to be the first attribute for a field or it won't be found by the macro. The code used in this library is originally from here.

In most cases you should not need this macro. But if you have different structs, which share a lot of fields and you want to avoid duplicating their declaration and avoid an indirection this is useful.

flatten_structs!(
    #[allow(unused)]
    pub struct BaseStruct {
        enable: bool,
        #[flatten]
        nested: NestedStruct,
    }
);

flatten_structs!(
    #[allow(unused)]
    struct NestedStruct {
        value_0: f32,
        value_1: f32,
    }
);
// Will expand the BaseStruct to this
let base_struct = BaseStruct {
    enable: true,
    value_0: 0.0,
    value_1: 0.0,
};

License: MIT

Commit count: 2

cargo fmt