| 1 | use futures_task::{FutureObj, Spawn, SpawnError}; |
| 2 | use std::cell::{Ref, RefCell}; |
| 3 | |
| 4 | /// An implementation of [`Spawn`](futures_task::Spawn) that records |
| 5 | /// any [`Future`](futures_core::future::Future)s spawned on it. |
| 6 | /// |
| 7 | /// # Examples |
| 8 | /// |
| 9 | /// ``` |
| 10 | /// use futures::task::SpawnExt; |
| 11 | /// use futures_test::task::RecordSpawner; |
| 12 | /// |
| 13 | /// let recorder = RecordSpawner::new(); |
| 14 | /// recorder.spawn(async { }).unwrap(); |
| 15 | /// assert_eq!(recorder.spawned().len(), 1); |
| 16 | /// ``` |
| 17 | #[derive(Debug, Default)] |
| 18 | pub struct RecordSpawner { |
| 19 | spawned: RefCell<Vec<FutureObj<'static, ()>>>, |
| 20 | } |
| 21 | |
| 22 | impl RecordSpawner { |
| 23 | /// Create a new instance |
| 24 | pub fn new() -> Self { |
| 25 | Default::default() |
| 26 | } |
| 27 | |
| 28 | /// Inspect any futures that were spawned onto this [`Spawn`]. |
| 29 | pub fn spawned(&self) -> Ref<'_, Vec<FutureObj<'static, ()>>> { |
| 30 | self.spawned.borrow() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl Spawn for RecordSpawner { |
| 35 | fn spawn_obj(&self, future: FutureObj<'static, ()>) -> Result<(), SpawnError> { |
| 36 | self.spawned.borrow_mut().push(future); |
| 37 | Ok(()) |
| 38 | } |
| 39 | } |
| 40 | |