我该如何将一个生命周期非静态的对象传递给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
,其中包含使用运行时参数初始化的装箱闭包。这些参数包括密码、密钥和秘密等内容。实际代码是开源的,可以在此处找到。它还在进行中,因此可能会发生变化,并且并非完全最新,但希望可以为最终目标提供一些想法。
manage()
的签名明确指出了Send + Sync + 'static
。你可以尝试修改foo()
以接受bar: Bar<'static>
而不是通用生命周期,并从那里开始逐步解决问题。只要不捕获非静态生命周期,那么闭包应该是'static
的。 - user2722968