具有结构引用的 Iron 持久状态
Iron persistent state with struct reference
我正在努力获取使用 Iron 的持久参考,但不确定如何设置合适的生命周期。我希望能够在不同的路线上重复使用控制器。
示例:
use iron::prelude::*;
use iron::typemap::Key;
use persistent::Read;
use router::Router;
pub struct Controller;
pub struct Rest {
controller: Controller,
}
impl Key for &Controller {
type Value = Self;
}
impl Rest {
pub fn run(&self) {
let router = Router::new();
let mut chain = Chain::new(router);
chain.link(Read::<&Controller>::both(&self.controller));
Iron::new(chain).http(format!("0.0.0.0:1234")).ok();
}
}
fn main() {
Rest {
controller: Controller,
}
.run();
}
[dependencies]
iron = "0.6.*"
router = "0.6.*"
persistent = "0.4.0"
error[E0478]: lifetime bound not satisfied
--> src/main.rs:12:6
|
12 | impl Key for &Controller {
| ^^^
|
note: lifetime parameter instantiated with the lifetime '_ as defined on the impl at 12:14
--> src/main.rs:12:14
|
12 | impl Key for &Controller {
| ^
= note: but lifetime parameter must outlive the static lifetime
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'_` due to conflicting requirements
--> src/main.rs:12:6
|
12 | impl Key for &Controller {
| ^^^
|
note: first, the lifetime cannot outlive the lifetime '_ as defined on the impl at 12:14...
--> src/main.rs:12:14
|
12 | impl Key for &Controller {
| ^
= note: ...so that the types are compatible:
expected typemap::Key
found typemap::Key
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `&Controller` will meet its required lifetime bounds
--> src/main.rs:12:6
|
12 | impl Key for &Controller {
| ^^^
您需要为 impl
块添加明确的生命周期绑定。否则编译器不知道控制器引用的引用有效性。
这应该可以,但我还没有测试过
pub struct Rest {
controller: controller::Controller,
}
impl<'a> Key for &'a controller::Controller {
type Value = Self;
}
impl Rest {
pub fn run(&self) {
let mut router = Router::new();
let mut chain = Chain::new(router);
chain.link(Read::<&controller::Controller>::both(&self.controller));
Iron::new(chain).http(format!("0.0.0.0:1234"))
}
}
附带说明一下,我认为您不需要在 Read
调用中指定类型,编译器应该能够从上下文中确定它。
如错误消息所述:
but lifetime parameter must outlive the static lifetime
这是因为Key
uses Any
作为超特质,需要'static
:
pub trait Any: 'static {
fn type_id(&self) -> TypeId;
}
最简单的解决方案是为一个值实现Key
,然后将值赋给Read::both
:
impl Key for Controller {
type Value = Self;
}
impl Rest {
pub fn run(self) {
let router = Router::new();
let mut chain = Chain::new(router);
chain.link(Read::<Controller>::both(self.controller));
Iron::new(chain).http(format!("0.0.0.0:1234")).ok();
}
}
I want the persistent data to be globally shared across all of these routes
在那种情况下,我会完全避免使用持久性板条箱而只创建一个单例:
- How do I create a global, mutable singleton?
- How can you make a safe static singleton in Rust?
我正在努力获取使用 Iron 的持久参考,但不确定如何设置合适的生命周期。我希望能够在不同的路线上重复使用控制器。
示例:
use iron::prelude::*;
use iron::typemap::Key;
use persistent::Read;
use router::Router;
pub struct Controller;
pub struct Rest {
controller: Controller,
}
impl Key for &Controller {
type Value = Self;
}
impl Rest {
pub fn run(&self) {
let router = Router::new();
let mut chain = Chain::new(router);
chain.link(Read::<&Controller>::both(&self.controller));
Iron::new(chain).http(format!("0.0.0.0:1234")).ok();
}
}
fn main() {
Rest {
controller: Controller,
}
.run();
}
[dependencies]
iron = "0.6.*"
router = "0.6.*"
persistent = "0.4.0"
error[E0478]: lifetime bound not satisfied
--> src/main.rs:12:6
|
12 | impl Key for &Controller {
| ^^^
|
note: lifetime parameter instantiated with the lifetime '_ as defined on the impl at 12:14
--> src/main.rs:12:14
|
12 | impl Key for &Controller {
| ^
= note: but lifetime parameter must outlive the static lifetime
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'_` due to conflicting requirements
--> src/main.rs:12:6
|
12 | impl Key for &Controller {
| ^^^
|
note: first, the lifetime cannot outlive the lifetime '_ as defined on the impl at 12:14...
--> src/main.rs:12:14
|
12 | impl Key for &Controller {
| ^
= note: ...so that the types are compatible:
expected typemap::Key
found typemap::Key
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `&Controller` will meet its required lifetime bounds
--> src/main.rs:12:6
|
12 | impl Key for &Controller {
| ^^^
您需要为 impl
块添加明确的生命周期绑定。否则编译器不知道控制器引用的引用有效性。
这应该可以,但我还没有测试过
pub struct Rest {
controller: controller::Controller,
}
impl<'a> Key for &'a controller::Controller {
type Value = Self;
}
impl Rest {
pub fn run(&self) {
let mut router = Router::new();
let mut chain = Chain::new(router);
chain.link(Read::<&controller::Controller>::both(&self.controller));
Iron::new(chain).http(format!("0.0.0.0:1234"))
}
}
附带说明一下,我认为您不需要在 Read
调用中指定类型,编译器应该能够从上下文中确定它。
如错误消息所述:
but lifetime parameter must outlive the static lifetime
这是因为Key
uses Any
作为超特质,需要'static
:
pub trait Any: 'static {
fn type_id(&self) -> TypeId;
}
最简单的解决方案是为一个值实现Key
,然后将值赋给Read::both
:
impl Key for Controller {
type Value = Self;
}
impl Rest {
pub fn run(self) {
let router = Router::new();
let mut chain = Chain::new(router);
chain.link(Read::<Controller>::both(self.controller));
Iron::new(chain).http(format!("0.0.0.0:1234")).ok();
}
}
I want the persistent data to be globally shared across all of these routes
在那种情况下,我会完全避免使用持久性板条箱而只创建一个单例:
- How do I create a global, mutable singleton?
- How can you make a safe static singleton in Rust?