Description
Some consumers of streams expect streams to implement Sync
. This is true, for example, of tonic GRPC servers when the result of a method is a stream.
Currently, an AsyncStream
type automatically gets Sync
if the generator also implicitly implements Sync
. But it's easy to write a generator that doesn't implement Sync
. Just call any async function on a trait that uses async_trait
. The return type of such a function is Pin<Box<dyn Future + Send + 'async>>
. Notice it doesn't include + Sync
.
My guess is that async_trait
is doing the right thing here, not adding unnecessary trait bounds. And I don't know why one would want to access a future simultaneously from multiple threads anyway.
For that matter, I don't know why one would want to access a stream simultaneously from multiple threads. But if I understand Sync
correctly, it would be safe to explicitly implement it on AsyncStream
(though it would require an unsafe block), because the generator is only ever accessed inside a method that takes a mutable reference to the AsyncStream
, so IIUC only one thread at a time should be able to call that method.
Note: I'm still pretty new to Rust, so my understanding may be way off. Currently I'm stuck trying to call a Rusoto function inside a try_stream!
block, and I'm getting a compiler error because the resulting generator doesn't implement Sync
and tonic needs Sync
on the stream.