将可变Arc引用传递给hyper service_fn处理程序的问题

时间:2019-03-14 14:04:11

标签: rust future smart-pointers rust-tokio hyper

我一直在尝试以下

相关的导入和显示的代码

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()上存在生命期,这与期货要求静态生命期的错误不同。

仅使用相关内容@ https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=10ea31450e88a122455006760d7fcdd1

更新了代码

1 个答案:

答案 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))
};