Struct async_winit::Handler
source · pub struct Handler<T: Event> { /* private fields */ }
Expand description
An event handler.
This type is used to receive events from the GUI system. Whenever an event occurs, it is sent to all of the listeners of the corresponding event type. The listeners can then process the event asynchronously.
There are four ways to listen to events:
- Using the
wait_once()
function, which waits for a single instance of the event. However, there is a race condition where it can miss events in multithreaded environments where the event occurs between the time the event is received and the time the listener is registered. To avoid this, use one of the other methods. However, this method is the most efficient. - Using the
wait_many()
stream, which asynchronously iterates over events. - Using the
wait_direct[_async]()
function, which runs a closure in the event handler. This is good for use cases like drawing. - Using the
wait_guard()
function, which forces the event handler to stop until the event has been completely processed. This is good for use cases like handling suspends.
This type does not allocate unless you use any waiting functions; therefore, you only pay overhead for events that you use.
Implementations§
source§impl<T: Event> Handler<T>
impl<T: Event> Handler<T>
sourcepub async fn wait_direct_async<Fut: Future<Output = bool> + Send + 'static, F: FnMut(&mut T::Unique<'_>) -> Fut + Send + 'static>(
&self,
f: F
) -> usize
pub async fn wait_direct_async<Fut: Future<Output = bool> + Send + 'static, F: FnMut(&mut T::Unique<'_>) -> Fut + Send + 'static>( &self, f: F ) -> usize
Register an async closure be called when the event is received.
sourcepub async fn wait_direct(
&self,
f: impl FnMut(&mut T::Unique<'_>) -> bool + Send + 'static
) -> usize
pub async fn wait_direct( &self, f: impl FnMut(&mut T::Unique<'_>) -> bool + Send + 'static ) -> usize
Register a closure be called when the event is received.
sourcepub async fn remove_direct(&self, id: usize)
pub async fn remove_direct(&self, id: usize)
Remove a direct listener.
sourcepub fn wait_guard(&self) -> WaitGuard<'_, T>
pub fn wait_guard(&self) -> WaitGuard<'_, T>
A guard that prevents the event handler from returning before it is processed.
Trait Implementations§
impl<T: Event> Unpin for Handler<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for Handler<T>
impl<T> Send for Handler<T>
impl<T> Sync for Handler<T>
impl<T> !UnwindSafe for Handler<T>
Blanket Implementations§
source§impl<F> FutureExt for Fwhere
F: Future + ?Sized,
impl<F> FutureExt for Fwhere F: Future + ?Sized,
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere F: Future,
§type IntoFuture = F
type IntoFuture = F
Which kind of future are we turning this into?
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Creates a future from a value. Read more