Type alias and impl


I'm using nom to write parsers and have a bunch of parsers with signatures like these:

use nom::Parser;
pub(crate) fn identifier<'a>() -> impl Parser<&'a str, Expression, VerboseError<&'a str>> {

All parsers have similar return types. The only thing that varies is the Expression part. So, I want to use a type alias like this:

type MParser<'a, O> = Parser<&'a str, O, VerboseError<&'a str>>;

But I can't figure out how to rewrite the functions using this alias. If I include impl in the alias, I get an error "impl Trait in type aliases is unstable", and if I try to write the impl in front of the alias in the function definition, it complains that impl cannot be followed by an alias. Without impl, it fails because it is not Sized. Is there a way? Thanks!

Best, Oliver

The unstable trait_alias feature provides a simple solution for this.

For now, on stable Rust, you can create a new empty trait with a blanket implementation like this:

trait MParser<'a, O>: Parser<&'a str, O, VerboseError<&'a str>> {}

impl<'a, T, O> MParser<'a, O> for T where T: Parser<&'a str, O, VerboseError<&'a str>> {}

Awesome, thanks, that works (the empty trait, that is)!