diff options
Diffstat (limited to 'engine-reflection/src/lib.rs')
| -rw-r--r-- | engine-reflection/src/lib.rs | 78 |
1 files changed, 76 insertions, 2 deletions
diff --git a/engine-reflection/src/lib.rs b/engine-reflection/src/lib.rs index c8b0004..4e5e8d8 100644 --- a/engine-reflection/src/lib.rs +++ b/engine-reflection/src/lib.rs @@ -1,5 +1,5 @@ use std::alloc::Layout; -use std::any::{type_name, TypeId}; +use std::any::{type_name, Any, TypeId}; use std::borrow::Cow; use std::fmt::Debug; @@ -64,12 +64,50 @@ impl Type _ => None, } } + + #[inline] + pub fn default_value(&self) -> Option<Box<dyn Any>> + { + match self { + Self::Struct(struct_type) => struct_type.default_value(), + Self::Enum(enum_type) => enum_type.default_value(), + Self::Literal(literal_type) => literal_type.default_value(), + Self::Array(_) | Self::Slice(_) => None, + } + } + + #[inline] + pub fn has_default_value(&self) -> bool + { + match self { + Self::Struct(struct_type) => struct_type.has_default_value(), + Self::Enum(enum_type) => enum_type.has_default_value(), + Self::Literal(literal_type) => literal_type.has_default_value(), + Self::Array(_) | Self::Slice(_) => false, + } + } } #[derive(Debug, Clone)] pub struct Struct { pub fields: &'static [Field], + pub get_default_value: fn() -> Option<DefaultValueFn>, +} + +impl Struct +{ + #[inline] + pub fn default_value(&self) -> Option<Box<dyn Any>> + { + Some((self.get_default_value)()?()) + } + + #[inline] + pub fn has_default_value(&self) -> bool + { + (self.get_default_value)().is_some() + } } #[derive(Debug, Clone)] @@ -83,6 +121,23 @@ pub struct Enum pub discriminant_layout: Layout, pub fields_layout: Option<Layout>, + + pub get_default_value: fn() -> Option<DefaultValueFn>, +} + +impl Enum +{ + #[inline] + pub fn default_value(&self) -> Option<Box<dyn Any>> + { + Some((self.get_default_value)()?()) + } + + #[inline] + pub fn has_default_value(&self) -> bool + { + (self.get_default_value)().is_some() + } } #[derive(Debug, Clone)] @@ -169,6 +224,22 @@ pub struct Literal pub type_id: TypeId, pub ty: LiteralType, pub type_name: fn() -> &'static str, + pub get_default_value: fn() -> Option<DefaultValueFn>, +} + +impl Literal +{ + #[inline] + pub fn default_value(&self) -> Option<Box<dyn Any>> + { + Some((self.get_default_value)()?()) + } + + #[inline] + pub fn has_default_value(&self) -> bool + { + (self.get_default_value)().is_some() + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -219,7 +290,8 @@ macro_rules! impl_reflection_for_literals { layout: Layout::new::<$literal>(), type_id: TypeId::of::<$literal>(), ty: LiteralType::$literal_type, - type_name: || type_name::<$literal>() + type_name: || type_name::<$literal>(), + get_default_value: || Some(|| Box::new(Self::default())) }); } )* @@ -288,3 +360,5 @@ impl<Value: Debug> Debug for FnWithDebug<Value> .finish() } } + +type DefaultValueFn = fn() -> Box<dyn Any>; |
