gpt4 book ai didi

rust - 期望一个实现 `Fn` 特性的闭包,但是这个闭包只实现了 `FnOnce`

转载 作者:行者123 更新时间:2023-11-29 08:27:02 26 4
gpt4 key购买 nike

我想使用 Hyper 来实现一个网络服务。我从 the hello world example 复制代码它成功了。当我尝试将数据访问对象添加到 HelloWorld 结构时,出现错误,我不知道如何修复它。如何将特征成员添加到 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();
| ^^^

最佳答案

我对 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();
}

关于rust - 期望一个实现 `Fn` 特性的闭包,但是这个闭包只实现了 `FnOnce`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48764839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com