【发布时间】:2021-02-18 06:16:33
【问题描述】:
下面的函数,取自here:
fn connection_for(
&self,
pool_key: PoolKey,
) -> impl Future<Output = Result<Pooled<PoolClient<B>>, ClientError<B>>> {
let checkout = self.pool.checkout(pool_key.clone());
let connect = self.connect_to(pool_key);
let executor = self.conn_builder.exec.clone();
// The order of the `select` is depended on below...
future::select(checkout, connect).then(move |either| match either {
...
应该返回一个Future。但是,它返回的返回结果是
future::select(checkout, connect).then(...)
.then 在哪里这样做:
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
这不是Future。这怎么可能?
我试图了解此函数返回的内容。这是'.then'的结尾:
Either::Right((Err(err), checkout)) => Either::Right(Either::Right({
if err.is_canceled() {
Either::Left(checkout.map_err(ClientError::Normal))
} else {
Either::Right(future::err(ClientError::Normal(err)))
}
})),
看起来它返回了 Either::Right(Either::Right 的东西。我很困惑。
【问题讨论】:
-
我很困惑,
Thenis aFuture -
@kmdreko 好的,所以
.then(f: F)接受f: ()->Future。这是否会使 ` Either::Right(Either::Right({...` 被类型推断转换为未来? -
Eitheris aFutureif itsAandBare。看起来他们正在使用Eithers 将所有可能运行的期货(总共看起来像 6 个)组合成一个类型。 -
@kmdreko 好的,所以
Either确实是Future,但它怎么可能是Result<Pooled<PoolClient<B>>, ClientError<B>>的未来呢?这是Future所需的类型。最外面的Either是未来,但里面又是Eithers -
@kmdreko 我的意思是,为什么它返回
Either::Right(Either::Right(Either::left(error)))而不是返回Future(error)?
标签: rust rust-tokio tokio