From 548cfe53130537dfee33d143e8a563879e30e1a2 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 8 Jul 2026 00:42:44 +0200 Subject: feat(engine): add try_get_field_mut fn to enum variant reflection --- engine-reflection/src/lib.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'engine-reflection') 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>, ) -> 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 Reflection for Option Ok(()) }, + try_get_field_mut: |target, field_index| { + if field_index != 0 { + return Err(EnumVariantGetFieldError::FieldIndexOutOfBounds); + } + + let target = target + .downcast_mut::() + .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 Reflection for Option Ok(()) }, + try_get_field_mut: |_, _| { + Err(EnumVariantGetFieldError::FieldIndexOutOfBounds) + }, }, ], is_unit_only: false, -- cgit v1.2.3-18-g5258