Постоянное состояние железа со структурной ссылкой - PullRequest
0 голосов
/ 06 мая 2019

Я изо всех сил пытаюсь получить постоянную ссылку, работая с 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 {
   |      ^^^

Ответы [ 2 ]

1 голос
/ 07 мая 2019

Как говорится в сообщении об ошибке:

но параметр времени жизни должен пережить статическое время жизни

Это потому, что Key использует 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();
    }
}

Я хочу, чтобы постоянные данные были глобально распределены по всем этим маршрутам

В этом случае я бы полностью отказался от использования постоянного ящика и просто создал бы синглтон:

0 голосов
/ 06 мая 2019

Вам необходимо добавить явную привязку времени жизни к вашему блоку 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, компилятор должен иметь возможность определять его из контекста.

...