Why this param is trait object?


Use traits_preview and move UnboundedReceiver, how come it becomes trait object whereas it is a struct?

use futures::channel::mpsc;
use futures::stream::{Stream, StreamExt};

fn test(event_in: mpsc::UnboundedReceiver<String>) {
    let event_in = event_in.left_stream::<Stream<Item=()>>();
fn main() {}
error: the `left_stream` method cannot be invoked on a trait object
 --> src/main.rs:5:29
5 |     let event_in = event_in.left_stream::<Stream<Item=()>>();
  |                             ^^^^^^^^^^^
help: another candidate was found in the following trait, perhaps add a `use` for it:
1 | use futures_util::stream::StreamExt;


futures = {version = "0.3.0-alpha.12", package = "futures-preview"}


Stream<Item=()> is the trait object it does not support. Also Item must match.

if type inference is not filling in the value (with right_stream) then you have to fill it with the type you want. Box can often do the job.

let event_in = event_in.left_stream::<Box<dyn Stream<Item=String>>>();
1 Like