futures_test/task/
record_spawner.rs

1use futures_core::future::FutureObj;
2use futures_core::task::{Spawn, SpawnError};
3
4/// An implementation of [`Spawn`](futures_core::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 mut recorder = RecordSpawner::new();
14/// recorder.spawn(async { }).unwrap();
15/// assert_eq!(recorder.spawned().len(), 1);
16/// ```
17#[derive(Debug)]
18pub struct RecordSpawner {
19    spawned: Vec<FutureObj<'static, ()>>,
20}
21
22impl RecordSpawner {
23    /// Create a new instance
24    pub fn new() -> Self {
25        Self {
26            spawned: Vec::new(),
27        }
28    }
29
30    /// Inspect any futures that were spawned onto this [`Spawn`].
31    pub fn spawned(&self) -> &[FutureObj<'static, ()>] {
32        &self.spawned
33    }
34}
35
36impl Spawn for RecordSpawner {
37    fn spawn_obj(
38        &mut self,
39        future: FutureObj<'static, ()>,
40    ) -> Result<(), SpawnError> {
41        self.spawned.push(future);
42        Ok(())
43    }
44}
45
46impl Default for RecordSpawner {
47    fn default() -> Self {
48        Self::new()
49    }
50}