rust_struct_iterable icon indicating copy to clipboard operation
rust_struct_iterable copied to clipboard

Adding `iter_mut` method to Iterable

Open mjhouse opened this issue 2 years ago • 2 comments

I'll have a PR out for this is a minute, but it seems to work. Is there some reason this wasn't already implemented? It seems fairly straightforward.

I added the following method signature to the Iterable trait:

fn iter_mut(&mut self) -> std::vec::IntoIter<(&'static str, &'_ mut dyn std::any::Any)>;

Added another field map in the proc macro:

    let fields_iter_mut = fields.iter().map(|field| {
        let field_ident = &field.ident;
        let field_name = field_ident.as_ref().unwrap().to_string();
        quote! {
            (#field_name, &mut (self.#field_ident) as &mut dyn std::any::Any)
        }
    });

And constructed the new function:

    fn iter_mut<'a>(&'a mut self) -> std::vec::IntoIter<(&'static str, &'a mut dyn std::any::Any)> {
        vec![
            #(#fields_iter_mut),*
        ].into_iter()
    }

mjhouse avatar Aug 26 '23 11:08 mjhouse

Hi Michael, thank you so much for your PR, I'll check as soon as possible. :)

decomoraes avatar Aug 28 '23 19:08 decomoraes

No problem, and no rush.

mjhouse avatar Aug 28 '23 21:08 mjhouse