use std::sync::Arc; use std::sync::mpsc::{SyncSender, sync_channel}; use std::thread; use futures::{self, Async, Future}; use futures::executor::{Notify, Spawn}; pub type VoidFuture = Box>; pub fn consume(mut fut: VoidFuture) { let (tx, rx) = sync_channel(1); let mut spawned = futures::executor::spawn(fut); let notifier = Arc::new(AsyncNotifier { sender: Arc::new(tx) }); loop { match spawned.poll_future_notify(¬ifier, 0) { Ok(Async::Ready(_)) => { debug!("Async future finished"); break; } Ok(Async::NotReady) => { debug!("Async future not ready"); if let Err(_) = rx.recv() { error!("Error waiting for future. Aborting"); break; } debug!("Async future woke up"); } Err(_) => { error!("Error polling async future."); break; } } } } #[derive(Clone)] struct AsyncNotifier { sender: Arc>, } impl Notify for AsyncNotifier { fn notify(&self, id: usize) { debug!("AsyncNotifier received notification"); let _ = self.sender.send(()); } }