Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a BoxStream into BoxFuture

I have a function which returns a BoxStream<(), io::Error> and would like to convert this stream into a Future (or BoxFuture) but I'm having some compiler issues:

extern crate futures;

use futures::stream::BoxStream;
use std::io;

pub fn foo() -> BoxStream<(), io::Error> {
    unimplemented!()
}

fn main() {
    let a = foo().into_future();
}

And the error message:

error[E0277]: the trait bound `futures::Stream<Error=std::io::Error, Item=()> + std::marker::Send + 'static: std::marker::Sized` is not satisfied
  --> src/main.rs:23:19
   |
23 |     let a = foo().into_future();
   |                   ^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `futures::Stream<Error=std::io::Error, Item=()> + std::marker::Send + 'static`
   |
   = note: `futures::Stream<Error=std::io::Error, Item=()> + std::marker::Send + 'static` does not have a constant size known at compile-time

Is there a way around this?

like image 863
Peter Jankuliak Avatar asked Dec 05 '25 09:12

Peter Jankuliak


1 Answers

You are calling futures::future::IntoFuture::into_future, not futures::stream::Stream::into_future. You need to import the trait:

extern crate futures;

use futures::Stream; // This
use futures::stream::BoxStream;
use std::io;

pub fn foo() -> BoxStream<(), io::Error> {
    unimplemented!()
}

fn main() {
    let a = foo().into_future();
}

You can validate the difference with

fn main() {
    let a = futures::future::IntoFuture::into_future(foo());
    let a = futures::Stream::into_future(foo());
}
like image 98
Shepmaster Avatar answered Dec 07 '25 17:12

Shepmaster