Невозможно создать обработчик Iron, так как связанный трейт std::ops::Fn‹(&mut iron::Request)› не выполняется

Я пытаюсь создать обработчик запросов железа:

extern crate iron;
extern crate mount;

use iron::{Iron, Request, Response, IronResult, status};
use mount::Mount;
use iron::middleware::Handler;

struct Server {
    message: String
}

impl Server {
    pub fn start(&self){
        let mut mount = Mount::new();
        mount.mount("/", &self);
        Iron::new(mount).http("0.0.0.0:3000").unwrap();
    }
}

impl Handler for Server {
    fn handle(&self, _req: &mut Request) -> IronResult<Response>{
        Ok(Response::with((status::Ok, self.message)))
    }
}

fn main() {
    Server{message: "test".to_string()}.start();
}

но ответ компилятора:

error[E0277]: the trait bound `for<'r, 'r, 'r> Server: std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>` is not satisfied
  --> src/main.rs:15:15
   |
15 |         mount.mount("/", &self);
   |               ^^^^^ trait `for<'r, 'r, 'r> Server: std::ops::Fn<(&'r mut iron::Request<'r, 'r>,)>` not satisfied
   |
   = note: required because of the requirements on the impl of `std::ops::FnOnce<(&mut iron::Request<'_, '_>,)>` for `&Server`
   = note: required because of the requirements on the impl of `iron::Handler` for `&&Server`

Я не мог понять, что говорит мне Rust.


person TlmaK0    schedule 01.12.2016    source источник
comment
stackoverflow.com/q/34489422 имеет ту же ошибку   -  person Shepmaster    schedule 02.12.2016
comment
Как и stackoverflow.com/q/38659008.   -  person Shepmaster    schedule 02.12.2016
comment
Вам следует перейти на новейшую версию Rust, в которой улучшены сообщения об ошибках и формат сообщений об ошибках. Я обновил ваше сообщение об ошибке до более нового формата, потому что нам также легче читать здесь.   -  person Shepmaster    schedule 02.12.2016


Ответы (1)


Вот репродукция вашего вопроса; Вы можете определить проблему?

trait Foo {}

struct Bar;

impl Foo for Bar {}

impl Bar {
    fn thing(&self) {
        requires_bar(self);
    }
}

fn requires_bar<F>(foo: F) where F: Foo {}

fn main() {}

Сдаться?

Вы реализовали трейт для своей структуры:

impl Handler for Server

Но затем пытаетесь использовать ссылку на ссылку на вашу структуру, которая не реализует трейт:

pub fn start(&self) {
    // ...
    mount.mount("/", &self);
    // ...
}

Так что это не может работать. Вам нужно реструктурировать свой код или реализовать трейт для ссылки на вашу структуру.

person Shepmaster    schedule 02.12.2016
comment
Спасибо @Шепмастер - person TlmaK0; 03.12.2016