diff options
| author | HampusM <hampus@hampusmat.com> | 2026-04-06 18:50:04 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-04-06 18:50:04 +0200 |
| commit | 942df064017258a92eee1a14cd613c6aec983dc8 (patch) | |
| tree | 3ae245635826dccfa9dd1d6a9d064434a2436dc5 /engine/src/reflection.rs | |
| parent | d9dbf1f720099624ab8d9622e8c6170ee03f2dbb (diff) | |
feat(engine): make reflection of reflected struct fields optional
Diffstat (limited to 'engine/src/reflection.rs')
| -rw-r--r-- | engine/src/reflection.rs | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/engine/src/reflection.rs b/engine/src/reflection.rs index 3ce424e..7c5ee5c 100644 --- a/engine/src/reflection.rs +++ b/engine/src/reflection.rs @@ -1,5 +1,6 @@ use std::alloc::Layout; use std::any::{TypeId, type_name}; +use std::fmt::Debug; pub use engine_macros::Reflection; @@ -50,7 +51,15 @@ pub struct StructField pub byte_offset: usize, pub type_id: TypeId, pub type_name: &'static str, - pub reflection: &'static Reflection, + pub get_reflection: FnWithDebug<Option<&'static Reflection>>, +} + +impl StructField +{ + pub fn reflection(&self) -> Option<&'static Reflection> + { + self.get_reflection.get() + } } #[derive(Debug, Clone)] @@ -157,14 +166,32 @@ impl<T: With> With for &'static [T] } } -// Used by the Reflection derive macro -#[doc(hidden)] -pub mod __private +#[derive(Clone)] +pub struct FnWithDebug<Value> { - pub const fn get_type_reflection<T>() -> &'static super::Reflection - where - T: super::With, + func: fn() -> Value, +} + +impl<Value> FnWithDebug<Value> +{ + pub const fn new(func: fn() -> Value) -> Self + { + Self { func } + } + + pub fn get(&self) -> Value + { + (self.func)() + } +} + +impl<Value: Debug> Debug for FnWithDebug<Value> +{ + fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - T::REFLECTION + formatter + .debug_tuple("FnWithDebug") + .field(&self.get()) + .finish() } } |
