【发布时间】:2019-05-08 03:54:06
【问题描述】:
我正在努力获得与 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 {
| ^^^
【问题讨论】:
-
我希望能够在不同的路由上重复使用控制器——你希望持久数据在所有这些路由之间共享,还是每个路由都共享?路由有自己独特的持久数据?
-
我希望在所有这些路由中全局共享持久数据。