为什么在使用 TryFuture 而不是等效的 Future 时会收到有关类型不匹配的错误?
Why do I get an error about mismatched types when using TryFuture instead of the equivalent Future?
我有以下使用 Future
.
的工作代码
use futures::{future, Future};
fn fut_res() -> impl Future<Output = Result<(), failure::Error>> {
future::ok::<(), failure::Error>(())
}
#[tokio::main]
async fn main() -> Result<(), failure::Error> {
if let Err(e) = fut_res().await {
println!("{:?}", e);
}
Ok(())
}
根据我在文档中阅读的内容,我应该能够更改代码以使用 TryFuture
,如下所示:
use futures::{future, TryFuture};
fn try_fut() -> impl TryFuture<Ok = (), Error = failure::Error> {
future::ok::<(), failure::Error>(())
}
#[tokio::main]
async fn main() -> Result<(), failure::Error> {
if let Err(e) = try_fut().await {
println!("{:?}", e);
}
Ok(())
}
编译器抱怨 try_fut
必须 return 关联类型 Output
,但根据定义,此类型 是 Result<(), failure::Error>
:
error[E0308]: mismatched types
--> src/lib.rs:9:12
|
9 | if let Err(e) = try_fut().await {
| ^^^^^^ expected associated type, found enum `std::result::Result`
|
= note: expected type `<impl futures_core::future::TryFuture as core::future::future::Future>::Output`
found type `std::result::Result<_, _>`
= note: consider constraining the associated type `<impl futures_core::future::TryFuture as core::future::future::Future>::Output` to `std::result::Result<_, _>` or calling a method that returns `<impl futures_core::future::TryFuture as core::future::future::Future>::Output`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
如何向编译器解释这一点?
Playground 不起作用,因为 tokio
仍处于 alpha 阶段,但也有一个合适的 Cargo.toml
.
您的语法目前无效。 TryFuture
的文档显示您必须暂时将 Result
包装在 Poll
中(重点是我的):
fn try_poll(
self: PinMut<Self>,
cx: &mut Context ) -> Poll<Result<Self::Ok, Self::Error>> [−]
Poll this TryFuture
as if it were a Future
.
This method is a stopgap for a compiler limitation that prevents us
from directly inheriting from the Future
trait; in the future it won't
be needed.
解决此编译器限制后,将不再需要此方法,您将能够做您所做的事情(或类似的事情)
参考资料
我有以下使用 Future
.
use futures::{future, Future};
fn fut_res() -> impl Future<Output = Result<(), failure::Error>> {
future::ok::<(), failure::Error>(())
}
#[tokio::main]
async fn main() -> Result<(), failure::Error> {
if let Err(e) = fut_res().await {
println!("{:?}", e);
}
Ok(())
}
根据我在文档中阅读的内容,我应该能够更改代码以使用 TryFuture
,如下所示:
use futures::{future, TryFuture};
fn try_fut() -> impl TryFuture<Ok = (), Error = failure::Error> {
future::ok::<(), failure::Error>(())
}
#[tokio::main]
async fn main() -> Result<(), failure::Error> {
if let Err(e) = try_fut().await {
println!("{:?}", e);
}
Ok(())
}
编译器抱怨 try_fut
必须 return 关联类型 Output
,但根据定义,此类型 是 Result<(), failure::Error>
:
error[E0308]: mismatched types
--> src/lib.rs:9:12
|
9 | if let Err(e) = try_fut().await {
| ^^^^^^ expected associated type, found enum `std::result::Result`
|
= note: expected type `<impl futures_core::future::TryFuture as core::future::future::Future>::Output`
found type `std::result::Result<_, _>`
= note: consider constraining the associated type `<impl futures_core::future::TryFuture as core::future::future::Future>::Output` to `std::result::Result<_, _>` or calling a method that returns `<impl futures_core::future::TryFuture as core::future::future::Future>::Output`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
如何向编译器解释这一点?
Playground 不起作用,因为 tokio
仍处于 alpha 阶段,但也有一个合适的 Cargo.toml
.
您的语法目前无效。 TryFuture
的文档显示您必须暂时将 Result
包装在 Poll
中(重点是我的):
fn try_poll( self: PinMut<Self>, cx: &mut Context ) -> Poll<Result<Self::Ok, Self::Error>> [−]
Poll this
TryFuture
as if it were aFuture
.This method is a stopgap for a compiler limitation that prevents us from directly inheriting from the
Future
trait; in the future it won't be needed.
解决此编译器限制后,将不再需要此方法,您将能够做您所做的事情(或类似的事情)