drogue_bazaar/core/
spawn.rs

1use core::{future::Future, pin::Pin};
2
3/// A spawner, gathering spawned tasks during the setup phase.
4///
5/// NOTE: The spawner itself might not execute/drive those tasks. It may be necessary to hand over
6/// gathered tasks to some concept like [`crate::app::Main`].
7pub trait Spawner {
8    fn spawn_boxed(&mut self, future: Pin<Box<dyn Future<Output = anyhow::Result<()>>>>);
9}
10
11pub trait SpawnerExt: Spawner {
12    fn spawn_iter<I>(&mut self, iter: I)
13    where
14        I: IntoIterator<Item = Pin<Box<dyn Future<Output = anyhow::Result<()>>>>>,
15    {
16        for i in iter {
17            self.spawn_boxed(i);
18        }
19    }
20
21    fn spawn<F>(&mut self, f: F)
22    where
23        F: Future<Output = anyhow::Result<()>> + 'static,
24    {
25        self.spawn_boxed(Box::pin(f))
26    }
27}
28
29impl<S: ?Sized> SpawnerExt for S where S: Spawner {}
30
31impl Spawner for Vec<futures_core::future::LocalBoxFuture<'_, anyhow::Result<()>>> {
32    fn spawn_boxed(&mut self, future: Pin<Box<dyn Future<Output = anyhow::Result<()>>>>) {
33        self.push(future);
34    }
35}