diff options
| author | HampusM <hampus@hampusmat.com> | 2026-07-08 00:42:44 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-07-08 00:42:44 +0200 |
| commit | 548cfe53130537dfee33d143e8a563879e30e1a2 (patch) | |
| tree | 584804acd078416cd6dac51a6278f8a9cc3a4903 /engine-reflection | |
| parent | 28686c9359da2c2c46d12ae8aaada14ece17246f (diff) | |
feat(engine): add try_get_field_mut fn to enum variant reflection
Diffstat (limited to 'engine-reflection')
| -rw-r--r-- | engine-reflection/src/lib.rs | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/engine-reflection/src/lib.rs b/engine-reflection/src/lib.rs index a31fef0..79a7417 100644 --- a/engine-reflection/src/lib.rs +++ b/engine-reflection/src/lib.rs @@ -168,12 +168,17 @@ pub struct EnumVariant dst: &mut dyn Any, fields: &mut dyn Iterator<Item = Box<dyn Any>>, ) -> Result<(), EnumVariantWriteNewToError>, + + pub try_get_field_mut: fn( + target: &mut dyn Any, + field_index: usize, + ) -> Result<&mut dyn Any, EnumVariantGetFieldError>, } #[derive(Debug, thiserror::Error)] pub enum EnumVariantWriteNewToError { - #[error("Destination must be the same type as the variant's enum")] + #[error("Destination must have the same type as the variant's enum")] WrongDstType, #[error("Too few fields are specified")] @@ -186,6 +191,19 @@ pub enum EnumVariantWriteNewToError WrongFieldType, } +#[derive(Debug, thiserror::Error)] +pub enum EnumVariantGetFieldError +{ + #[error("Target is not the same type as the variant's enum")] + WrongTargetType, + + #[error("Target is not the correct variant")] + WrongTargetVariant, + + #[error("Field index is out of bounds")] + FieldIndexOutOfBounds, +} + #[derive(Debug, Clone)] pub enum EnumVariantFields { @@ -468,6 +486,21 @@ unsafe impl<T: Reflection> Reflection for Option<T> Ok(()) }, + try_get_field_mut: |target, field_index| { + if field_index != 0 { + return Err(EnumVariantGetFieldError::FieldIndexOutOfBounds); + } + + let target = target + .downcast_mut::<Self>() + .ok_or(EnumVariantGetFieldError::WrongTargetType)?; + + let Some(value_field) = target else { + return Err(EnumVariantGetFieldError::WrongTargetVariant); + }; + + Ok(value_field) + }, }, EnumVariant { name: "None", @@ -485,6 +518,9 @@ unsafe impl<T: Reflection> Reflection for Option<T> Ok(()) }, + try_get_field_mut: |_, _| { + Err(EnumVariantGetFieldError::FieldIndexOutOfBounds) + }, }, ], is_unit_only: false, |
