Hello everyone,
I have a function that takes a generic stream and returns another stream, like for example a transformation on top of the previous one. So for example this works:
use futures::Stream;
use futures::stream;
use bytes::Bytes;
use futures::StreamExt;
fn transform(input: impl Stream<Item = Bytes> + Send + Sync + 'static) -> impl Stream<Item = Bytes> + Send + Sync + 'static {
input.map(|el| Bytes::from(el))
}
#[tokio::main]
async fn main() {
let mut transformed_stream = transform(
stream::iter(vec![Bytes::from("hello")])
);
println!("{:?}", transformed_stream.next().await);
}
but if I try to move it into a trait, using the type alias feature for impl trait, then it doesn't:
#![feature(type_alias_impl_trait)]
use futures::Stream;
use futures::stream;
use bytes::Bytes;
use futures::StreamExt;
trait CoolTrait {
type OutputStream: Stream<Item = Bytes> + Send + Sync + 'static;
fn transform(&self, input: impl Stream<Item = Bytes> + Send + Sync + 'static) -> Self::OutputStream;
}
struct CoolTraitImpl;
impl CoolTrait for CoolTraitImpl {
type OutputStream = impl Stream<Item = Bytes>;
fn transform(&self, input: impl Stream<Item = Bytes> + Send + Sync + 'static) -> Self::OutputStream {
input.map(|el| Bytes::from(el))
}
}
#[tokio::main]
async fn main() {
let cool_trait_impl = CoolTraitImpl;
let mut transformed_stream = cool_trait_impl.transform(
stream::iter(vec![Bytes::from("hello")])
);
println!("{:?}", transformed_stream.next().await);
}
and I get an error that I don't really understand. Is what I'm trying to do above possible? What am I missing?
Thanks for your help.