Skip to content

Commit

Permalink
Merge pull request #20 from tmccombs/net-refactor
Browse files Browse the repository at this point in the history
Refactor tokio-net features into seperate mod
  • Loading branch information
tmccombs committed Mar 20, 2022
2 parents 8cf2162 + 21fc694 commit 49a011c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{Deref, DerefMut};
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-h1", feature = "hyper-h2"))))]
impl AsyncAccept for AddrIncoming {
type Connection = AddrStream;
type Error = io::Error;
type Error = std::io::Error;

fn poll_accept(
self: Pin<&mut Self>,
Expand Down
42 changes: 4 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use pin_project_lite::pin_project;
#[cfg(feature = "rt")]
pub use spawning_handshake::SpawningHandshakes;
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
Expand All @@ -35,6 +34,9 @@ mod spawning_handshake;
#[cfg(any(feature = "hyper-h1", feature = "hyper-h2"))]
pub mod hyper;

#[cfg(feature = "tokio-net")]
mod net;

/// Default number of concurrent handshakes
pub const DEFAULT_MAX_HANDSHAKES: usize = 64;
/// Default timeout for the TLS handshake.
Expand Down Expand Up @@ -228,7 +230,7 @@ where
#[cfg(feature = "rustls")]
impl<C: AsyncRead + AsyncWrite + Unpin> AsyncTls<C> for tokio_rustls::TlsAcceptor {
type Stream = tokio_rustls::server::TlsStream<C>;
type Error = io::Error;
type Error = std::io::Error;
type AcceptFuture = tokio_rustls::Accept<C>;

fn accept(&self, conn: C) -> Self::AcceptFuture {
Expand Down Expand Up @@ -305,42 +307,6 @@ pub fn builder<T>(tls: T) -> Builder<T> {
}
}

#[cfg(feature = "tokio-net")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
impl AsyncAccept for tokio::net::TcpListener {
type Connection = tokio::net::TcpStream;
type Error = io::Error;

fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Connection, Self::Error>>> {
match (*self).poll_accept(cx) {
Poll::Ready(Ok((stream, _))) => Poll::Ready(Some(Ok(stream))),
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Pending => Poll::Pending,
}
}
}

#[cfg(all(unix, feature = "tokio-net"))]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
impl AsyncAccept for tokio::net::UnixListener {
type Connection = tokio::net::UnixStream;
type Error = io::Error;

fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Connection, Self::Error>>> {
match (*self).poll_accept(cx) {
Poll::Ready(Ok((stream, _))) => Poll::Ready(Some(Ok(stream))),
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Pending => Poll::Pending,
}
}
}

pin_project! {
/// See [`AsyncAccept::until`]
pub struct Until<A, E> {
Expand Down
41 changes: 41 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use super::AsyncAccept;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::net::{TcpListener, TcpStream, UnixListener, UnixStream};

#[cfg(feature = "tokio-net")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
impl AsyncAccept for TcpListener {
type Connection = TcpStream;
type Error = io::Error;

fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Connection, Self::Error>>> {
match (*self).poll_accept(cx) {
Poll::Ready(Ok((stream, _))) => Poll::Ready(Some(Ok(stream))),
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Pending => Poll::Pending,
}
}
}

#[cfg(all(unix, feature = "tokio-net"))]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio-net")))]
impl AsyncAccept for UnixListener {
type Connection = UnixStream;
type Error = io::Error;

fn poll_accept(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Connection, Self::Error>>> {
match (*self).poll_accept(cx) {
Poll::Ready(Ok((stream, _))) => Poll::Ready(Some(Ok(stream))),
Poll::Ready(Err(e)) => Poll::Ready(Some(Err(e))),
Poll::Pending => Poll::Pending,
}
}
}

0 comments on commit 49a011c

Please sign in to comment.