我正在尝试用自己特质的隐式符号进行封闭;我的特征具有与闭包输出相关的类型。
这很好用,并且关联类型位于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)
}
}
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
谓词约束?