ryo33 / enum_downcast

Safe downcasting for enums
3 stars 0 forks source link

How to work with deserialized? #4

Open makorne opened 1 month ago

makorne commented 1 month ago

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:

    let mut deserialized:  Enum  = serde_json::from_str(&serialized).unwrap();
    println!("{:?}", &deserialized);
    for item in deserialized.iter(){
        println!("{:?}", &item);
        item.power = 10;
    }
ryo33 commented 1 month ago

Something like this would work: deserialized.iter_mut().filter_map(AsVariantMut::<Items>::as_variant_mut)

I'll try add that later!

makorne commented 1 month ago

Looks like .iter_mut() does not exist

deserialized.iter_mut().filter_map(AsVariantMut::<Player>::as_variant_mut);
                      ^^^^^^^^ method not found in `Enum`
ryo33 commented 1 month ago

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().

makorne commented 1 month ago

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`
ryo33 commented 1 month ago

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

makorne commented 1 month ago

enum_downcast_mut works.

But loop over Items only. How to loop over Player and Enemy too?