I'm using tonic and tonic returns a Streaming<A>
, I want to turn it into a Box<dyn Stream<Item=B>>
, so the user of my function don't need to know about tonic detail.
I tried
stream.map(|val| (turns A into B))
which gives me a Map<Streaming<A>, |A| -> B>
, rather than a Stream<B>
that I want.
Stream<Item = B>
is a trait, not a type, so no value can be one. However,Map<Streaming<A>, |A| -> B>
does implementStream<Item = B>
, which means it can be coerced intodyn Stream<Item = B>
, which is a type. This trait object is put behind a pointer type (Box
in this case) because it is unsized.It's likely you can leave off the
as
cast, since type inference will pick it up in most cases.It may be possible to return
impl Stream<Item = B>
instead, which allows you to remove theBox
. This hides the inner type from your public API.Also note that
TryStream
andTryStreamExt
are useful for streams that returnResult
(they can be difficult to discover on your own). You should still returnimpl Stream
ordyn Stream
since those can be used asTryStream
, butTryStream
can't always be used asStream
. But you can use their methods inside your implementation.