usecrate::either::Either; use std::future::Future; use std::io::Result; use std::pin::Pin; use std::task::{Context, Poll};
#[cfg(unix)] pubmod unix;
/// A trait for a listener: `TcpListener` and `UnixListener`. pubtrait Listener { /// The stream's type of this listener. type Io: tokio::io::AsyncRead + tokio::io::AsyncWrite; /// The socket address type of this listener. type Addr;
/// Polls to accept a new incoming connection to this listener. fn poll_accept(&mutself, cx: &mut Context<'_>) -> Poll<Result<(Self::Io, Self::Addr)>>;
/// Accepts a new incoming connection from this listener. fn accept(&mutself) -> ListenerAcceptFut<'_, Self> where Self: Sized,
{
ListenerAcceptFut { listener: self }
}
/// Returns the local address that this listener is bound to. fn local_addr(&self) -> Result<Self::Addr>;
}
impl Listener for tokio::net::TcpListener { type Io = tokio::net::TcpStream; type Addr = std::net::SocketAddr;
/// Future for accepting a new connection from a listener. #[derive(Debug)] #[must_use = "futures do nothing unless you `.await` or poll them"] pubstruct ListenerAcceptFut<'a, L> {
listener: &'a mut L,
}
impl<'a, L> Future for ListenerAcceptFut<'a, L> where
L: Listener,
{ type Output = Result<(L::Io, L::Addr)>;
impl<L, R> Either<L, R> where
L: Listener,
R: Listener,
{ /// Accepts a new incoming connection from this listener. pubasyncfn accept(&mutself) -> Result<Either<(L::Io, L::Addr), (R::Io, R::Addr)>> { matchself {
Either::Left(listener) => { let (stream, addr) = listener.accept().await?;
Ok(Either::Left((stream, addr)))
}
Either::Right(listener) => { let (stream, addr) = listener.accept().await?;
Ok(Either::Right((stream, addr)))
}
}
}
/// Returns the local address that this listener is bound to. pubfn local_addr(&self) -> Result<Either<L::Addr, R::Addr>> { matchself {
Either::Left(listener) => { let addr = listener.local_addr()?;
Ok(Either::Left(addr))
}
Either::Right(listener) => { let addr = listener.local_addr()?;
Ok(Either::Right(addr))
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.9 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.