Fn的特征隐含式中的无约束类型参数

时间:2018-09-20 14:54:31

标签: rust

我正在尝试用自己特质的隐式符号进行封闭;我的特征具有与闭包输出相关的类型。

这很好用,并且关联类型位于Fn的返回位置:

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);
    }
}

但是我无法在Fn的参数位置使用相同的关联类型:

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

类型参数State是否受where谓词约束?

0 个答案:

没有答案