Ожидается закрытие, которое реализует черту `Fn`, но это закрытие только реализует`FnOnce`

Я хочу использовать Hyper для реализации веб-службы. Я скопировал код из примера "Привет, мир", и это удалось. Когда я пытаюсь добавить объект доступа к данным в HelloWorld struct, я получаю ошибку, и я не знаю, как ее исправить. Как добавить признак участника на Hyper-сервер?

extern crate futures;
extern crate hyper;

use futures::future::Future;
use hyper::header::ContentLength;
use hyper::server::{Http, Request, Response, Service};

trait Dao {}

struct MysqlDao;

impl Dao for MysqlDao {}

struct HelloWorld {
    dao: Box<Dao>,
}

const PHRASE: &'static str = "Hello, World!";

impl Service for HelloWorld {
    // boilerplate hooking up hyper's server types
    type Request = Request;
    type Response = Response;
    type Error = hyper::Error;
    // The future representing the eventual Response your call will
    // resolve to. This can change to whatever Future you need.
    type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;

    fn call(&self, _req: Request) -> Self::Future {
        // We're currently ignoring the Request
        // And returning an 'ok' Future, which means it's ready
        // immediately, and build a Response with the 'PHRASE' body.
        Box::new(futures::future::ok(
            Response::new()
                .with_header(ContentLength(PHRASE.len() as u64))
                .with_body(PHRASE),
        ))
    }
}

fn main() {
    let addr = "127.0.0.1:3000".parse().unwrap();
    let dao = Box::new(MysqlDao);
    let server = Http::new().bind(&addr, || Ok(HelloWorld { dao })).unwrap();
    server.run().unwrap();
}

Информация об ошибке:

error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
  --> src/main.rs:44:42
   |
44 |     let server = Http::new().bind(&addr, || Ok(HelloWorld { dao })).unwrap();
   |                              ----        ^^^^^^^^^^^^^^^^^^^^^^^^^
   |                              |
   |                              the requirement to implement `Fn` derives from here
   |
note: closure is `FnOnce` because it moves the variable `dao` out of its environment
  --> src/main.rs:44:61
   |
44 |     let server = Http::new().bind(&addr, || Ok(HelloWorld { dao })).unwrap();
   |                                                             ^^^

1 ответ

Решение

Я внес эти изменения в HelloWorld структура:

struct HelloWorld<'a> {
    dao: &'a Dao,
}

Я также изменил let server заявление для:

let server = Http::new()
    .bind(&addr, move || Ok(HelloWorld { dao: &dao }))
    .unwrap();

Весь код:

extern crate futures;
extern crate hyper;

use futures::future::Future;
use hyper::header::ContentLength;
use hyper::server::{Http, Request, Response, Service};

trait Dao {}

struct MysqlDao;

impl Dao for MysqlDao {}

struct HelloWorld<'a> {
    dao: &'a Dao,
}

const PHRASE: &'static str = "Hello, World!";

impl<'a> Service for HelloWorld<'a> {
    // boilerplate hooking up hyper's server types
    type Request = Request;
    type Response = Response;
    type Error = hyper::Error;
    // The future representing the eventual Response your call will
    // resolve to. This can change to whatever Future you need.
    type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;

    fn call(&self, _req: Request) -> Self::Future {
        // We're currently ignoring the Request
        // And returning an 'ok' Future, which means it's ready
        // immediately, and build a Response with the 'PHRASE' body.
        Box::new(futures::future::ok(
            Response::new()
                .with_header(ContentLength(PHRASE.len() as u64))
                .with_body(PHRASE),
        ))
    }
}

fn main() {
    let addr = "127.0.0.1:3000".parse().unwrap();
    let dao = MysqlDao;
    let server = Http::new()
        .bind(&addr, move || Ok(HelloWorld { dao: &dao }))
        .unwrap();
    server.run().unwrap();
}
Другие вопросы по тегам