Listener::local_addr()

Fortune for transfer's current commit: Half blessing − 半吉
master
Avril 3 years ago
parent 7a8b5e669d
commit 596788f2ad
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,7 @@
#[macro_export] macro_rules! show_size {
($t:ty) => {
const _: [(); 0] = [(); ::std::mem::size_of::<$t>()];
}
}

@ -19,6 +19,7 @@ use color_eyre::{
}; };
use futures::Future; use futures::Future;
mod ext; use ext::*;
mod sock; mod sock;
mod store; mod store;
mod config; mod config;

@ -5,6 +5,7 @@ use std::io;
use std::path::{ use std::path::{
Path, PathBuf Path, PathBuf
}; };
use std::{fmt, error};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SocketAddrUnix pub struct SocketAddrUnix
@ -41,6 +42,15 @@ impl TryFrom<tokio::net::unix::SocketAddr> for SocketAddrUnix
from.as_pathname().ok_or(AddrParseError).map(|path| Self{path: path.into()}) from.as_pathname().ok_or(AddrParseError).map(|path| Self{path: path.into()})
} }
} }
impl TryFrom<tokio::net::unix::SocketAddr> for SocketAddr
{
type Error = AddrParseError;
fn try_from(from: tokio::net::unix::SocketAddr) -> Result<Self, Self::Error>
{
SocketAddrUnix::try_from(from).map(Self::Unix)
}
}
impl From<SocketAddrUnix> for SocketAddr impl From<SocketAddrUnix> for SocketAddr
{ {
@ -61,6 +71,16 @@ impl From<std::net::SocketAddr> for SocketAddr
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddrParseError; 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<std::net::AddrParseError> for AddrParseError impl From<std::net::AddrParseError> for AddrParseError
{ {
fn from(_: std::net::AddrParseError) -> Self fn from(_: std::net::AddrParseError) -> Self
@ -98,7 +118,7 @@ enum StreamInner
impl ListenerInner impl ListenerInner
{ {
fn con_unix(sock: &SocketAddrUnix) -> io::Result<Self> #[inline] fn con_unix(sock: &SocketAddrUnix) -> io::Result<Self>
{ {
tokio::net::UnixListener::bind(&sock.path).map(Self::Unix) tokio::net::UnixListener::bind(&sock.path).map(Self::Unix)
} }
@ -155,6 +175,22 @@ impl Listener
ListenerInner::Tcp(tcp) => tcp.accept().await.map(Into::into), ListenerInner::Tcp(tcp) => tcp.accept().await.map(Into::into),
} }
} }
/// Local bound address
pub fn local_addr(&self) -> io::Result<SocketAddr>
{
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 //TODO: impl Stream
impl Stream
{
}

Loading…
Cancel
Save