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 53 54
use futures_task::{FutureObj, Spawn, SpawnError};
/// An implementation of [`Spawn`](futures_task::Spawn) that panics
/// when used.
///
/// # Examples
///
/// ```should_panic
/// use futures::task::SpawnExt;
/// use futures_test::task::PanicSpawner;
///
/// let spawn = PanicSpawner::new();
/// spawn.spawn(async { })?; // Will panic
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug)]
pub struct PanicSpawner {
_reserved: (),
}
impl PanicSpawner {
/// Create a new instance
pub fn new() -> Self {
Self { _reserved: () }
}
}
impl Spawn for PanicSpawner {
fn spawn_obj(&self, _future: FutureObj<'static, ()>) -> Result<(), SpawnError> {
panic!("should not spawn")
}
}
impl Default for PanicSpawner {
fn default() -> Self {
Self::new()
}
}
/// Get a reference to a singleton instance of [`PanicSpawner`].
///
/// # Examples
///
/// ```should_panic
/// use futures::task::SpawnExt;
/// use futures_test::task::panic_spawner_mut;
///
/// let spawner = panic_spawner_mut();
/// spawner.spawn(async { })?; // Will panic
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn panic_spawner_mut() -> &'static mut PanicSpawner {
Box::leak(Box::new(PanicSpawner::new()))
}