pub trait Transport {
// Required methods
fn connect(&mut self) -> impl Future<Output = Result<()>> + Send;
fn send(
&mut self,
topic: &str,
payload: &[u8],
) -> impl Future<Output = Result<()>> + Send;
fn subscribe(
&mut self,
topic: &str,
) -> impl Future<Output = Result<()>> + Send;
// Provided method
fn send_text(
&mut self,
topic: &str,
text: &str,
) -> impl Future<Output = Result<()>> + Send { ... }
}Expand description
A bidirectional, topic-addressed message transport.
Implementations include MQTT, CoAP, LoRa, serial, and CAN. They are expected to handle reconnection and backpressure internally so that callers see a uniform, protocol-agnostic surface.
This trait is the outbound half of a link: connect, publish, and register
interest in a topic. A link that can also deliver what it subscribed to
implements Receive as well. The two are separate because not every link
listens: a LoRa uplink or a satellite messenger only sends, and it still belongs
on a transport ladder.
The returned futures are Send, so a transport can be driven from a task on
a multi-threaded runtime and can be erased behind a trait object that is.
Both matter in practice: a gateway ticks its links from spawned tasks, and a
transport ladder holds rungs of different concrete types in one list. An
implementation written as async fn satisfies this as long as everything it
holds across an await is Send, which every transport here already is.
Required Methods§
Sourcefn connect(&mut self) -> impl Future<Output = Result<()>> + Send
fn connect(&mut self) -> impl Future<Output = Result<()>> + Send
Establishes the connection to the broker, peer, or bus.
§Returns
Ok(()) once the transport is connected and ready to carry traffic.
§Errors
Returns Error::Transport if the connection
cannot be established.
Sourcefn send(
&mut self,
topic: &str,
payload: &[u8],
) -> impl Future<Output = Result<()>> + Send
fn send( &mut self, topic: &str, payload: &[u8], ) -> impl Future<Output = Result<()>> + Send
Publishes a payload to a topic.
§Arguments
topic- the destination topic or channel address.payload- the raw bytes to publish.
§Returns
Ok(()) once the payload has been handed to the transport for delivery.
§Errors
Returns Error::Transport if the payload cannot
be sent, or Error::Closed if the transport is not
connected.
Sourcefn subscribe(&mut self, topic: &str) -> impl Future<Output = Result<()>> + Send
fn subscribe(&mut self, topic: &str) -> impl Future<Output = Result<()>> + Send
Subscribes to a topic so that matching payloads are routed to this transport.
§Arguments
topic- the topic or channel filter to subscribe to.
§Returns
Ok(()) once the subscription is registered with the transport.
§Errors
Returns Error::Transport if the subscription
is rejected, or Error::Closed if the transport is
not connected.
Provided Methods§
Sourcefn send_text(
&mut self,
topic: &str,
text: &str,
) -> impl Future<Output = Result<()>> + Send
fn send_text( &mut self, topic: &str, text: &str, ) -> impl Future<Output = Result<()>> + Send
Publishes text to a topic: words, or a number written out.
This is send with the text’s UTF-8 bytes, so a reading or a
command that is plain text needs no encoding step on either end; the receiver
reads it back with Message::text or Message::number.
§Arguments
topic- the destination topic.text- the text to publish.
§Returns
Ok(()) once the transport has accepted the message.
§Errors
Whatever send returns.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".