Unconstrained type parameter when using generic trait

This can be solved with one more level of indirection, by forcing functions to expose their return type as an associated type through a custom trait.

The essence is defining FnAssoc, implementing it for functions and MapToInt, then implementing all parsers in terms of this trait using a blanket impl:

trait FnAssoc<In> {
    type Out;

    fn do_call(self, input: In) -> Self::Out;
}

impl<In, Out, F> FnAssoc<In> for F
where
    F: FnOnce(In) -> Out
{
    type Out = Out;

    fn do_call(self, input: In) -> Out {
        self(input)
    }
}
1 Like