Expand description
§Nested Structs & Enums
This crate allows you to nest structs, enums, and impls, in a way that is similar to how it’s done in Zig.
§Examples
nesting::nest! {
// #![] attributes apply to all structs and enums in the nest!{} block.
#![derive(Debug)]
// Becomes:
// #![structs(allow(dead_code))]
// #![enums(allow(dead_code))]
// You can also scope the attributes like so:
#![all(allow(dead_code))] // using `all` means it will also apply to `impl`s!
// Which is the same as:
// #![structs(allow(dead_code))]
// #![enums(allow(dead_code))]
// #![impls(allow(dead_code))]
pub struct MarkerStruct;
struct TupleStruct(String, u32);
struct FieldStruct {
field1: String,
field2: u32
child: ChildStruct,
my_enum: Enum
// Loose functions get put into impl StructName {} blocks.
// i.e. this would be impl FieldStruct { pub fn my_fn() {} }
pub fn some_struct_fn(&self) {
println!("This function is nested inside the struct!");
}
// You can add attributes to individual structs/enums/impls as usual
#[derive(Clone, Hash)]
struct ChildStruct {
field1: String,
field2: u32,
child: ChildChildStruct
// You can nest infinitely...
#[derive(Clone, Hash)]
struct ChildChildStruct;
}
impl ChildStruct {
pub fn some_impl_fn(&self) {
println!("This function is nested inside struct->impl");
}
}
// We can impl Foo for Bar
impl std::fmt::Display for ChildStruct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", &self.field1, self.field2)
}
}
// We can make enums too
enum Enum {
A,
B(String),
C {
a: String,
b: u64,
}
// Nesting works just fine in enums
#[derive(Default)]
struct StructInsideEnum(String);
enum Enum2 { A, B, C }
pub fn some_enum_fn(&self) {
println!("This is a function inside an enum!");
}
}
}
}
Macros§
- nest
- Create nested structs, enums, and impls.