Struct base::WaitContext
source · 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>
impl<T: EventToken> WaitContext<T>
sourcepub fn new() -> Result<WaitContext<T>>
pub fn new() -> Result<WaitContext<T>>
Creates a new WaitContext.
sourcepub fn build_with(
triggers: &[(&dyn AsRawDescriptor, T)]
) -> Result<WaitContext<T>>
pub fn build_with( triggers: &[(&dyn AsRawDescriptor, T)] ) -> Result<WaitContext<T>>
Creates a new WaitContext with the the associated triggers.
sourcepub fn add(&self, descriptor: &dyn AsRawDescriptor, token: T) -> Result<()>
pub fn add(&self, descriptor: &dyn AsRawDescriptor, token: T) -> Result<()>
Adds a trigger to the WaitContext.
sourcepub fn add_for_event(
&self,
descriptor: &dyn AsRawDescriptor,
event_type: EventType,
token: T
) -> Result<()>
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
sourcepub fn add_many(&self, triggers: &[(&dyn AsRawDescriptor, T)]) -> Result<()>
pub fn add_many(&self, triggers: &[(&dyn AsRawDescriptor, T)]) -> Result<()>
Adds multiple triggers to the WaitContext.
sourcepub fn modify(
&self,
descriptor: &dyn AsRawDescriptor,
event_type: EventType,
token: T
) -> Result<()>
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.
sourcepub fn delete(&self, descriptor: &dyn AsRawDescriptor) -> Result<()>
pub fn delete(&self, descriptor: &dyn AsRawDescriptor) -> Result<()>
Removes the given handle from triggers registered in the WaitContext if present.
sourcepub fn wait(&self) -> Result<SmallVec<[TriggeredEvent<T>; 16]>>
pub fn wait(&self) -> Result<SmallVec<[TriggeredEvent<T>; 16]>>
Waits for one or more of the registered triggers to become signaled.
sourcepub fn wait_timeout(
&self,
timeout: Duration
) -> Result<SmallVec<[TriggeredEvent<T>; 16]>>
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>
impl<T: EventToken> AsRawDescriptor for WaitContext<T>
source§fn as_raw_descriptor(&self) -> RawDescriptor
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> AsRawDescriptors for Twhere
T: AsRawDescriptor,
impl<T> AsRawDescriptors for Twhere
T: AsRawDescriptor,
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