dynasty_rs/reflection/
mod.rs

1use std::any::{Any, TypeId};
2use std::collections::HashMap;
3use crate::error::DynastyError;
4
5#[cfg(feature = "reflection")]
6#[derive(Clone)]
7pub struct ReflectionData {
8    fields: HashMap<String, FieldInfo>,
9    methods: HashMap<String, MethodInfo>,
10}
11
12#[cfg(feature = "reflection")]
13#[derive(Clone)]
14pub struct FieldInfo {
15    pub name: String,
16    pub type_id: TypeId,
17    pub offset: usize,
18}
19
20#[cfg(feature = "reflection")]
21#[derive(Clone)]
22pub struct MethodInfo {
23    pub name: String,
24    pub signature: String,
25}
26
27#[cfg(feature = "reflection")]
28pub trait Reflect: crate::traits::Class {
29    fn get_field(&self, name: &str) -> Option<&dyn Any>;
30    fn get_field_mut(&mut self, name: &str) -> Option<&mut dyn Any>;
31    fn set_field(&mut self, name: &str, value: Box<dyn Any>) -> Result<(), DynastyError>;
32}
33
34#[cfg(feature = "reflection")]
35impl ReflectionData {
36    pub fn new<T: 'static>() -> Self {
37        Self {
38            fields: HashMap::new(),
39            methods: HashMap::new(),
40        }
41    }
42
43    pub fn add_field(&mut self, name: &str, type_id: TypeId, offset: usize) {
44        self.fields.insert(
45            name.to_string(),
46            FieldInfo {
47                name: name.to_string(),
48                type_id,
49                offset,
50            },
51        );
52    }
53
54    pub fn add_method(&mut self, name: &str, signature: &str) {
55        self.methods.insert(
56            name.to_string(),
57            MethodInfo {
58                name: name.to_string(),
59                signature: signature.to_string(),
60            },
61        );
62    }
63}