Gatonito
Gatonito

Reputation: 2174

Simple future example

I'm trying to do a simple future example. I followed https://blog.logrocket.com/a-practical-guide-to-async-in-rust/

So I did:

use futures::Future;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

pub async fn receive_fut(
        f: &mut dyn FnMut(&[u8])
) -> impl Future<Output = Result<Vec<u8>>> {
    futures::future::ok(Ok(Vec::new())).await
}

but I get

error[E0277]: `std::result::Result<std::result::Result<Vec<_>, _>, _>` is not a future
 --> src/lib.rs:6:10
  |
6 |     ) -> impl Future<Output = Result<Vec<u8>>> {
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `std::result::Result<std::result::Result<Vec<_>, _>, _>` is not a future
  |
  = help: the trait `futures::Future` is not implemented for `std::result::Result<std::result::Result<Vec<_>, _>, _>`

error: aborting due to previous error

Why std::result::Result should implement future, if I used futures::future::ok to turn it into a future result?

Upvotes: 0

Views: 1215

Answers (1)

kmdreko
kmdreko

Reputation: 60447

This is answered in the article:

Async functions differ in one important way: all your return types are “wrapped” into a Future.

You might read the documentation about Futures in Rust and think your async function needs to look like this:

async fn our_async_program() -> impl Future<Output = Result<String>> {
    future::ok("Hello world".to_string()).await
}

This is wrong! If you’re doing this, you’re overthinking it. An async function already wraps the return type, so you can write functions the way you’re used to.

This is what you actually want:

async fn our_async_program() -> Result<String> {
    future::ok("Hello world".to_string()).await
}

So you'd want to drop the impl Future<... part of the return type:

pub async fn receive_fut(
        f: &mut dyn FnMut(&[u8])
) -> Result<Vec<u8>> {
    futures::future::ok(Ok(Vec::new())).await
}

And then realize that future::ok already yields a Result::Ok, so you don't need to wrap it in Ok() again.

pub async fn receive_fut(
        f: &mut dyn FnMut(&[u8])
) -> Result<Vec<u8>> {
    futures::future::ok(Vec::new()).await
}

Upvotes: 2

Related Questions