2

我一直在尝试以下

显示的相关导入和代码

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

我一直试图通过传递对句柄方法的引用来避免额外的 Arc 克隆,但似乎无法解决这个问题。避免 handle() 上的生命周期在要求静态生命周期的期货方面得到不同的错误。

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

4

1 回答 1

0

an 的全部意义Arc在于它计算有多少引用,这在克隆时发生。传递对 a 的引用会Arc破坏这一点。

与其传递引用,不如传递Arc自身。所以handle的签名变成:

fn handle<'a>(
    req: Request<Body>,
    challenge: Arc<Mutex<ChallengeState>>,
) -> BoxFut<'a>

从闭包传递Arcby 引用是不可能的,因为您将引用立即超出范围的东西。相反,移动Arcinto handle

let listener_service = move || {
    service_fn(move |req: Request<Body>| handle(req, challenge))
}; 
于 2019-03-14T14:13:55.660 回答