/* use xreflect::Reflect; // just derive Reflect #[derive(Reflect)] pub struct Enemy { pub health: u8, } #[derive(Reflect)] pub enum GameState { Playing, Won { score: i32, remaining_health: u8 }, Lost(Enemy), } fn main() { // With Reflect, you can: // 1. access and modify fields dynamically let mut enemy = Enemy { health: 0 }; let enemy_health = enemy.field::("health"); enemy.set_field::("health", 7); // 2. construct items dynamically let mut game_state = GameState::Playing; game_state = GameState::construct("Won") .with_field::("score", 0.0) .with_field::("remaining_health", 20); game_state = GameState::construct("Lost").with_tuple_field::(0, enemy); // 3. iterate through an item's fields and their types for (field_name, field_type) in Enemy::fields() { // both field_name and field_type are &str match field_type { "i32" => { // note that you still have to provide the generic with the actual type enemy.field::(field_name) = 1; } // custom types have convenience associated functions GameState::type_name() => {} } } } */ use std::any::TypeId; use xreflect::Reflect; // just derive Reflect #[derive(Reflect)] pub struct Enemy { health: u8, } #[derive(Reflect)] pub enum GameState { Playing, Won { score: i32, remaining_health: u8 }, Lost(Enemy), } #[test] fn main() { // You can now access and modify fields dynamically let mut enemy = Enemy { health: 2 }; let enemy_health = enemy.get_field::("health"); assert_eq!(enemy_health, &2); enemy.set_field::("health", 7); assert_eq!(enemy.get_field::("health"), &7); let game_state = GameState::Won { score: 500, remaining_health: 17 }; assert_eq!(game_state.get_type_of_field("score"), TypeId::of::()); // supports tuple structs as well! let game_state = GameState::Lost(enemy); assert_eq!(game_state.get_type_of_field_at(0), TypeId::of::()); }