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 55 56 57 58 59 60 61 62 63 64 65
use futures_core::future::FutureObj; use futures_core::task::{Spawn, SpawnObjError}; /// An implementation of [`Spawn`](futures_core::task::Spawn) that panics /// when used. /// /// # Examples /// /// ```should_panic /// #![feature(async_await, futures_api)] /// use futures::task::SpawnExt; /// use futures_test::task::{noop_context, PanicSpawner}; /// /// let mut cx = noop_context(); /// let mut spawn = PanicSpawner::new(); /// let cx = &mut cx.with_spawner(&mut spawn); /// /// cx.spawner().spawn(async { }); // Will panic /// ``` #[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( &mut self, _future: FutureObj<'static, ()>, ) -> Result<(), SpawnObjError> { 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 /// #![feature(async_await, futures_api)] /// use futures::task::{self, SpawnExt}; /// use futures_test::task::{panic_local_waker_ref, panic_spawner_mut}; /// /// let mut cx = task::Context::new( /// panic_local_waker_ref(), /// panic_spawner_mut(), /// ); /// /// cx.spawner().spawn(async { }); // Will panic /// ``` pub fn panic_spawner_mut() -> &'static mut PanicSpawner { Box::leak(Box::new(PanicSpawner::new())) }