我一直在尝试以下
相关的导入和显示的代码
use std::sync::{Arc, Mutex};
use std::thread;
use hyper::rt::{self, Future, Stream};
use hyper::service::service_fn;
use hyper::{Body, Request, Response, Server, StatusCode};
pub struct ChallengeState;
pub struct ChallengeResponse;
type BoxFut<'a> = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send + 'a>;
fn handle_challengeproof<'a>(
req: Request<Body>,
challenge: &Arc<Mutex<ChallengeState>>,
) -> BoxFut<'a> {
let resp = req.into_body().concat2().map(move |body| {
let challenge_lock = challenge.lock().unwrap();
Response::builder()
.status(StatusCode::OK)
.body(Body::from("test"))
.unwrap()
});
Box::new(resp)
}
fn handle<'a>(
req: Request<Body>,
challenge: &Arc<Mutex<ChallengeState>>,
) -> BoxFut<'a> {
handle_challengeproof(req, challenge)
}
pub fn run_listener(
challenge: Arc<Mutex<ChallengeState>>,
) -> thread::JoinHandle<()> {
let addr = ([127, 0, 0, 1], 9999).into();
let listener_service = move || {
let challenge = Arc::clone(&challenge);
service_fn(move |req: Request<Body>| {
handle(req, &challenge)
})
};
let server = Server::bind(&addr)
.serve(listener_service)
.map_err(|_| () );
thread::spawn(move || {
rt::run(server);
})
}
我一直在尝试通过传递对handle方法的引用来避免额外的Arc克隆,但似乎无法解决这个问题。避免handle()上存在生命期,这与期货要求静态生命期的错误不同。
更新了代码答案 0 :(得分:0)
Arc
的全部要点是,它计算有多少引用,这是在克隆时发生的。传递对Arc
的引用将使这一点失效。
传递Arc
本身,而不是传递引用。因此handle
的签名变为:
fn handle<'a>(
req: Request<Body>,
challenge: Arc<Mutex<ChallengeState>>,
) -> BoxFut<'a>
无法通过闭包的引用传递Arc
,因为您将引用立即超出范围的内容。而是将Arc
移动到handle
上:
let listener_service = move || {
service_fn(move |req: Request<Body>| handle(req, challenge))
};