pub struct Events { /* private fields */ }
Expand description
Manages subscriptions to service events.
This type is created by Handle::events
. Events can be subscribe
d and
unsubscribe
d. After subscribing to events, this type should be polled
through its implementation of Stream
.
Subscriptions can be removed implicitly, e.g. when a Service
has been
destroyed. When there are no subscriptions left (or when none have been made in the first
place) Stream::poll_next
will return None
.
When the Client
shuts down, all subscriptions are removed and
Stream::poll_next
will return None
as well.
Events
holds an internal Handle
and will thus prevent the Client
from
shutting down automatically. The Handle
is only released when Events
is dropped.
This is low-level type. You should generally use the auto-generated event streams instead, which do not require knowledge of event ids and provide better ergonomics for event arguments.
§Examples
use aldrin::Event;
let mut events = handle.events();
events.subscribe(service_id, 1).await?;
events.subscribe(service_id, 2).await?;
while let Some(event) = events.next_event().await {
match event {
Event { id: 1, value, .. } => {
let value: u32 = value.deserialize()?;
println!("Event 1 with u32 value {value}.");
}
Event { id: 2, value, .. } => {
let value: String = value.deserialize()?;
println!("Event 2 with string value {value}.");
}
_ => unreachable!(),
}
}
Implementations§
source§impl Events
impl Events
sourcepub async fn subscribe(
&mut self,
service_id: ServiceId,
id: u32
) -> Result<bool, Error>
pub async fn subscribe( &mut self, service_id: ServiceId, id: u32 ) -> Result<bool, Error>
Subscribes to an event.
This function returns true
, if the event id
of service service_id
was not already
subscribed to. Otherwise false
is returned.
sourcepub fn unsubscribe(
&mut self,
service_id: ServiceId,
id: u32
) -> Result<bool, Error>
pub fn unsubscribe( &mut self, service_id: ServiceId, id: u32 ) -> Result<bool, Error>
Unsubscribe from an event.
This function returns true
, if the event id
of service service_id
was subscribed to
before the call to this function and is now unsubscribed from. Otherwise false
is
returned.
sourcepub fn poll_next_event(&mut self, cx: &mut Context<'_>) -> Poll<Option<Event>>
pub fn poll_next_event(&mut self, cx: &mut Context<'_>) -> Poll<Option<Event>>
Polls for the next event.
sourcepub async fn next_event(&mut self) -> Option<Event>
pub async fn next_event(&mut self) -> Option<Event>
Returns the next event.
Trait Implementations§
source§impl FusedStream for Events
impl FusedStream for Events
source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the stream should no longer be polled.Auto Trait Implementations§
impl Freeze for Events
impl !RefUnwindSafe for Events
impl Send for Events
impl Sync for Events
impl Unpin for Events
impl !UnwindSafe for Events
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
source§impl<T> StreamExt for T
impl<T> StreamExt for T
source§fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
source§fn into_future(self) -> StreamFuture<Self>
fn into_future(self) -> StreamFuture<Self>
source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
source§fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
source§fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
source§fn collect<C>(self) -> Collect<Self, C>
fn collect<C>(self) -> Collect<Self, C>
source§fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
source§fn concat(self) -> Concat<Self>
fn concat(self) -> Concat<Self>
source§fn count(self) -> Count<Self>where
Self: Sized,
fn count(self) -> Count<Self>where
Self: Sized,
source§fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
source§fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
true
if any element in stream satisfied a predicate. Read moresource§fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
true
if all element in stream satisfied a predicate. Read moresource§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
source§fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
StreamExt::fold
that holds internal state
and produces a new stream. Read moresource§fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
true
. Read moresource§fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
true
. Read moresource§fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
source§fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
items of the underlying stream. Read moresource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
items of the underlying stream. Read moresource§fn zip<St>(self, other: St) -> Zip<Self, St>
fn zip<St>(self, other: St) -> Zip<Self, St>
source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where
Self: Sized,
peek
method. Read moresource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
source§fn left_stream<B>(self) -> Either<Self, B>
fn left_stream<B>(self) -> Either<Self, B>
source§fn right_stream<B>(self) -> Either<B, Self>
fn right_stream<B>(self) -> Either<B, Self>
source§fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
Stream::poll_next
on Unpin
stream types.