From 989b360fecd025157a82b4d3f2a65d330e85c342 Mon Sep 17 00:00:00 2001 From: person93 Date: Fri, 4 Apr 2025 22:33:00 -0400 Subject: [PATCH] Add accessors to `DynamicEnum` for the `DynamicVariant` (#18693) # Objective - Closes https://github.com/bevyengine/bevy/issues/18692 ## Solution Add the methods as described ```rust impl DynamicEnum { fn variant(&self) -> &DynamicVariant; fn variant_mut(&mut self) -> &mut DynamicVariant; } ``` --- crates/bevy_reflect/src/enums/dynamic_enum.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/bevy_reflect/src/enums/dynamic_enum.rs b/crates/bevy_reflect/src/enums/dynamic_enum.rs index a968b311f2..3380921fbe 100644 --- a/crates/bevy_reflect/src/enums/dynamic_enum.rs +++ b/crates/bevy_reflect/src/enums/dynamic_enum.rs @@ -140,6 +140,22 @@ impl DynamicEnum { self.variant = variant.into(); } + /// Get a reference to the [`DynamicVariant`] contained in `self`. + pub fn variant(&self) -> &DynamicVariant { + &self.variant + } + + /// Get a mutable reference to the [`DynamicVariant`] contained in `self`. + /// + /// Using the mut reference to switch to a different variant will ___not___ update the + /// internal tracking of the variant name and index. + /// + /// If you want to switch variants, prefer one of the setters: + /// [`DynamicEnum::set_variant`] or [`DynamicEnum::set_variant_with_index`]. + pub fn variant_mut(&mut self) -> &mut DynamicVariant { + &mut self.variant + } + /// Create a [`DynamicEnum`] from an existing one. /// /// This is functionally the same as [`DynamicEnum::from_ref`] except it takes an owned value.