【发布时间】:2020-07-26 08:20:20
【问题描述】:
我有一个特性,它返回一个附加到它自己生命周期的借用:
trait SomeTrait {
fn do<'a>(&'a self, other: &AnonymousLifetime) -> &'a Output;
}
如何在where clause 中表达相同的限制,以便SomeTrait 可以impl From<Closure>?
示例
场景的minimal, reproducible example (playground):
// The traits
trait Context {
fn give(&self) -> usize;
}
trait ContextDecider {
fn decide<'a>(&'a self, context: &dyn Context) -> &'a str;
}
// A concrete implementation example
// As expected, works OK
struct SomeDecider(Vec<String>);
impl ContextDecider for SomeDecider {
fn decide<'a>(&'a self, context: &dyn Context) -> &'a str {
let some_context = context.give();
if some_context > self.0.len() {
panic!("Oh no!");
}
&self.0[some_context]
}
}
// An implemetation for a closure
// Help here!!
impl<'a, F> ContextDecider for F
where
F: 'a + Fn(&dyn Context) -> &'a str,
{
fn decide<'b>(&'b self, giver: &dyn Context) -> &'b str {
self(giver)
}
}
编译失败:
error[E0312]: lifetime of reference outlives lifetime of borrowed content...
--> src/lib.rs:30:9
|
30 | self(giver)
| ^^^^^^^^^^^
|
note: ...the reference is valid for the lifetime `'b` as defined on the method body at 29:15...
--> src/lib.rs:29:15
|
29 | fn decide<'b>(&'b self, giver: &dyn Context) -> &'b str {
| ^^
note: ...but the borrowed content is only valid for the lifetime `'a` as defined on the impl at 25:6
--> src/lib.rs:25:6
|
25 | impl<'a, F> ContextDecider for F
| ^^
在示例中,我未能在闭包边界中表达特征所施加的限制并且编译器不满意。
编译器并没有帮助我使用什么语法来让我将两个生命周期锁定在一起。
【问题讨论】:
-
看来How does for<> syntax differ from a regular lifetime bound? 的答案可能会回答您的问题。如果没有,请edit您的问题来解释差异。否则,我们可以将此问题标记为已回答。
-
@Shepmaster 链接不是不同的场景吗?例如,将生命周期与 HRTB 绑定我得到this。还是我错过了什么?我知道你真的很擅长将 Rust 问题保持在高水平,所以在我说它们不同之前,我在这里尽我所能应用提供的链接中的答案。但在某种程度上,问题是关于表达和语法,而答案并没有真正帮助。
-
如果我 100% 确定建议的副本已回答了该问题,我会自己关闭它。这更像是试图在我有的几分钟内提供帮助。
-
请注意,即使 this 代码有效,仍然无法使闭包返回对自身的引用:请参阅 Cannot infer an appropriate lifetime for a closure that returns a reference 以获得解释。恐怕您可能必须找到其他解决方案。
标签: rust closures traits lifetime