Idiomatic future conversion


#1

How can I effectively and idiomatically convert futures::FutureResult<std::boxed::Box<futures::Future<Error=futures::Canceled, Item=std::result::Result<X, errors::Error>>>, errors::Error>`

futures::Future<Error=errors::Error, Item=()> + std::marker::Send + 'static>

All attempts using into_future() and map_err(|e| e.into()).map(|_| () ) failed.

Any ideas? Note that I am just getting starting with tokio/futures.


#2

Not sure if I correctly understood your conversion requirement, but here’s a shot:

extern crate futures;

use futures::future::{self, Future};

// I'm assuming errors::Error is something like this, in a nutshell
mod errors {
    pub struct Error;
}

fn get_fut() -> future::FutureResult<
    Box<
        futures::Future<
            Error = futures::Canceled,
            Item = std::result::Result<String, errors::Error>,
        >
            + Send, // <-- Note that we need to specify that this boxed Future is Send so we can rely on that later
    >,
    errors::Error,
> {
    unimplemented!()
}

// An example where we want to take it as a generic type and monomorphize
fn call<F: Future<Error = errors::Error, Item = ()>>(_: F) {}

fn caller() {
    call(get_fut().map(|_| ()));
}

// An example where we want to return it as a trait object
fn convert() -> Box<futures::Future<Error = errors::Error, Item = ()> + Send + 'static> {
    let inner = get_fut();
    Box::new(inner.map(|_| ()))
}

fn main() {}

Playground

The conversion here is merely dropping the entire inner Box<Future<Error=Canceled, Item=Result<X, errors::Error>>> and turning it into ().

Let me know if I’ve misunderstood.


#3

So simple :slight_smile: thanks a lot!