Do I have to use Pin when returning a boxed trait object from an async method?

Viewed 342
async fn execute(&self, partition: usize) -> Result<Pin<Box<dyn BatchStream + Send + Sync>>>;
1 Answers

In general: No. If you have a function in the form async fn f() -> Box<dyn Trait>, there is no need for Pin.


However, returning a Future or Stream is a special case. So if BatchStream is an alias, or supertrait, or just mimics the Stream trait, then the Pin is likely necessary for the return value to be useful.

See other Q&As that explain where Pin is needed with Streams:

Related