use crossbeam::channel::{unbounded, Sender}; use futures_executor as executor; use lazy_static::lazy_static; use lightproc::prelude::*; use std::any::Any; use std::fmt::Debug; use std::future::Future; use std::ops::Deref; use std::thread; fn spawn_on_thread(future: F) -> RecoverableHandle where F: Future + Send + 'static, R: Debug + Send + 'static, { lazy_static! { // A channel that holds scheduled procs. static ref QUEUE: Sender = { let (sender, receiver) = unbounded::(); // Start the executor thread. thread::spawn(move || { for proc in receiver { proc.run(); } }); sender }; } let schedule = |t| (QUEUE.deref()).send(t).unwrap(); let span = tracing::trace_span!("runtime.spawn", kind = "local"); let (proc, handle) = LightProc::recoverable(future, schedule, span, None); let handle = handle.on_panic( |err: Box| match err.downcast::<&'static str>() { Ok(reason) => println!("Future panicked: {}", &reason), Err(err) => println!( "Future panicked with a non-text reason of typeid {:?}", err.type_id() ), }, ); proc.schedule(); handle } fn main() { let handle = spawn_on_thread(async { panic!("Panic here!"); }); executor::block_on(handle); println!("But see, despite the inner future panicking we can continue executing as normal."); }