use std::{
fmt::Debug,
io::{self, ErrorKind},
net::{SocketAddr, UdpSocket},
task::{Context, Poll},
};
pub trait UdpTransport: Send + Sync + Debug + Sized + 'static {
fn from_std(socket: UdpSocket) -> io::Result<Self>;
fn local_addr(&self) -> io::Result<SocketAddr>;
fn poll_recv_io<R>(
&self,
cx: &mut Context<'_>,
recv: impl FnMut(&Self) -> io::Result<R>,
) -> Poll<io::Result<R>>;
fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
fn try_send_io<R>(&self, send: impl FnOnce(&Self) -> io::Result<R>) -> io::Result<R>;
fn max_transmit_segments(&self) -> usize {
1
}
fn max_receive_segments(&self) -> usize {
1
}
fn may_fragment(&self) -> bool {
true
}
}
fn unsupported() -> io::Error {
io::Error::new(ErrorKind::Unsupported, "UDP not supported by this runtime")
}
impl UdpTransport for () {
fn from_std(_: UdpSocket) -> io::Result<Self> {
Err(unsupported())
}
fn local_addr(&self) -> io::Result<SocketAddr> {
Err(unsupported())
}
fn poll_recv_io<R>(
&self,
_: &mut Context<'_>,
_: impl FnMut(&Self) -> io::Result<R>,
) -> Poll<io::Result<R>> {
Poll::Ready(Err(unsupported()))
}
fn poll_writable(&self, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Err(unsupported()))
}
fn try_send_io<R>(&self, _: impl FnOnce(&Self) -> io::Result<R>) -> io::Result<R> {
Err(unsupported())
}
}