将非静态生命周期传递给 Rocket 的管理
Passing a non-static lifetime to Rocket's manage
如何将具有非静态生命周期的对象传递给 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
。原因在 documentation for State:
中说明
The type being managed must be thread safe and sendable across thread boundaries. In other words, it must implement Send + Sync + 'static.
也就是说,由于(工作)线程可以随时访问托管状态,并且由于无法保证这些线程何时退出,因此托管状态必须至少与整个程序一样长;那是 'static
。
您可以尝试改变您的 foo()
以 bar: Bar<'static>
而不是一般的生命周期,然后从那里开始。 'static
的要求通常并不像听起来那么糟糕,因为所有拥有的值(如 String::new()
)都是 'static
,只要它们不包含对其他事物的引用。
如果您不能提供 Bar<'static>
,您可以使用 Arc
而不是普通引用,这样 Bar<'a>
就变成了 Bar
。这里的基本原理是 Bar
持有原子计数的引用而不是引用,所以持有 Bar
保证所有成员都活着,而 Bar
是活着的。这使得 Bar
'static
。
作为旁注:在考虑 'static
时,某些类型的要求 'static
确实 而不是 意味着事实上,价值确实永远存在。这只是意味着价值可以永生。在您的情况下,State
无法强制其他线程 not 退出并销毁它们的值。因此 State
必须保证它所操作的所有值都可以在 State
想要的时间内存活。仅当这些值在线程边界处为 'static
时才会如此。
如何将具有非静态生命周期的对象传递给 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
。原因在 documentation for State:
The type being managed must be thread safe and sendable across thread boundaries. In other words, it must implement Send + Sync + 'static.
也就是说,由于(工作)线程可以随时访问托管状态,并且由于无法保证这些线程何时退出,因此托管状态必须至少与整个程序一样长;那是 'static
。
您可以尝试改变您的 foo()
以 bar: Bar<'static>
而不是一般的生命周期,然后从那里开始。 'static
的要求通常并不像听起来那么糟糕,因为所有拥有的值(如 String::new()
)都是 'static
,只要它们不包含对其他事物的引用。
如果您不能提供 Bar<'static>
,您可以使用 Arc
而不是普通引用,这样 Bar<'a>
就变成了 Bar
。这里的基本原理是 Bar
持有原子计数的引用而不是引用,所以持有 Bar
保证所有成员都活着,而 Bar
是活着的。这使得 Bar
'static
。
作为旁注:在考虑 'static
时,某些类型的要求 'static
确实 而不是 意味着事实上,价值确实永远存在。这只是意味着价值可以永生。在您的情况下,State
无法强制其他线程 not 退出并销毁它们的值。因此 State
必须保证它所操作的所有值都可以在 State
想要的时间内存活。仅当这些值在线程边界处为 'static
时才会如此。