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
Changes from 3 commits
4305804
69b0379
cd1ddf9
9bda024
d0dba7a
c2ec467
8afdc3e
75dcc76
4a46da1
fe231ec
812dfa5
a538f0b
e194407
b639d98
27912b7
316f015
61bd0fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ cfg_codec! { | |
|
||
cfg_net! { | ||
pub mod udp; | ||
pub mod net; | ||
} | ||
|
||
cfg_compat! { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! TCP/UDP/Unix helpers for tokio. | ||
|
||
#[cfg(unix)] | ||
pub mod unix; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the trait should look more like this:
Suggested change
Then, the crate would define a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
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 howIo
is set up.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 9bda024