Unconstrained type parameter in trait impl for Fn

Viewed 393

I'm experimenting with impls of my own traits for closures; my trait has an associated type for the closure's output.

This works fine, with the associated type in Fn's return position:

trait MyFn<Input> {
    type State;

    fn call(&self, state: &mut Self::State, input: Input);
}

impl<Input, State, F> MyFn<Input> for F where F: Fn(Input) -> State {
    type State = State;

    fn call(&self, state: &mut State, input: Input) {
        *state = self(input);
    }
}

But I can't get the same associated type to work in the Fn's parameter position:

impl<Input, State, F> MyFn<Input> for F where F: Fn(&mut State, Input) {
    type State = State;

    fn call(&self, state: &mut State, input: Input) {
        self(state, input)
    }
}

https://play.rust-lang.org/?gist=dfbb1a258775d1ac2f92235cc2610cc7&version=stable&mode=debug&edition=2015

error[E0207]: the type parameter `State` is not constrained by the impl trait, self type, or predicates
  --> src/lib.rs:15:13
   |
15 | impl<Input, State, F> MyFn<Input> for F where F: Fn(&mut State, Input) {
   |             ^^^^^ unconstrained type parameter

Surely the type parameter State is constrained by the where predicate?

0 Answers
Related