futures_test/task/
noop_spawner.rs

1use futures_core::future::FutureObj;
2use futures_core::task::{Spawn, SpawnError};
3
4/// An implementation of [`Spawn`](futures_core::task::Spawn) that
5/// discards spawned futures when used.
6///
7/// # Examples
8///
9/// ```
10/// use futures::task::SpawnExt;
11/// use futures_test::task::NoopSpawner;
12///
13/// let mut spawner = NoopSpawner::new();
14/// spawner.spawn(async { }).unwrap();
15/// ```
16#[derive(Debug)]
17pub struct NoopSpawner {
18    _reserved: (),
19}
20
21impl NoopSpawner {
22    /// Create a new instance
23    pub fn new() -> Self {
24        Self { _reserved: () }
25    }
26}
27
28impl Spawn for NoopSpawner {
29    fn spawn_obj(
30        &mut self,
31        _future: FutureObj<'static, ()>,
32    ) -> Result<(), SpawnError> {
33        Ok(())
34    }
35}
36
37impl Default for NoopSpawner {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43/// Get a reference to a singleton instance of [`NoopSpawner`].
44///
45/// # Examples
46///
47/// ```
48/// use futures::task::SpawnExt;
49/// use futures_test::task::noop_spawner_mut;
50///
51/// let spawner = noop_spawner_mut();
52/// spawner.spawn(async { }).unwrap();
53/// ```
54pub fn noop_spawner_mut() -> &'static mut NoopSpawner {
55    Box::leak(Box::new(NoopSpawner::new()))
56}