macro_rules! parse_generics {
    (
        $(:: $(@$leading:tt@)? )? $first:ident $(:: $trailing:ident)* ! {$($prefix:tt)*}

        ($($generics:tt)*)
    ) => { ... };
}
Available on crate feature generics_parsing only.
Expand description

Transforms generic parameters for use in type definitions, impl blocks and generic arguments, passing them to a callback macro.

Examples

Basic

Basic example of the syntax this macro expects and passes to a callback macro.

use core_extensions::parse_generics;
 
{
    assert_eq!(hello(), "world")
}
 
// `parse_generics` calls `crate::foo` here
parse_generics! {
    crate::foo!{ 
        // The first tokens passed to the `crate::foo` macro
        hello "world" foo bar 
    }
     
    (
        // The parsed tokens
        'a, T: Foo = A, const N: usize
    )
}
 
#[macro_export]
macro_rules! foo {
    (
        $fn_name:ident $string:literal foo bar

        // generics for use in type/trait declarations
        ('a, T: Foo + = $default_ty:ty, const N: $const_ty0:ty,)

        // generics for use in `impl<...>`, and function`declarations
        ('a, T: Foo +, const N: $const_ty1:ty,)

        // generics for use in generic arguments
        ('a, T, N,)

        // `PhantomData` type that uses all lifetimes and types
        ($phantom:ty)
    ) => {
        fn $fn_name() -> &'static str {
            $string
        }
    };
}