我有一个方法,根据谓词的不同,它会返回一个未来或另一个。换句话说,一个if-else表达式返回一个future:
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]>`
期货的创建方式不同,可能包含闭包,因此它们的类型不相等。理想情况下,该解决方案将不使用Box
es,因为我的异步逻辑的其余部分都不使用它们。
期货中的if-else逻辑通常如何完成?
答案 0 :(得分:14)
Either
使用futures::future::Either
没有额外的堆分配:
extern crate futures; // 0.1.23
use futures::{
future::{self, Either},
Future,
};
fn f() -> impl Future<Item = usize, Error = ()> {
if 1 > 0 {
Either::A(future::ok(2).map(|x| x))
} else {
Either::B(future::ok(10).and_then(|x| future::ok(x + 2)))
}
}
但是,这需要固定的堆栈分配。如果A
占用1个字节并在99%的时间内发生,但是B
占用512个字节,那么您的Either
将始终占用512个字节(加上一些)。这并不总是胜利。
extern crate futures; // 0.1.23
use futures::{future, Future};
fn f() -> Box<Future<Item = usize, Error = ()>> {
if 1 > 0 {
Box::new(future::ok(2).map(|x| x))
} else {
Box::new(future::ok(10).and_then(|x| future::ok(x + 2)))
}
}
作为Matthieu M. points out,可以将两种解决方案结合起来:
我会注意到,对于大
的极少数情况下为堆分配付费B
:Either(A, Box<B>)
的情况,有一个中间解决方案。这样,您仅需在B
请注意,如果您有两个以上的条件(Either
; Either<A, Either<B, C>>
等),您也可以堆叠Either<Either<A, B>, Either<C, D>>
:
另请参阅: