Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

async fn execute(&self, partition: usize) -> Result<Pin<Box<dyn BatchStream + Send + Sync>>>;
like image 532
Ken Avatar asked Jan 27 '26 20:01

Ken


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:

  • How to use map for Box<dyn Stream<Item=()>>?
  • Traits returning a trait: In some cases works, in others it doesn't
  • `FuturesUnordered` does not satisfy `Stream`?
like image 62
kmdreko Avatar answered Jan 29 '26 13:01

kmdreko