#Is there a way to access the currently set `DynamicVariant` of a `DynamicEnum`?

21 messages · Page 1 of 1 (latest)

safe axle
#

I see that there are methods to get the variant type, indes, and name. I see a method to replace the variant with another one, but I don't see how to inspect the current variant and modify it in place.

sinful shore
#

you can set via index, and there's some way to get the index from the name

safe axle
sinful shore
#

more specific?

#

why does it need to be in-place?

safe axle
#
let mut dynamic_enum = get_a_dynamic_enum_from_somewhere();
let variant = dynamic_enum.variant_mut(); // this function is not real
match variant {
  DynamicVariant::Struct(dynamic_struct) => todo!(),
  _ => ()
}
#

then i modify fields of the struct

#

it needs to be in place because it already has data in it that i don't want to lose

#

my use case is that i use pyo3 to convert between python objects and bevy reflect

sinful shore
#

<DynamicEnum as Enum>::field_at_mut?

safe axle
#

yes, that's what i was looking for, thank you so much!

#

wait, hold on

#

that gets a Option<&dyn PartialReflect>, it returns the value of a field in the current variant

#

it doesn't say which variant, and it returns None if the the current variant is not a tuple

sinful shore
#

there's also variant_name, variant_index, and variant_type

sinful shore
#

non-tuple structs are given indices too

#

returns None iff index >= length iirc

safe axle
#

I see, I was hoping for a way to get the actual variant that is there

#

i will open an issue on github proposing new methods that do that

#

thanks for your time