Struct popol::Waker

source ·
pub struct Waker { /* private fields */ }
Expand description

Wakers are used to wake up wait.

Implementations§

source§

impl Waker

source

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(())
}
source

pub fn new() -> Result<Waker>

Create a new waker.

source

pub fn wake(&self) -> Result<()>

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

source

pub fn reset(fd: impl AsRawFd) -> Result<()>

Reset the waker by draining the receive buffer.

Trait Implementations§

source§

impl AsRawFd for &Waker

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.