Let's say I have the following traits, where I have a bunch of concrete implementations. For each concrete TokenParser there is only ever one concrete TokenHolder for it.
trait Token {}
trait TokenHolder<T: Token> {}
trait TokenParser<H: TokenHolder<T>, T: Token> {}
I have a bunch of code that consumes TokenParser and would like to cut down on the amount of boilerplate that I have. I feel like there should be a way of doing that like
trait TokenParser<H: TokenHolder<T: Token>> {}
// or
trait TokenParser<H: TokenHolder<T>> where T: Token {}
CodePudding user response:
This can't really be achieved using generics but can be used with an associated type within TokenHolder
trait TokenHolder {
type Token: Token;
}
trait TokenParser<H: TokenHolder> {
// This signature demonstrates using the type on the holder
// in the signature for the parser.
fn extract_token(holder: H) -> H::Token;
}
Updated
Updating to use official term based on Chayim Friedman's comment.