use crate::{
io::{rx, tx},
path::{self, mtu},
time::{Clock, Timestamp},
};
use core::{
fmt,
future::Future,
task::{Context, Poll},
};
pub mod limits;
pub use limits::Limiter;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Type {
Client,
Server,
}
impl Type {
pub fn is_client(self) -> bool {
self == Self::Client
}
pub fn is_server(self) -> bool {
self == Self::Server
}
#[must_use]
pub fn peer_type(self) -> Self {
match self {
Self::Client => Self::Server,
Self::Server => Self::Client,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Location {
Local,
Remote,
}
impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Local => write!(f, "the local endpoint"),
Self::Remote => write!(f, "the remote endpoint"),
}
}
}
impl Location {
pub fn is_local(self) -> bool {
self == Self::Local
}
pub fn is_remote(self) -> bool {
self == Self::Remote
}
#[must_use]
pub fn peer_type(self) -> Self {
match self {
Self::Local => Self::Remote,
Self::Remote => Self::Local,
}
}
}
pub trait Endpoint: 'static + Send + Sized {
type PathHandle: path::Handle;
type Subscriber: crate::event::Subscriber;
const ENDPOINT_TYPE: Type;
fn receive<Rx, C>(&mut self, rx: &mut Rx, clock: &C)
where
Rx: rx::Queue<Handle = Self::PathHandle>,
C: Clock;
fn transmit<Tx, C>(&mut self, tx: &mut Tx, clock: &C)
where
Tx: tx::Queue<Handle = Self::PathHandle>,
C: Clock;
fn wakeups<'a, C: Clock>(&'a mut self, clock: &'a C) -> Wakeups<'a, Self, C> {
Wakeups {
endpoint: self,
clock,
}
}
fn poll_wakeups<C: Clock>(
&mut self,
cx: &mut Context<'_>,
clock: &C,
) -> Poll<Result<usize, CloseError>>;
fn timeout(&self) -> Option<Timestamp>;
fn set_mtu_config(&mut self, mtu_config: mtu::Config);
fn subscriber(&mut self) -> &mut Self::Subscriber;
}
pub struct Wakeups<'a, E: Endpoint, C: Clock> {
endpoint: &'a mut E,
clock: &'a C,
}
impl<E: Endpoint, C: Clock> Future for Wakeups<'_, E, C> {
type Output = Result<usize, CloseError>;
fn poll(mut self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let clock = self.clock;
self.endpoint.poll_wakeups(cx, clock)
}
}
#[derive(Clone, Copy, Debug)]
pub struct CloseError;