Struct ConnectionStateWatch
pub struct ConnectionStateWatch {
pub(crate) rx: Receiver<ConnectionStatus>,
}Expand description
A handle for observing the connection status of an
MqttClient.
Returned by
MqttClient::connection_status.
Clone to get an independently-owned copy backed by the same watch channel.
§Example
use finmqtt_tokio::{ConnectionStatus, MqttClient};
use tokio::time::{Duration, timeout};
async fn monitor(client: &MqttClient) {
let status = client.connection_status();
println!("initial: {:?}", status.current());
// Wait up to 5 s for the initial connection.
let connected = timeout(Duration::from_secs(5), status.wait_until_connected())
.await
.expect("connection timed out");
assert!(connected, "event loop exited before connecting");
// Observe every subsequent state transition.
let mut changes = status.changes();
while let Some(state) = changes.next().await {
println!("new state: {state:?}");
}
}Fields§
§rx: Receiver<ConnectionStatus>Implementations§
§impl ConnectionStateWatch
impl ConnectionStateWatch
pub(crate) const fn new(rx: Receiver<ConnectionStatus>) -> Self
pub fn current(&self) -> ConnectionStatus
pub fn current(&self) -> ConnectionStatus
Returns the current connection status without blocking.
pub async fn wait_until_connected(&self) -> bool
pub async fn wait_until_connected(&self) -> bool
Resolves once the client reaches
Connected.
Returns true if the client connected, or false if the event loop
exited before a connection was established (e.g.
stop was called before the
first connection attempt succeeded).
Wrap with [tokio::time::timeout] to bound the wait:
timeout(
Duration::from_secs(5),
client.connection_status().wait_until_connected(),
)
.await
.expect("timed out waiting for connection");pub fn changes(&self) -> ConnectionStatusStream
pub fn changes(&self) -> ConnectionStatusStream
Returns a stream that emits each time the connection status transitions.
The stream does not yield the current status on subscription. It only
emits future transitions. Use current to snapshot the
initial status, and wait_until_connected
for the common “wait until the client is up” use case.
The stream ends (yields None) when the event loop exits.
Trait Implementations§
§impl Clone for ConnectionStateWatch
impl Clone for ConnectionStateWatch
§fn clone(&self) -> ConnectionStateWatch
fn clone(&self) -> ConnectionStateWatch
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ConnectionStateWatch
impl RefUnwindSafe for ConnectionStateWatch
impl Send for ConnectionStateWatch
impl Sync for ConnectionStateWatch
impl Unpin for ConnectionStateWatch
impl UnwindSafe for ConnectionStateWatch
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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