pub struct RequestApprover<P, R> { /* private fields */ }Expand description
An ID-tracked, multi-request approval queue for server contexts.
Each request is assigned a unique ID, stored in FIFO order, and awaits a response from the UI/caller side. Designed for web-server polling patterns where the UI fetches pending requests and responds asynchronously.
Use event_listener::EventListener from listen to
efficiently wait for state changes without polling.
§Type Parameters
P: Payload type (cloneable request data visible to the UI).R: Response type sent back to the requester.
Implementations§
Source§impl<P: Clone, R> RequestApprover<P, R>
impl<P: Clone, R> RequestApprover<P, R>
Sourcepub fn respond(&self, id: &str, response: R) -> bool
pub fn respond(&self, id: &str, response: R) -> bool
Respond to a pending request by ID.
Returns true if the request was found and the response was sent.
§Panics
Panics if the request lock was poisoned by a panic in another thread.
Sourcepub fn peek(&self) -> Option<(String, P)>
pub fn peek(&self) -> Option<(String, P)>
Return the oldest pending request (ID + cloned payload) without removing it.
§Panics
Panics if the request lock was poisoned by a panic in another thread.
Sourcepub fn peek_filtered(
&self,
predicate: impl Fn(&P) -> bool,
) -> Option<(String, P)>
pub fn peek_filtered( &self, predicate: impl Fn(&P) -> bool, ) -> Option<(String, P)>
Return the oldest pending request matching a predicate.
§Panics
Panics if the request lock was poisoned by a panic in another thread.
Sourcepub fn get_payload(&self, id: &str) -> Option<P>
pub fn get_payload(&self, id: &str) -> Option<P>
Get a cloned payload by request ID.
§Panics
Panics if the request lock was poisoned by a panic in another thread.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are no pending requests.
§Panics
Panics if the request lock was poisoned by a panic in another thread.
Sourcepub fn listen(&self) -> EventListener ⓘ
pub fn listen(&self) -> EventListener ⓘ
Create a listener that resolves when the approver state changes.
Use this for efficient async polling:
loop {
if let Some((id, payload)) = approver.peek() {
// handle request
}
approver.listen().await;
}Trait Implementations§
Source§impl<P, R> Debug for RequestApprover<P, R>
impl<P, R> Debug for RequestApprover<P, R>
Auto Trait Implementations§
impl<P, R> !Freeze for RequestApprover<P, R>
impl<P, R> RefUnwindSafe for RequestApprover<P, R>
impl<P, R> Send for RequestApprover<P, R>
impl<P, R> Sync for RequestApprover<P, R>
impl<P, R> Unpin for RequestApprover<P, R>
impl<P, R> UnsafeUnpin for RequestApprover<P, R>
impl<P, R> UnwindSafe for RequestApprover<P, R>
Blanket Implementations§
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more