[−][src]Function fibers::sync::oneshot::monitor
pub fn monitor<T, E>() -> (Monitored<T, E>, Monitor<T, E>)
Creates a oneshot channel for unidirectional monitoring.
When Monitored
object is (intentionally or unintentionally) dropped,
the corresponding Monitor
object will detect it and
return the resulting value at the next time the Future::poll
method is
called on it.
Examples
An example of monitoring a successful completion:
use fibers::{Executor, InPlaceExecutor, Spawn}; use fibers::sync::oneshot; use futures::{Async, Future}; let mut executor = InPlaceExecutor::new().unwrap(); let (monitored, mut monitor) = oneshot::monitor(); // Spawns monitored fiber // (In practice, spawning fiber via `spawn_monitor` function is // more convenient way to archieve the same result) executor.spawn_fn(move || { // Notifies the execution have completed successfully. monitored.exit(Ok("succeeded") as Result<_, ()>); Ok(()) }); // Runs `executor` until above fiber exists loop { let result = monitor.poll().expect("Unexpected failure"); if let Async::Ready(value) = result { assert_eq!(value, "succeeded"); break; } else { executor.run_once().unwrap(); } }
An example of detecting unintentional termination:
use fibers::{Executor, InPlaceExecutor, Spawn}; use fibers::sync::oneshot; use futures::{Async, Future}; let mut executor = InPlaceExecutor::new().unwrap(); let (monitored, mut monitor) = oneshot::monitor::<(),()>(); // Spawns monitored fiber // (In practice, spawning fiber via `spawn_monitor` function is // more convenient way to archieve the same result) executor.spawn_fn(move || { let _ = monitored; // This fiber owns `monitored` Ok(()) // Terminated before calling `Monitored::exit` method }); // Runs `executor` until above fiber exists loop { match monitor.poll() { Ok(Async::NotReady) => { executor.run_once().unwrap(); } Ok(Async::Ready(_)) => unreachable!(), Err(e) => { assert_eq!(e, oneshot::MonitorError::Aborted); break; } } }
Implementation Details
Internally, this channel is almost the same as the one created by channel
function.