Struct Handler

Source
pub struct Handler<T: Event, TS: ThreadSafety> { /* 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, TS: ThreadSafety> Handler<T, TS>

Source

pub fn wait(&self) -> Waiter<'_, T, TS>

Wait for the next event.

Source

pub fn wait_direct_async<Fut: Future<Output = bool> + Send + 'static, F: FnMut(&mut T::Unique<'_>) -> Fut + Send + 'static>( &self, f: F, )

Register an async closure be called when the event is received.

Source

pub fn wait_direct( &self, f: impl FnMut(&mut T::Unique<'_>) -> bool + Send + 'static, )

Register a closure be called when the event is received.

Trait Implementations§

Source§

impl<'a, T: Event, TS: ThreadSafety> IntoFuture for &'a Handler<T, TS>

Source§

type IntoFuture = Waiter<'a, T, TS>

Which kind of future are we turning this into?
Source§

type Output = <T as Event>::Clonable

The output that the future will produce on completion.
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more
Source§

impl<T: Event, TS: ThreadSafety> Unpin for Handler<T, TS>

Auto Trait Implementations§

§

impl<T, TS> Freeze for Handler<T, TS>
where <TS as __ThreadSafety>::OnceLock<Box<<TS as __ThreadSafety>::Mutex<State<T>>>>: Freeze,

§

impl<T, TS> RefUnwindSafe for Handler<T, TS>
where <TS as __ThreadSafety>::OnceLock<Box<<TS as __ThreadSafety>::Mutex<State<T>>>>: RefUnwindSafe,

§

impl<T, TS> Send for Handler<T, TS>
where <TS as __ThreadSafety>::OnceLock<Box<<TS as __ThreadSafety>::Mutex<State<T>>>>: Send,

§

impl<T, TS> Sync for Handler<T, TS>
where <TS as __ThreadSafety>::OnceLock<Box<<TS as __ThreadSafety>::Mutex<State<T>>>>: Sync,

§

impl<T, TS> UnwindSafe for Handler<T, TS>
where <TS as __ThreadSafety>::OnceLock<Box<<TS as __ThreadSafety>::Mutex<State<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> 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>,

Source§

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>,

Source§

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.