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 {}
This can't really be achieved using generics but can be used with a type assigned 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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.