我正在试验我自己的特征是否有关闭;我的特征有一个关闭输出的关联类型 .
这很好用,在 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
谓词的约束?