Skip to content

Instantly share code, notes, and snippets.

@lulf
Last active May 16, 2022 08:26
Show Gist options
  • Save lulf/d83298e2c41a7e2e7e857ed53c02489a to your computer and use it in GitHub Desktop.
Save lulf/d83298e2c41a7e2e7e857ed53c02489a to your computer and use it in GitHub Desktop.
Shared tcp stack traits
/// Documentation
pub trait TcpClient: embedded_io::Io {
/// Documentation
type TcpConnection<'m>: Read + Write + Close
where
Self: 'm;
/// Future returned by `connect` function.
type ConnectFuture<'m>: Future<Output = Result<Self::TcpConnection<'m>, Self::Error>> + 'm
where
Self: 'm;
/// Connect to the given remote host and port.
///
/// Returns `Ok` if the connection was successful.
fn connect<'m>(&'m mut self, remote: SocketAddr) -> Self::ConnectFuture<'m>;
/// Future returned by `is_connected` function.
type IsConnectedFuture<'m>: Future<Output = Result<bool, Self::Error>> + 'm
where
Self: 'm;
/// Check if this socket is connected
fn is_connected<'m>(&'m mut self) -> Self::IsConnectedFuture<'m>;
}
/// This trait is implemented by TCP/IP stacks. You could, for example, have an implementation
/// which knows how to send AT commands to an ESP8266 WiFi module. You could have another implementation
/// which knows how to driver the Rust Standard Library's `std::net` module. Given this trait, you can
/// write a portable HTTP client which can work with either implementation.
pub trait TcpClientStack {
/// The type returned when we create a new TCP socket
type TcpClient: TcpClient;
/// The type returned when we have an error
type Error: core::fmt::Debug;
/// Create future
type CreateFuture<'m>: Future<Output = Result<Self::TcpClient, Self::Error>> + 'm
where
Self: 'm;
/// Open a Create for usage as a TCP client.
///
/// The Create must be connected before it can be used.
///
/// Returns `Ok(Create)` if the Create was successfully created.
fn create<'m>(&'m mut self) -> Self::CreateFuture<'m>;
}
/// Docs
pub trait Close: embedded_io::Io {
/// Future returned by `close` function.
type CloseFuture<'m>: Future<Output = Result<(), Self::Error>>;
/// Close an existing TCP socket.
fn close<'m>(self) -> Self::CloseFuture<'m>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment