[英]How do I implement a trait on a curried function?
如果我尝试为foo
Frob
的函数实现特征Frob
如下:
fn foo<'b>(state: &'b mut i32) -> impl FnMut(&str) -> i32 + 'b {
move |i| *state
}
trait Frob<S, I, O> {
fn frob(self, state: &mut S, input: I) -> O;
}
impl<S, I, O, F, G> Frob<S, I, O> for F
where
F: FnMut(&mut S) -> G,
G: FnMut(I) -> O,
{
fn frob(mut self, state: &mut S, input: I) -> O {
self(state)(input)
}
}
fn bar() {
foo.frob(&mut 1, "hi");
}
我收到错误
error[E0599]: the method `frob` exists for fn item `for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo}`,
but its trait bounds were not satisfied
...
= note: the following trait bounds were not satisfied:
`<for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo} as FnOnce<(&mut _,)>>::Output = _`
which is required by `for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo}: Frob<_, _, _>`
`<&for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo} as FnOnce<(&mut _,)>>::Output = _`
which is required by `&for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo}: Frob<_, _, _>`
`<&mut for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo} as FnOnce<(&mut _,)>>::Output = _`
which is required by `&mut for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo}: Frob<_, _, _>`
首先,我该如何解释这个错误信息? 其次,如何正确编写 trait bound? 据推测,问题与挂在state
上的返回闭包有关,但我找不到指定G
生命周期的地方。
首先,我该如何解释这个错误信息?
是的,这有点神秘,不是吗? 需要认识两件事:
<for<'b> fn(&'b mut i32) -> impl for<'b, 'r> FnMut<(&'r str,)> {foo}
是编译器非常冗长的表达函数类型的方式foo
; 和
对于该函数类型、对该函数类型的共享引用和对该函数类型的可变引用重复相同的注释——当编译器尝试在方法调用语法中自动引用时会发生这种情况,例如在foo.frob(...)
.
因此,我们可以快速将错误消息提炼为:
error[E0599]: the method `frob` exists for fn item `{foo}`, but its trait bounds were not satisfied ... = note: the following trait bounds were not satisfied: `<{foo} as FnOnce<(&mut _,)>>::Output = _` which is required by `{foo}: Frob<_, _, _>`
编译器告诉我们它在{foo}
上找到了一个潜在的frob
方法,但为了使其适用, {foo}
的返回类型必须匹配Frob
特征的约束(但事实并非如此)。
其次,如何正确编写 trait bound? 据推测,问题与挂在
state
上的返回闭包有关,但我找不到指定G
生命周期的地方。
您需要将生命周期约束添加到 trait ( playground ):
trait Frob<'b, S, I, O> { fn frob(self, state: &'b mut S, input: I) -> O; } impl<'b, S: 'b, I, O, F, G> Frob<'b, S, I, O> for F where F: FnMut(&'b mut S) -> G, G: 'b + FnMut(I) -> O, { fn frob(mut self, state: &'b mut S, input: I) -> O { self(state)(input) } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.