Open makorne opened 1 month ago
Something like this would work: deserialized.iter_mut().filter_map(AsVariantMut::<Items>::as_variant_mut)
I'll try add that later!
Looks like .iter_mut() does not exist
deserialized.iter_mut().filter_map(AsVariantMut::<Player>::as_variant_mut);
^^^^^^^^ method not found in `Enum`
I misinterpreted the code as Vec<Enum>
. Correct is something like for item in enum.downcast_mut::<Items>().iter_mut().map(|items| &mut items.vec).flatten()
.
That does not work too.
413 | enum Enum {
| --------- method `downcast_mut` not found for this enum
...
461 | deserialized.downcast_mut::<Item>().iter_mut().flatten();
| ^^^^^^^^^^^^
|
help: there is a method `enum_downcast_mut` with a similar name
|
461 | deserialized.enum_downcast_mut::<Item>().iter_mut().flatten();
Items:
error[E0412]: cannot find type `Items` in this scope
--> src/main.rs:462:50
|
410 | struct Item(String);
| -------------------- similarly named struct `Item` defined here
...
462 | let found = deserialized.enum_downcast_mut::<Items>().iter_mut().flatten();
| ^^^^^
|
help: there is an enum variant `crate::Enum::Items`; try using the variant's enum
|
462 | let found = deserialized.enum_downcast_mut::<crate::Enum>().iter_mut().flatten();
| ~~~~~~~~~~~
help: a struct with a similar name exists
|
462 | let found = deserialized.enum_downcast_mut::<Item>().iter_mut().flatten();
Item:
deserialized.enum_downcast_mut::<Item>().iter_mut().flatten();
| ^^^^^^^^^^^^^^^^^ the trait `AsVariantMut<Item>` is not implemented for `Enum`
|
= help: the following other types implement trait `AsVariantMut<T>`:
<Enum as AsVariantMut<Enemy>>
<Enum as AsVariantMut<Player>>
<Enum as AsVariantMut<Vec<Item>>>
note: required by a bound in `enum_downcast_mut`
Sorry my example was wrong.
I've added the following. Can you try that for loop with enum_downcast_mut
instead of `enum_downcast_ref?
https://github.com/ryo33/enum_downcast/blob/main/examples/basic.rs#L56-L62
enum_downcast_mut works.
But loop over Items only. How to loop over Player and Enemy too?
Hi! Thank you for your great crates!!
Please add to end of your serde example how to iter over deserialized Enum and change values of structs. Something like this dummy-code: