https://github.com/engineersbox/rust-struct-reification
A macro to reify structs and their fields for type-safe runtime invocation and reflective access to fields and attributes
https://github.com/engineersbox/rust-struct-reification
attributes reflection reification rust struct
Last synced: 4 months ago
JSON representation
A macro to reify structs and their fields for type-safe runtime invocation and reflective access to fields and attributes
- Host: GitHub
- URL: https://github.com/engineersbox/rust-struct-reification
- Owner: EngineersBox
- Created: 2022-03-26T06:27:08.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-03-27T07:17:27.000Z (almost 4 years ago)
- Last Synced: 2025-03-22T17:44:52.065Z (11 months ago)
- Topics: attributes, reflection, reification, rust, struct
- Language: Rust
- Homepage:
- Size: 4.88 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rust-Struct-Reification
A macro to reify structs and their fields for type-safe runtime invocation and reflective access to fields and attributes
## Example:
```rust
use crate::reify;
reify!{
struct TestStruct {
#[some_attr=5]
pub field1: u64,
#[macro_attr(Value)]
field2: Vec,
field3: Box,
}
}
impl TestStruct {
pub fn new() -> TestStruct {
return TestStruct {
field1: 42,
field2: vec![String::from("something"), String::from("else")],
field3: Box::new(64),
};
}
}
fn main() {
println!("{:?}", TestStruct::get_field_attribute_map());
// Prints: { "field1": "some_attr=5", "field2": "macro_attr(Value)", "field3": "" }
println!("{:?}", TestStruct::get_field_attribute("field1"));
// Prints: Ok(Some("some_attr=5"))
println!("{:?}", TestStruct::get_field_attribute("field3"));
// Prints: Ok(None)
println!("{:?}", TestStruct::get_field_attribute("field4"));
// Prints: Err(TypedAttributeRetrievalError{ message: "..." })
let test_struct: TestStruct = TestStruct::new();
println!("{:?}", test_struct::get_field("field1"));
// Prints: Ok(Any { .. })
println!("{:?}", test_struct::get_field_typed::>("field2"))
// Prints: Ok({ "something", "else" })
}
```