19

我有一种方法,根据谓词,将返回一个或另一个未来。换句话说,一个返回未来的 if-else 表达式:

extern crate futures; // 0.1.23

use futures::{future, Future};

fn f() -> impl Future<Item = usize, Error = ()> {
    if 1 > 0 {
        future::ok(2).map(|x| x)
    } else {
        future::ok(10).and_then(|x| future::ok(x + 2))
    }
}

这不编译:

error[E0308]: if and else have incompatible types
  --> src/lib.rs:6:5
   |
6  | /     if 1 > 0 {
7  | |         future::ok(2).map(|x| x)
8  | |     } else {
9  | |         future::ok(10).and_then(|x| future::ok(x + 2))
10 | |     }
   | |_____^ expected struct `futures::Map`, found struct `futures::AndThen`
   |
   = note: expected type `futures::Map<futures::FutureResult<{integer}, _>, [closure@src/lib.rs:7:27: 7:32]>`
              found type `futures::AndThen<futures::FutureResult<{integer}, _>, futures::FutureResult<{integer}, _>, [closure@src/lib.rs:9:33: 9:54]>`

期货的创建方式不同,并且可能持有闭包,因此它们的类型不相等。理想情况下,该解决方案不会使用Boxes,因为我的其余异步逻辑不使用它们。

期货中的 if-else 逻辑通常是如何完成的?

4

1 回答 1

28

使用async/await

从 Rust 1.39 开始,您可以使用asyncandawait语法来涵盖大多数情况:

async fn a() -> usize {
    2
}
async fn b() -> usize {
    10
}

async fn f() -> usize {
    if 1 > 0 {
        a().await
    } else {
        b().await + 2
    }
}

也可以看看:

Either

futures::future::Either通过trait使用FutureExt没有额外的堆分配:

use futures::{Future, FutureExt}; // 0.3.5

async fn a() -> usize {
    2
}

async fn b() -> usize {
    10
}

fn f() -> impl Future<Output = usize> {
    if 1 > 0 {
        a().left_future()
    } else {
        b().right_future()
    }
}

但是,这需要固定的堆栈分配。如果A占用 1 个字节并且 99% 的时间发生,但B占用 512 个字节,那么您Either始终占用 512 个字节(加上一些)。这并不总是一场胜利。

此解决方案也适用于Streams。

盒装特征对象

这里我们使用FutureExt::boxed返回一个 trait 对象:

use futures::{Future, FutureExt}; // 0.3.5

async fn a() -> usize {
    2
}

async fn b() -> usize {
    10
}

fn f() -> impl Future<Output = usize> {
    if 1 > 0 {
        a().boxed()
    } else {
        b().boxed()
    }
}

此解决方案也适用于Streams。


正如Matthieu M. 指出的那样,这两种解决方案可以结合起来:

B我会注意到对于大型:的情况有一个中间解决方案Either(A, Box<B>)。这样,您只需在极少数情况下为堆分配付费B

请注意,Either如果您有两个以上的条件(Either<A, Either<B, C>>;Either<Either<A, B>, Either<C, D>>等),您也可以堆叠 s:

use futures::{Future, FutureExt}; // 0.3.5

async fn a() -> i32 {
    2
}

async fn b() -> i32 {
    0
}

async fn c() -> i32 {
    -2
}

fn f(v: i32) -> impl Future<Output = i32> {
    use std::cmp::Ordering;

    match v.cmp(&0) {
        Ordering::Less => a().left_future(),
        Ordering::Equal => b().left_future().right_future(),
        Ordering::Greater => c().right_future().right_future(),
    }
}

也可以看看:

于 2018-08-16T21:52:00.683 回答