Crates.io | nested-struct |
lib.rs | nested-struct |
version | 0.1.0 |
source | src |
created_at | 2023-02-04 07:12:00.833985 |
updated_at | 2023-02-04 07:12:00.833985 |
description | Create nested structs using a macro |
homepage | https://github.com/chipsenkbeil/nested-struct |
repository | https://github.com/chipsenkbeil/nested-struct |
max_upload_size | |
id | 776237 |
size | 20,516 |
Ever wanted to be able to create a data structure that contains nested data? Do you find yourself creating many individual structs simply to combine them together? Well, this is the library for you!
Transform this
pub struct MyStruct {
pub data: MyStructData,
}
pub struct MyStructData {
pub data: u32
}
Into this
use nested_struct::*;
nested_struct! {
pub struct MyStruct {
pub data: MyStructData {
pub data: u32
}
}
}
Creates a new struct that may feature nested fields.
use nested_struct::*;
nested_struct! {
pub struct MyStruct {
pub regular_field: u32,
pub nested_field: NestedField {
pub inner_field: bool
}
}
}
let _ = MyStruct {
regular_field: 123,
nested_field: NestedField {
inner_field: true,
},
};
Nesting is not limited to a single level. You can generate structs with multi-nested fields:
use nested_struct::*;
nested_struct! {
pub struct MyStruct {
pub nested_field: NestedField {
pub nested_field: DeeperNestedField {
pub inner_field: bool
}
}
}
}
let _ = MyStruct {
nested_field: NestedField {
nested_field: DeeperNestedField {
inner_field: true,
}
},
};
Like with a normal struct, nested fields can have attributes placed on them:
use nested_struct::*;
nested_struct! {
pub struct MyStruct {
pub regular_field: u32,
#[doc = "my nested field"]
pub nested_field: NestedField {
pub inner_field: bool
}
}
}
If you want to apply attributes on the generated, nested struct, you need to
use the @nested
marker. This can be used multiple times, but must occur AFTER
any field-specific attributes:
use nested_struct::*;
nested_struct! {
pub struct MyStruct {
pub regular_field: u32,
#[doc = "my nested field"]
@nested(#[derive(Clone)])
pub nested_field: NestedField {
pub inner_field: bool
}
}
}
let nested_field = NestedField { inner_field: true };
let _ = MyStruct {
regular_field: 123,
nested_field: nested_field.clone(),
};
This project is licensed under either of
Apache License, Version 2.0, (LICENSE-APACHE or apache-license) MIT license (LICENSE-MIT or mit-license) at your option.