[][src]Struct popol::Waker

pub struct Waker { /* fields omitted */ }

Wakers are used to wake up wait.

Implementations

impl Waker[src]

pub fn new<K: Eq + Clone>(sources: &mut Sources<K>, key: K) -> Result<Waker>[src]

Create a new Waker.

Examples

Wake a wait 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::{Sources, Events, Waker};

    const WAKER: &'static str = "waker";

    let mut events = Events::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::new(&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.wait_timeout(&mut events, Duration::from_secs(1))?;

    assert!(!events.is_empty(), "There should be at least one event selected");

    let mut events = events.iter();
    let (key, event) = events.next().unwrap();

    assert!(key == &WAKER, "The event is triggered by the waker");
    assert!(event.readable, "The event is readable");
    assert!(events.next().is_none(), "There was only one event");

    handle.join().unwrap();

    Ok(())
}

pub fn wake(&self) -> Result<()>[src]

Wake up a waker. Causes popol::wait to return with a readiness event for this waker.

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.