Error while creating proc_macro: expcted HasIterator, found ThereIsNoIteratorInRepetition

Hello again!
I started playing with proc_macro and I'm trying to a macro that maps fields of struct A into struct B. If there are optional fields in struct A, it checks that are not None. If are not not are mapped into struct B otherwise an error is returned. I think I'm mostly there but I'm encountering the error in the title
mismatched types expected HasIterator, found ThereIsNoIteratorInRepetition`` and I don't know how to solve it. the error is at let gen = quote! {

use proc_macro::TokenStream;
use quote::quote;
use syn::{self, Data, DeriveInput, Fields, Ident};

#[proc_macro_derive(WopStructs)]
pub fn wop_structs(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_wop_structs(&ast)
}

fn impl_wop_structs(ast: &syn::DeriveInput) -> TokenStream {
    let struct_name = &ast.ident;
    let fields = match &ast.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => &fields.named,
            _ => {
                return quote! {
                    compile_error!("WopStructs only supports structs with named fields.");
                }
                .into();
            }
        },
        _ => {
            return quote! {
                compile_error!("WopStructs only supports named fields in structs.");
            }
            .into();
        }
    };

    let gen = quote! {
        impl #struct_name {
            fn wop(&mut self, other: Self) -> Result<(), &'static str> {
                let other = other;
                #(if let Some(val) = other.#fields {
                    if let Some(ref mut field) = self.#fields {
                        *field = val;
                    } else {
                        return Err("Required field is None");
                    }
                })*
                Ok(())
            }
        }
    };
    gen.into()
}
-&fields.named
+fields.named.iter()

Rust Playground

The error says fields is not a iterator, so make it be.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.