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