use std::future::Future;
use std::io;
use std::net::SocketAddr;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::TcpListener;
#[cfg(unix)]
use tokio::net::UnixListener;
pub trait Listener: Send + 'static {
type Io: AsyncRead + AsyncWrite + Unpin + Send + 'static;
type Addr: Send;
fn accept(&mut self) -> impl Future<Output = io::Result<(Self::Io, Self::Addr)>> + Send;
fn tcp_addr(&self) -> Option<SocketAddr> {
None
}
}
impl Listener for TcpListener {
type Io = tokio::net::TcpStream;
type Addr = SocketAddr;
async fn accept(&mut self) -> io::Result<(Self::Io, Self::Addr)> {
TcpListener::accept(self).await
}
fn tcp_addr(&self) -> Option<SocketAddr> {
TcpListener::local_addr(self).ok()
}
}
#[cfg(unix)]
impl Listener for UnixListener {
type Io = tokio::net::UnixStream;
type Addr = tokio::net::unix::SocketAddr;
async fn accept(&mut self) -> io::Result<(Self::Io, Self::Addr)> {
UnixListener::accept(self).await
}
}