From 596788f2ade15b9b952a8621326593d587553a5d Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 31 Oct 2021 22:55:33 +0000 Subject: [PATCH] Listener::local_addr() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for transfer's current commit: Half blessing − 半吉 --- src/ext.rs | 7 +++++++ src/main.rs | 1 + src/sock.rs | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/ext.rs diff --git a/src/ext.rs b/src/ext.rs new file mode 100644 index 0000000..9682b56 --- /dev/null +++ b/src/ext.rs @@ -0,0 +1,7 @@ + + +#[macro_export] macro_rules! show_size { + ($t:ty) => { + const _: [(); 0] = [(); ::std::mem::size_of::<$t>()]; + } +} diff --git a/src/main.rs b/src/main.rs index b99b103..e2e0bd9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ use color_eyre::{ }; use futures::Future; +mod ext; use ext::*; mod sock; mod store; mod config; diff --git a/src/sock.rs b/src/sock.rs index 30b9b1b..22204a4 100644 --- a/src/sock.rs +++ b/src/sock.rs @@ -5,6 +5,7 @@ use std::io; use std::path::{ Path, PathBuf }; +use std::{fmt, error}; #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct SocketAddrUnix @@ -41,6 +42,15 @@ impl TryFrom for SocketAddrUnix from.as_pathname().ok_or(AddrParseError).map(|path| Self{path: path.into()}) } } +impl TryFrom for SocketAddr +{ + type Error = AddrParseError; + + fn try_from(from: tokio::net::unix::SocketAddr) -> Result + { + SocketAddrUnix::try_from(from).map(Self::Unix) + } +} impl From for SocketAddr { @@ -61,6 +71,16 @@ impl From for SocketAddr #[derive(Debug, Clone, PartialEq, Eq)] pub struct AddrParseError; +impl error::Error for AddrParseError{} +impl fmt::Display for AddrParseError +{ + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result + { + write!(f, "failed to parse address") + } +} + + impl From for AddrParseError { fn from(_: std::net::AddrParseError) -> Self @@ -98,7 +118,7 @@ enum StreamInner impl ListenerInner { - fn con_unix(sock: &SocketAddrUnix) -> io::Result + #[inline] fn con_unix(sock: &SocketAddrUnix) -> io::Result { tokio::net::UnixListener::bind(&sock.path).map(Self::Unix) } @@ -155,6 +175,22 @@ impl Listener ListenerInner::Tcp(tcp) => tcp.accept().await.map(Into::into), } } + + /// Local bound address + pub fn local_addr(&self) -> io::Result + { + match self.0.as_ref() + { + ListenerInner::Unix(un) => un.local_addr().and_then(|addr| addr.try_into().map_err(|e| io::Error::new(io::ErrorKind::Unsupported, e))), + ListenerInner::Tcp(t) => t.local_addr().map(Into::into) + } + } + + // TODO: Add `poll_accept` for unpinned `&self` polling } //TODO: impl Stream +impl Stream +{ + +}