Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic trait to combine UnixListener and TcpListener #4385

Merged
merged 17 commits into from Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio-util/src/lib.rs
Expand Up @@ -31,6 +31,7 @@ cfg_codec! {

cfg_net! {
pub mod udp;
pub mod net;
}

cfg_compat! {
Expand Down
4 changes: 4 additions & 0 deletions tokio-util/src/net/mod.rs
@@ -0,0 +1,4 @@
//! TCP/UDP/Unix helpers for tokio.

#[cfg(unix)]
pub mod unix;
76 changes: 76 additions & 0 deletions tokio-util/src/net/unix/mod.rs
@@ -0,0 +1,76 @@
//! Unix domain socket helpers.

use std::future::Future;
use std::io::Result;
use std::pin::Pin;

/// Listening address.
#[derive(Debug)]
pub enum ListenAddr {
/// Socket address.
SocketAddr(std::net::SocketAddr),
/// Unix socket.
UnixSocket(tokio::net::unix::SocketAddr),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an associated type of the Listener trait like how Io is set up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 9bda024


impl From<std::net::SocketAddr> for ListenAddr {
fn from(addr: std::net::SocketAddr) -> Self {
Self::SocketAddr(addr)
}
}

impl From<tokio::net::unix::SocketAddr> for ListenAddr {
fn from(addr: tokio::net::unix::SocketAddr) -> Self {
Self::UnixSocket(addr)
}
}

/// A trait for a listener: `TcpListener` and `UnixListener`.
pub trait Listener: Send + Unpin {
/// The stream's type of this listener.
type Io: tokio::io::AsyncRead + tokio::io::AsyncWrite;

/// Accepts a new incoming connection from this listener.
fn accept<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(Self::Io, ListenAddr)>> + Send + 'a>>;

/// Returns the local address that this listener is bound to.
fn local_addr(&self) -> Result<ListenAddr>;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the trait should look more like this:

Suggested change
/// A trait for a listener: `TcpListener` and `UnixListener`.
pub trait Listener: Send + Unpin {
/// The stream's type of this listener.
type Io: tokio::io::AsyncRead + tokio::io::AsyncWrite;
/// Accepts a new incoming connection from this listener.
fn accept<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(Self::Io, ListenAddr)>> + Send + 'a>>;
/// Returns the local address that this listener is bound to.
fn local_addr(&self) -> Result<ListenAddr>;
}
/// A trait for a listener: `TcpListener` and `UnixListener`.
pub trait Listener {
/// The stream's type of this listener.
type Io: tokio::io::AsyncRead + tokio::io::AsyncWrite;
/// The address of the new connection.
type Addr;
fn poll_accept(self: Pin<&mut self>) -> Poll<(Self::Io, Self::ListenAddr)>;
/// Returns the local address that this listener is bound to.
fn local_addr(&self) -> Result<ListenAddr>;
/// Accepts a new incoming connection from this listener.
fn accept<'a>(&'a mut self) -> ListenerAcceptFut<'a, Self>
where
Self: Sized + Unpin,
{
ListenerAcceptFut::new(self)
}
}

Then, the crate would define a ListenAcceptFut type that implements Future with a manual impl similar to the return types in the Ext traits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c2ec467


impl Listener for tokio::net::TcpListener {
type Io = tokio::net::TcpStream;

fn accept<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(Self::Io, ListenAddr)>> + Send + 'a>> {
let accept = self.accept();
Box::pin(async {
let (stream, addr) = accept.await?;
Ok((stream, addr.into()))
})
}

fn local_addr(&self) -> Result<ListenAddr> {
self.local_addr().map(Into::into)
}
}

impl Listener for tokio::net::UnixListener {
type Io = tokio::net::UnixStream;

fn accept<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = Result<(Self::Io, ListenAddr)>> + Send + 'a>> {
let accept = self.accept();
Box::pin(async {
let (stream, addr) = accept.await?;
Ok((stream, addr.into()))
})
}

fn local_addr(&self) -> Result<ListenAddr> {
self.local_addr().map(Into::into)
}
}