Skip to content

Commit

Permalink
using ci to figure out if i missed any spots
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Jun 22, 2020
1 parent 578d372 commit 3ab3a72
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/listener/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod multi_listener;
mod tcp_listener;
mod to_listener;
#[cfg(unix)]
mod unix_listener;

use crate::utils::BoxFuture;
Expand All @@ -11,6 +12,7 @@ pub use multi_listener::MultiListener;
pub use to_listener::ToListener;

pub(crate) use tcp_listener::TcpListener;
#[cfg(unix)]
pub(crate) use unix_listener::UnixListener;

pub trait Listener<State: 'static>: std::fmt::Debug + Send + Sync + 'static {
Expand Down
48 changes: 41 additions & 7 deletions src/listener/to_listener.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::{Listener, MultiListener, TcpListener, UnixListener};
#[cfg(unix)]
use super::UnixListener;
use super::{Listener, MultiListener, TcpListener};
use crate::http::url::Url;
use crate::{utils::BoxFuture, Server};
use async_std::io;
Expand All @@ -11,13 +13,15 @@ pub trait ToListener<State: Send + Sync + 'static> {

#[derive(Debug)]
pub enum ParsedListener {
#[cfg(unix)]
Unix(UnixListener),
Tcp(TcpListener),
}

impl<State: Send + Sync + 'static> Listener<State> for ParsedListener {
fn listen<'a>(&'a mut self, app: Server<State>) -> BoxFuture<'a, io::Result<()>> {
match self {
#[cfg(unix)]
Self::Unix(u) => u.listen(app),
Self::Tcp(t) => t.listen(app),
}
Expand All @@ -30,12 +34,23 @@ impl<State: Send + Sync + 'static> ToListener<State> for Url {
fn to_listener(self) -> io::Result<Self::Listener> {
match self.scheme() {
"unix" | "file" | "http+unix" => {
let path = std::path::PathBuf::from(format!(
"{}{}",
self.domain().unwrap_or_default(),
self.path()
));
Ok(ParsedListener::Unix(UnixListener::from_path(path)))
#[cfg(unix)]
{
let path = std::path::PathBuf::from(format!(
"{}{}",
self.domain().unwrap_or_default(),
self.path()
));
return Ok(ParsedListener::Unix(UnixListener::from_path(path)));
}

#[cfg(not(unix))]
{
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Unix sockets not supported on this platform",
))
}
}

"tcp" | "http" => Ok(ParsedListener::Tcp(TcpListener::from_addrs(
Expand Down Expand Up @@ -82,13 +97,15 @@ impl<State: Send + Sync + 'static> ToListener<State> for &str {
}
}

#[cfg(unix)]
impl<State: Send + Sync + 'static> ToListener<State> for async_std::path::PathBuf {
type Listener = UnixListener;
fn to_listener(self) -> io::Result<Self::Listener> {
Ok(UnixListener::from_path(self))
}
}

#[cfg(unix)]
impl<State: Send + Sync + 'static> ToListener<State> for std::path::PathBuf {
type Listener = UnixListener;
fn to_listener(self) -> io::Result<Self::Listener> {
Expand Down Expand Up @@ -118,13 +135,15 @@ impl<State: Send + Sync + 'static> ToListener<State> for (&str, u16) {
}
}

#[cfg(unix)]
impl<State: Send + Sync + 'static> ToListener<State> for async_std::os::unix::net::UnixListener {
type Listener = UnixListener;
fn to_listener(self) -> io::Result<Self::Listener> {
Ok(UnixListener::from_listener(self))
}
}

#[cfg(unix)]
impl<State: Send + Sync + 'static> ToListener<State> for std::os::unix::net::UnixListener {
type Listener = UnixListener;
fn to_listener(self) -> io::Result<Self::Listener> {
Expand All @@ -139,6 +158,7 @@ impl<State: Send + Sync + 'static> ToListener<State> for TcpListener {
}
}

#[cfg(unix)]
impl<State: Send + Sync + 'static> ToListener<State> for UnixListener {
type Listener = Self;
fn to_listener(self) -> io::Result<Self::Listener> {
Expand All @@ -153,6 +173,20 @@ impl<State: Send + Sync + 'static> ToListener<State> for MultiListener<State> {
}
}

impl<State: Send + Sync + 'static> ToListener<State> for ParsedListener {
type Listener = Self;
fn to_listener(self) -> io::Result<Self::Listener> {
Ok(self)
}
}

impl<State: Send + Sync + 'static> ToListener<State> for std::net::SocketAddr {
type Listener = TcpListener;
fn to_listener(self) -> io::Result<Self::Listener> {
Ok(TcpListener::from_addrs(vec![self]))
}
}

impl<TL: ToListener<State>, State: Send + Sync + 'static> ToListener<State> for Vec<TL> {
type Listener = MultiListener<State>;
fn to_listener(self) -> io::Result<Self::Listener> {
Expand Down

0 comments on commit 3ab3a72

Please sign in to comment.