Zero cost compile-time map based on struct.
#[derive(Field)]
implements Field
.
Following example implement Field<u8>
, Field<u16>
, Field<String>
and access field by field type.
use fieldmap::Field;
#[derive(Field)]
struct ExampleType {
value_u8: u8,
value_u16: u16,
value_s: String,
}
let x = ExampleType {
value_u8: 100,
value_u16: 200,
value_s: "300".into(),
};
assert_eq!(*Field::<u8>::get(&x), 100);
assert_eq!(*Field::<u16>::get(&x), 200);
assert_eq!(*Field::<String>::get(&x), "300");
#[derive(Field)]
can use only struct with different type of each field.
#[derive(Fields)]
implements [Fields
].
You need to specify #[fields(item = "{TraitName}")]
.
use fieldmap::Fields;
use std::fmt::Debug;
#[derive(Fields)]
#[fields(item = "Debug")]
struct ExampleType {
value_u8: u8,
value_u16: u16,
value_s: String,
}
let x = ExampleType {
value_u8: 100,
value_u16: 200,
value_s: "300".into(),
};
for (name, value) in x.iter() {
println!("{} = {:?}", name, value);
}
Output:
value_u8 = 100
value_u16 = 200
value_s = "300"
Only 'static
type can implement Fields
.
Because this limitation is caused by Rust not supporting GAT (generic associated types),
so the limitation may be removed in the future.
This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.