1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use futures_task::{FutureObj, Spawn, SpawnError};

/// An implementation of [`Spawn`](futures_task::Spawn) that
/// discards spawned futures when used.
///
/// # Examples
///
/// ```
/// use futures::task::SpawnExt;
/// use futures_test::task::NoopSpawner;
///
/// let spawner = NoopSpawner::new();
/// spawner.spawn(async { }).unwrap();
/// ```
#[derive(Debug)]
pub struct NoopSpawner {
    _reserved: (),
}

impl NoopSpawner {
    /// Create a new instance
    pub fn new() -> Self {
        Self { _reserved: () }
    }
}

impl Spawn for NoopSpawner {
    fn spawn_obj(&self, _future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
        Ok(())
    }
}

impl Default for NoopSpawner {
    fn default() -> Self {
        Self::new()
    }
}

/// Get a reference to a singleton instance of [`NoopSpawner`].
///
/// # Examples
///
/// ```
/// use futures::task::SpawnExt;
/// use futures_test::task::noop_spawner_mut;
///
/// let spawner = noop_spawner_mut();
/// spawner.spawn(async { }).unwrap();
/// ```
pub fn noop_spawner_mut() -> &'static mut NoopSpawner {
    Box::leak(Box::new(NoopSpawner::new()))
}