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