pub struct WaitContext<T: EventToken>(pub(crate) EventContext<T>);
Expand description

Used to wait for multiple objects which are eligible for waiting.

§Example

use base::{Event, EventToken, Result, WaitContext};

#[derive(EventToken, Copy, Clone, Debug, PartialEq, Eq)]
enum ExampleToken {
   SomeEvent(u32),
   AnotherEvent,
}

let evt1 = Event::new()?;
let evt2 = Event::new()?;
let another_evt = Event::new()?;

let ctx: WaitContext<ExampleToken> = WaitContext::build_with(&[
    (&evt1, ExampleToken::SomeEvent(1)),
    (&evt2, ExampleToken::SomeEvent(2)),
    (&another_evt, ExampleToken::AnotherEvent),
])?;

// Trigger one of the `SomeEvent` events.
evt2.signal()?;

// Wait for an event to fire. `wait()` will return immediately in this example because `evt2`
// has already been triggered, but in normal use, `wait()` will block until at least one event
// is signaled by another thread or process.
let events = ctx.wait()?;
let tokens: Vec<ExampleToken> = events.iter().filter(|e| e.is_readable)
    .map(|e| e.token).collect();
assert_eq!(tokens, [ExampleToken::SomeEvent(2)]);

// Reset evt2 so it doesn't trigger again in the next `wait()` call.
let _ = evt2.reset()?;

// Trigger a different event.
another_evt.signal()?;

let events = ctx.wait()?;
let tokens: Vec<ExampleToken> = events.iter().filter(|e| e.is_readable)
    .map(|e| e.token).collect();
assert_eq!(tokens, [ExampleToken::AnotherEvent]);

let _ = another_evt.reset()?;

Tuple Fields§

§0: EventContext<T>

Implementations§

source§

impl<T: EventToken> WaitContext<T>

source

pub fn new() -> Result<WaitContext<T>>

Creates a new WaitContext.

source

pub fn build_with( triggers: &[(&dyn AsRawDescriptor, T)] ) -> Result<WaitContext<T>>

Creates a new WaitContext with the the associated triggers.

source

pub fn add(&self, descriptor: &dyn AsRawDescriptor, token: T) -> Result<()>

Adds a trigger to the WaitContext.

source

pub fn add_for_event( &self, descriptor: &dyn AsRawDescriptor, event_type: EventType, token: T ) -> Result<()>

Adds a trigger to the WaitContext watching for a specific type of event

source

pub fn add_many(&self, triggers: &[(&dyn AsRawDescriptor, T)]) -> Result<()>

Adds multiple triggers to the WaitContext.

source

pub fn modify( &self, descriptor: &dyn AsRawDescriptor, event_type: EventType, token: T ) -> Result<()>

Modifies a trigger already added to the WaitContext. If the descriptor is already registered, its associated token will be updated.

source

pub fn delete(&self, descriptor: &dyn AsRawDescriptor) -> Result<()>

Removes the given handle from triggers registered in the WaitContext if present.

source

pub fn wait(&self) -> Result<SmallVec<[TriggeredEvent<T>; 16]>>

Waits for one or more of the registered triggers to become signaled.

source

pub fn wait_timeout( &self, timeout: Duration ) -> Result<SmallVec<[TriggeredEvent<T>; 16]>>

Waits for one or more of the registered triggers to become signaled, failing if no triggers are signaled before the designated timeout has elapsed.

Trait Implementations§

source§

impl<T: EventToken> AsRawDescriptor for WaitContext<T>

source§

fn as_raw_descriptor(&self) -> RawDescriptor

Returns the underlying raw descriptor. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for WaitContext<T>
where T: RefUnwindSafe,

§

impl<T> Send for WaitContext<T>
where T: Send,

§

impl<T> Sync for WaitContext<T>
where T: Sync,

§

impl<T> Unpin for WaitContext<T>
where T: Unpin,

§

impl<T> UnwindSafe for WaitContext<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AsRawDescriptors for T
where T: AsRawDescriptor,

source§

fn as_raw_descriptors(&self) -> Vec<i32>

Returns the underlying raw descriptors. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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.