Generic parameter of syn:Type

Hi all,

I have faced with issue how to get generic parameter for for result of parsed function:

async fn handle_url(url: Url, handled_urls: Arc<Mutex<HashSet<Url>>>)
    -> Result<(), IOError>
{
    // Some logic
    Ok(())
}

And I try to parse this function with the following procedure macro:

    let input = syn::parse_macro_input!(item as syn::ItemFn);
    let attr_args = syn::parse_macro_input!(attrs as syn::AttributeArgs);

    let sig = &input.sig;
    let vis = input.vis;
    let name = &sig.ident;
    let mut args = Vec::new();
    for arg in &sig.inputs {
        args.push(arg);
    }
    let ret = match &sig.output {
        ReturnType::Default => {
            panic!("Used only for Result<?,?>");
        },
        ReturnType::Type(arrow, box_type) => {
            box_type
        }
    };
    match ret.as_ref() {
        Type::Path(type_path) => {
            println!("How to get Result<_,?> ?");
        },
        _ => {
            panic!("Used only for Result<?,?>");
        }
    }
    let body = &input.block;
    let attrs = &input.attrs;

println!("How to get Result<_,?> ?"); shows the place where I cannot figure out the error of Result ...

Any help will be appreciated !!

Following the documentation, we get:

2 Likes

One additional question ... do you know how to get crate where is the Type is defined ?

There is no way to do this, since macros are expanded before type resolution.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.