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