【发布时间】:2019-09-19 07:39:01
【问题描述】:
如何将具有非静态生命周期的对象传递给 Rocket 的 manage?目前我有这些方面的东西:
fn foo<'a>(bar: Bar<'a>) -> Result<(), Error> {
rocket::ignite()
.manage(bar)
.mount("/", routes![index])
.launch();
Ok(())
}
但我收到以下错误:
cannot infer an appropriate lifetime due to conflicting requirements
note: ...so that the expression is assignable:
expected bar::Bar<'_>
found bar::Bar<'a>
note: but, the lifetime must be valid for the static lifetime...
为了添加更多上下文,Bar 是一个 struct,包含使用运行时参数初始化的盒装闭包。 args 包含密码、密钥和机密等内容 - 实际代码是开源的,因此可以在 here 找到。它是 WIP,因此会发生变化,并且不是完全最新的,但希望为最终目标提供一个想法。
【问题讨论】:
-
您不能使用非静态生命周期,因为
manage()的签名字面意思是Send + Sync + 'static。您可以尝试更改您的foo()以采用bar: Bar<'static>而不是通用生命周期,然后从那里开始工作。盒装闭包应该是'static,只要它们不捕获非静态生命周期。 -
感谢您的回复。如果这是答案,可以将其发布为答案吗?不幸的是,这似乎是有限的,你知道为什么它被明确声明为
static吗?
标签: rust rust-rocket