gpt4 book ai didi

rust - 如何返回带有 `&self` 的 future 组合器

转载 作者:行者123 更新时间:2023-11-29 07:53:34 24 4
gpt4 key购买 nike

我有一段使用 futures v0.1 的代码:

impl ArcService for (Box<MiddleWare<Request>>, Box<ArcService>) {
fn call(&self, req: Request, res: Response) -> Box<Future<Item = Response, Error = Error>> {
box self.0.call(req).and_then(move |req| self.1.call(req, res))
}
}

pub trait ArcService: Send + Sync {
fn call(&self, req: Request, res: Response) -> Box<Future<Item = Response, Error = Error>>;
}

pub trait MiddleWare<T>: Sync + Send {
fn call<'a>(&'a self, param: T) -> Box<Future<Item = T, Error = Error> + 'a>;
}

type MiddleWareFuture<'a, I> = Box<Future<Item = I, Error = Error> + 'a>;

impl MiddleWare<Request> for Vec<Box<MiddleWare<Request>>> {
fn call(&self, request: Request) -> MiddleWareFuture<Request> {
self.iter()
.fold(box Ok(request).into_future(), |request, middleware| {
box request.and_then(move |req| middleware.call(req))
})
}
}

pub struct ArcRouter {
routes: HashMap<Method, Box<ArcService>>,
}

// Service implementation
impl hyper::Server::Service for ArcRouter {
type Response = Response;
type Request = Request;
type Error = hyper::Error;
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;

fn call(&self, req: Request) -> Box<Future<Item = Self::Response, Error = Self::Error>> {
if let Some(routeMatch) = self.matchRoute(req.path(), req.method()) {
let mut request: ArcRequest = req.into();
request.paramsMap.insert(routeMatch.params);
let response = routeMatch.handler //handler is ArcService
.call(request, ArcResponse::new())
.map(|res| res.into());
return box response;
}

// TODO: this should be handled by a user defined 404 handler
return box Ok(Response::new().with_status(StatusCode::NotFound)).into_future();
}
}

注意 Middleware 上的生命周期参数— 它用于避免生命周期问题。

这不会编译,因为 Box<Future<Item = Response, Error = Error>>隐式为 'static因此会导致生命周期问题。 hyper::Server::Service需要 'static Future

这是一个恰本地描述了我的问题的例子:

extern crate futures; // v0.1 (old)

use futures::{future, Future};

struct Example {
age: i32,
}

// trait is defined in an external crate. You can't change it's definition
trait MakeFuture {
fn make_a_future(&self) -> Box<Future<Item = i32, Error = ()>>;
}

impl MakeFuture for Example {
fn make_a_future(&self) -> Box<Future<Item = i32, Error = ()>> {
let f = future::ok(self).map(|ex| ex.age + 1);
Box::new(f)
}
}

playground link

它给出了生命周期错误:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:16:28
|
16 | let f = future::ok(self).map(|ex| ex.age + 1);
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 15:5...
--> src/main.rs:15:5
|
15 | / fn make_a_future(&self) -> Box<Future<Item = i32, Error = ()>> {
16 | | let f = future::ok(self).map(|ex| ex.age + 1);
17 | | Box::new(f)
18 | | }
| |_____^
note: ...so that expression is assignable (expected &Example, found &Example)
--> src/main.rs:16:28
|
16 | let f = future::ok(self).map(|ex| ex.age + 1);
| ^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that expression is assignable (expected std::boxed::Box<futures::Future<Item=i32, Error=()> + 'static>, found std::boxed::Box<futures::Future<Item=i32, Error=()>>)
--> src/main.rs:17:9
|
17 | Box::new(f)
| ^^^^^^^^^^^

有没有办法解决这个问题?我正在构建 hyper::Service并使用 Rust v1.25.0(每晚)

最佳答案

How to return a future combinator with &self

您返回一个指向 self 的 future 像这样:

use futures::future::{self, FutureResult}; // 0.1.28

struct Example {
age: i32,
}

impl Example {
fn make_a_future(&self) -> FutureResult<&Example, ()> {
future::ok(self)
}
}

正如在 Tokio documentation on returning futures 中讨论的那样,返回复杂 future 的最简单稳定的解决方案是 impl Trait。请注意,我们为 self 分配了明确的生命周期并在返回值中使用它(通过 + 'a ):

use futures::{future, Future}; // 0.1.28

struct Example {
age: i32,
}

impl Example {
fn make_a_future<'a>(&'a self) -> impl Future<Item = i32, Error = ()> + 'a {
future::ok(self).map(|ex| ex.age + 1)
}
}

真正的问题是“我怎样才能欺骗编译器并尝试将内存不安全引入我的程序?”

Box<SomeTrait + 'static> (或 Box<SomeTrait> 本身)意味着特征对象不得包含任何不会持续整个程序的引用。根据定义,您的 Example struct 的生命周期比那个短。

这与 future 无关。这是一个基本的 Rust 概念。

有很多关于线程的问题,它们有类似的限制。小样本:

就像在那些情况下一样,您正试图与变量被销毁之后可能存在的东西共享对变量的引用。 C 或 C++ 等语言允许您这样做,但当该变量在被释放后被访问时,您的程序只会在未来看似随机的时间点崩溃。顺便说一句,崩溃是好的情况;信息泄漏或代码执行也是可能的。

与线程的情况一样,您必须确保不会发生这种情况。最简单的方法是将变量移到 future ,而不是共享它。另一种选择是使用类似 Arc 的东西在你的变量周围,克隆 Arc并将克隆体交给 future 。

关于rust - 如何返回带有 `&self` 的 future 组合器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48613350/

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