#![no_std]
use core::{future::Future, net::SocketAddr};
pub trait AsyncBackend {
type Error;
type Listener<'a>: AsyncListener<Connection<'a> = Self::Connection<'a>, Error = Self::Error>
where
Self: 'a;
type Connection<'a>: AsyncConnection<Error = Self::Error>
where
Self: 'a;
fn bind(addr: SocketAddr) -> Result<Self, Self::Error>
where
Self: Sized;
fn listen(&self) -> Result<Self::Listener<'_>, Self::Error>;
fn connect(
&self,
addr: SocketAddr,
) -> impl Future<Output = Result<Self::Connection<'_>, Self::Error>> + '_;
}
pub trait AsyncListener {
type Error;
type Connection<'a>: AsyncConnection<Error = Self::Error>
where
Self: 'a;
fn accept(
&self,
) -> impl Future<Output = Result<(Self::Connection<'_>, SocketAddr), Self::Error>> + '_;
fn close(self) -> impl Future<Output = Result<(), Self::Error>>;
}
pub trait AsyncConnection {
type Error;
fn read<'fut>(
&'fut mut self,
buf: &'fut mut [u8],
) -> impl Future<Output = Result<usize, Self::Error>> + 'fut;
fn write<'fut>(
&'fut mut self,
buf: &'fut [u8],
) -> impl Future<Output = Result<usize, Self::Error>> + 'fut;
fn poll_readable(&self, cx: &mut core::task::Context<'_>) -> bool;
fn poll_writable(&self, cx: &mut core::task::Context<'_>) -> bool;
fn close(self) -> impl Future<Output = Result<(), Self::Error>>;
}