pub struct UiInbox<T> { /* private fields */ }Expand description
UI-thread side of a bounded generic mailbox.
Receiver is single-consumer and this type is intended to stay on the UI
thread. Dropping it closes the channel after buffered messages are dropped.
§Examples
use std::num::NonZeroUsize;
use ailloli_ui_runtime::app::UiInbox;
let (_sender, inbox) = UiInbox::<u8>::channel(NonZeroUsize::new(4).unwrap());
assert_eq!(inbox.stats().current_depth, 0);Implementations§
Source§impl<T> UiInbox<T>
Provides the operations defined for UiInbox<T>.
impl<T> UiInbox<T>
Provides the operations defined for UiInbox<T>.
Sourcepub fn channel(capacity: NonZeroUsize) -> (UiSender<T>, Self)
pub fn channel(capacity: NonZeroUsize) -> (UiSender<T>, Self)
Creates a bounded channel with exact nonzero message capacity.
No wake callback is installed. Producers may enqueue before late wake installation; the outstanding notification is retained.
§Examples
use std::num::NonZeroUsize;
use ailloli_ui_runtime::app::UiInbox;
let (sender, mut inbox) = UiInbox::channel(NonZeroUsize::new(2).unwrap());
sender.send(5).unwrap();
assert_eq!(inbox.drain(NonZeroUsize::new(2).unwrap()).unwrap().messages, [5]);Sourcepub fn install_wake(&self, wake: Arc<dyn UiWake>) -> Result<(), UiWakeError>
pub fn install_wake(&self, wake: Arc<dyn UiWake>) -> Result<(), UiWakeError>
Installs/replaces the thread-safe host wake callback.
If a wake is outstanding, the new callback is invoked synchronously before this method returns. Its error is returned; the callback remains installed and notification stays retryable.
§Errors
Returns the installed callback’s UiWakeError when delivering an
already outstanding notification fails.
§Examples
use std::num::NonZeroUsize;
use std::sync::Arc;
use ailloli_ui_runtime::app::{UiInbox, UiWake, UiWakeError};
struct Wake; impl UiWake for Wake { fn wake(&self)->Result<(),UiWakeError>{Ok(())} }
let (_sender, inbox) = UiInbox::<()>::channel(NonZeroUsize::new(1).unwrap());
assert!(inbox.install_wake(Arc::new(Wake)).is_ok());Sourcepub fn detach_wake(&self)
pub fn detach_wake(&self)
Removes the current callback without discarding an outstanding wake.
§Examples
use std::num::NonZeroUsize;
use ailloli_ui_runtime::app::UiInbox;
let (_sender, inbox) = UiInbox::<()>::channel(NonZeroUsize::new(1).unwrap());
inbox.detach_wake();
assert_eq!(inbox.stats().wake_calls, 0);Sourcepub fn stats(&self) -> UiInboxStats
pub fn stats(&self) -> UiInboxStats
Returns a non-resetting shared statistics snapshot.
§Examples
use std::num::NonZeroUsize;
use ailloli_ui_runtime::app::UiInbox;
let (_sender, inbox) = UiInbox::<()>::channel(NonZeroUsize::new(1).unwrap());
assert_eq!(inbox.stats().enqueued, 0);Sourcepub fn drain(&mut self, limit: NonZeroUsize) -> Result<UiDrain<T>, UiWakeError>
pub fn drain(&mut self, limit: NonZeroUsize) -> Result<UiDrain<T>, UiWakeError>
Removes up to limit FIFO messages and reports whether more remain.
When exactly limit messages were removed, the method probes one extra
message and retains it internally to determine remaining. If work
remains it synchronously requests another wake. A wake error is returned
after the batch has already been removed and counters updated, so the
caller does not receive those messages and must treat such a failure as
host-fatal or arrange higher-level recovery.
§Errors
Returns a wake error only when a pending remainder needs another signal. Channel disconnection is recorded in stats and otherwise yields a normal drain result.
§Examples
use std::num::NonZeroUsize;
use ailloli_ui_runtime::app::UiInbox;
let (sender, mut inbox) = UiInbox::channel(NonZeroUsize::new(3).unwrap());
sender.send(1).unwrap(); sender.send(2).unwrap();
let first = inbox.drain(NonZeroUsize::new(1).unwrap()).unwrap();
assert_eq!(first.messages, [1]);
assert!(first.remaining);