pub struct Waker { /* private fields */ }
Expand description
Wakers are used to wake up wait
.
Implementations§
source§impl Waker
impl Waker
sourcepub fn register<K: Eq + Clone>(
sources: &mut Sources<K>,
key: K
) -> Result<Waker>
pub fn register<K: Eq + Clone>( sources: &mut Sources<K>, key: K ) -> Result<Waker>
Create a new Waker
and register it.
Examples
Wake a poll
call from another thread.
fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::thread;
use std::time::Duration;
use std::sync::Arc;
use popol::{Event, Sources, Waker, Timeout};
const WAKER: &'static str = "waker";
let mut events = Vec::new();
let mut sources = Sources::new();
// Create a waker and keep it alive until the end of the program, so that
// the reading end doesn't get closed.
let waker = Arc::new(Waker::register(&mut sources, WAKER)?);
let _waker = waker.clone();
let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(160));
// Wake up popol on the main thread.
_waker.wake().expect("waking shouldn't fail");
});
// Wait to be woken up by the other thread. Otherwise, time out.
sources.poll(&mut events, Timeout::from_secs(1))?;
assert!(!events.is_empty(), "There should be at least one event selected");
let mut events = events.iter();
let Event { key, source } = events.next().unwrap();
assert!(key == &WAKER, "The event is triggered by the waker");
assert!(source.is_readable(), "The event is readable");
assert!(events.next().is_none(), "There was only one event");
handle.join().unwrap();
Ok(())
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Waker
impl Send for Waker
impl Sync for Waker
impl Unpin for Waker
impl UnwindSafe for Waker
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more