pub struct LoopbackTransport { /* private fields */ }Expand description
An in-process transport that routes through a shared LoopbackBroker.
A transport is created disconnected; connect registers
it with the broker so it can publish and receive. Inbound messages are read
with recv.
§Examples
use pamoja_core::Transport;
use pamoja_loopback::{LoopbackBroker, LoopbackTransport};
let broker = LoopbackBroker::new();
let mut subscriber = LoopbackTransport::new(broker.clone());
let mut publisher = LoopbackTransport::new(broker);
subscriber.connect().await?;
publisher.connect().await?;
subscriber.subscribe("sensors/+/temperature").await?;
publisher.send("sensors/1/temperature", b"21.5").await?;
let message = subscriber.recv().await?.expect("a message");
assert_eq!(message.topic, "sensors/1/temperature");
assert_eq!(message.payload, b"21.5");Implementations§
Source§impl LoopbackTransport
impl LoopbackTransport
Sourcepub fn new(broker: LoopbackBroker) -> Self
pub fn new(broker: LoopbackBroker) -> Self
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Reports whether the transport is connected to its broker.
§Returns
true once connect has succeeded and before
disconnect is called.
Sourcepub async fn recv(&mut self) -> Result<Option<Message>>
pub async fn recv(&mut self) -> Result<Option<Message>>
Awaits the next message from any subscribed topic.
§Returns
Some(message) for the next message, or None once the broker and all
other transports have been dropped.
§Errors
Returns Error::Closed if the transport is
not connected.
Sourcepub fn disconnect(&mut self)
pub fn disconnect(&mut self)
Disconnects the transport from the broker.
Its registration is pruned from the broker on the next publish.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LoopbackTransport
impl RefUnwindSafe for LoopbackTransport
impl Send for LoopbackTransport
impl Sync for LoopbackTransport
impl Unpin for LoopbackTransport
impl UnsafeUnpin for LoopbackTransport
impl UnwindSafe for LoopbackTransport
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
Mutably borrows from an owned value. Read more