Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use captured lifetime token in function parameter

Here is the code:

struct A<'a, 'b> {
    s1: &'a str,
    s2: &'b str,
}

struct B<'a> {
    s3: &'a str,
}

macro_rules! gen_fn {
    ( $name:ident, $param:ty, < $($gen:tt),+ > ) => {
        fn $name< $($gen),+ >(param: $param< $($gen),+ >) {
            todo!()
        }
    }
}

fn main() {
    gen_fn!( op_a, A, <'a, 'b>);
    gen_fn!( op_b, B, <'a> );
}

However this code can’t compile because Rust consider $param as a single argument, and $gen is another argument, so it expect token , or ).

When I try to expand the macro, the code is generated as I expect:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fn op_a<'a, 'b>(param: A<'a, 'b>) {
    $crate::panicking::panic("not yet implemented")
}
fn op_b<'a>(param: B<'a>) {
    $crate::panicking::panic("not yet implemented")
}

Am I doing wrong or it is just unsupported.

>Solution :

You can use ident instead of ty, but only if you’re not using paths (a::b):

macro_rules! gen_fn {
    ( $name:ident, $param:ident, < $($gen:tt),+ > ) => {
        fn $name< $($gen),+ >(param: $param< $($gen),+ >) {
            todo!()
        }
    }
}

If you’re using paths, you can use a repeated ident:

macro_rules! gen_fn {
    ( $name:ident, $($param:ident)::+, < $($gen:tt),+ > ) => {
        fn $name< $($gen),+ >(param: $($param)::+< $($gen),+ >) {
            todo!()
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading