TedDriggs / darling

A Rust proc-macro attribute parser
MIT License
1.02k stars 66 forks source link

Using #[darling(with = ...)] should somehow work for fields whose types don't impl FromMeta #305

Open TedDriggs opened 2 months ago

TedDriggs commented 2 months ago

Right now, the following code doesn't work:

fn demo(meta: &syn::Meta) -> darling::Result<Vec<usize>> {
    // doesn't matter
}

#[derive(FromDeriveInput)]
pub struct Example {
    #[darling(with = demo)]
    field: Vec<usize>
}

This fails to compile because Vec<usize> does not impl FromMeta.

The problem is the generated code for handling a missing field, which attempts to call <Vec<usize> as FromMeta>::from_none; that doesn't exist, and so the code doesn't compile. Adding a default for the field or struct works fine, but that shouldn't be required to make this work.