Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fields.members() for darling::ast::Fields<T> #324

Closed
Bleachfuel opened this issue Jan 28, 2025 · 3 comments
Closed

fields.members() for darling::ast::Fields<T> #324

Bleachfuel opened this issue Jan 28, 2025 · 3 comments

Comments

@Bleachfuel
Copy link

Bleachfuel commented Jan 28, 2025

add a .members() function to darling::ast::Fields<T> that works like syn::Fields::members(). this is useful for not having to match on whether its named, unnamed or a unit struct

@TedDriggs
Copy link
Owner

To do this, we'd need to figure out how one gets from a T to a syn::Member. T is not obliged to preserve the Ident for named fields, so at best we'd need some additional trait bound on the impl block that houses this method.

Could your use-case alternatively be achieved with something like:

fn field_to_member(field: &syn::Field, index: usize) -> syn::Member {
    match field.ident.clone() {
        Some(ident) => ident.into(),
        None => (syn::Index {
            index: index as u32,
            span: field.span(),
        })
        .into(),
    }
}

fn members<'a>(v: &'a Fields<syn::Field>) -> impl 'a + Iterator<Item = syn::Member> {
    v.iter().enumerate().map(|(i, f)| field_to_member(f, i))
}

You can replace syn::Field with the type of your choice, or make it generic over anything that impls AsRef<Ident> and Spanned.

@Bleachfuel
Copy link
Author

Yeah, that's nice!

@TedDriggs
Copy link
Owner

Given the availability of a workaround and the complexities of exposing this as an inherent method on ast::Fields, I'm closing this as "Not planned"

@TedDriggs TedDriggs closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants