Skip to content

Commit ad7ab58

Browse files
fix: remove unix on win (#96)
1 parent e4f0891 commit ad7ab58

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

silent/src/core/listener.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::core::connection::Connection;
44
use futures_util::StreamExt;
55
use futures_util::stream::FuturesUnordered;
66
use std::io::Result;
7+
#[cfg(not(target_os = "windows"))]
78
use std::path::Path;
89
use std::pin::Pin;
910
#[cfg(feature = "tls")]
@@ -20,6 +21,7 @@ pub trait Listen: Send + Sync {
2021

2122
pub enum Listener {
2223
TcpListener(tokio::net::TcpListener),
24+
#[cfg(not(target_os = "windows"))]
2325
UnixListener(tokio::net::UnixListener),
2426
}
2527

@@ -34,6 +36,7 @@ impl From<std::net::TcpListener> for Listener {
3436
}
3537
}
3638

39+
#[cfg(not(target_os = "windows"))]
3740
impl From<std::os::unix::net::UnixListener> for Listener {
3841
fn from(value: std::os::unix::net::UnixListener) -> Self {
3942
Listener::UnixListener(
@@ -48,6 +51,7 @@ impl From<tokio::net::TcpListener> for Listener {
4851
}
4952
}
5053

54+
#[cfg(not(target_os = "windows"))]
5155
impl From<tokio::net::UnixListener> for Listener {
5256
fn from(value: tokio::net::UnixListener) -> Self {
5357
Listener::UnixListener(value)
@@ -67,6 +71,7 @@ impl Listen for Listener {
6771
};
6872
Box::pin(accept_future)
6973
}
74+
#[cfg(not(target_os = "windows"))]
7075
Listener::UnixListener(listener) => {
7176
let accept_future = async move {
7277
let (stream, addr) = listener.accept().await?;
@@ -83,6 +88,7 @@ impl Listen for Listener {
8388
fn local_addr(&self) -> Result<SocketAddr> {
8489
match self {
8590
Listener::TcpListener(listener) => listener.local_addr().map(SocketAddr::Tcp),
91+
#[cfg(not(target_os = "windows"))]
8692
Listener::UnixListener(listener) => Ok(SocketAddr::Unix(listener.local_addr()?.into())),
8793
}
8894
}
@@ -150,6 +156,7 @@ impl ListenersBuilder {
150156
)));
151157
}
152158

159+
#[cfg(not(target_os = "windows"))]
153160
pub fn bind_unix<P: AsRef<Path>>(&mut self, path: P) {
154161
self.listeners.push(Box::new(Listener::from(
155162
std::os::unix::net::UnixListener::bind(path).expect("failed to bind listener"),

silent/src/core/socket_addr.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub enum SocketAddr {
77
Tcp(std::net::SocketAddr),
88
#[cfg(feature = "tls")]
99
TlsTcp(std::net::SocketAddr),
10+
#[cfg(not(target_os = "windows"))]
1011
Unix(std::os::unix::net::SocketAddr),
1112
}
1213

@@ -26,6 +27,7 @@ impl Debug for SocketAddr {
2627
SocketAddr::Tcp(addr) => write!(f, "http://{}", addr),
2728
#[cfg(feature = "tls")]
2829
SocketAddr::TlsTcp(addr) => write!(f, "https://{}", addr),
30+
#[cfg(not(target_os = "windows"))]
2931
SocketAddr::Unix(addr) => write!(f, "UnixSocketAddr({:?})", addr),
3032
}
3133
}
@@ -38,6 +40,7 @@ impl Display for SocketAddr {
3840
SocketAddr::Tcp(addr) => write!(f, "{}", addr),
3941
#[cfg(feature = "tls")]
4042
SocketAddr::TlsTcp(addr) => write!(f, "{}", addr),
43+
#[cfg(not(target_os = "windows"))]
4144
SocketAddr::Unix(addr) => {
4245
write!(f, "{:?}", addr.as_pathname())
4346
}
@@ -51,6 +54,7 @@ impl From<std::net::SocketAddr> for SocketAddr {
5154
}
5255
}
5356

57+
#[cfg(not(target_os = "windows"))]
5458
impl From<std::os::unix::net::SocketAddr> for SocketAddr {
5559
fn from(addr: std::os::unix::net::SocketAddr) -> Self {
5660
SocketAddr::Unix(addr)
@@ -60,6 +64,7 @@ impl From<std::os::unix::net::SocketAddr> for SocketAddr {
6064
impl FromStr for SocketAddr {
6165
type Err = std::io::Error;
6266

67+
#[cfg(not(target_os = "windows"))]
6368
fn from_str(s: &str) -> Result<Self> {
6469
if let Ok(addr) = s.parse::<std::net::SocketAddr>() {
6570
Ok(SocketAddr::Tcp(addr))
@@ -72,19 +77,34 @@ impl FromStr for SocketAddr {
7277
))
7378
}
7479
}
80+
#[cfg(target_os = "windows")]
81+
fn from_str(s: &str) -> Result<Self> {
82+
if let Ok(addr) = s.parse::<std::net::SocketAddr>() {
83+
Ok(SocketAddr::Tcp(addr))
84+
} else {
85+
Err(std::io::Error::new(
86+
std::io::ErrorKind::InvalidInput,
87+
"invalid socket address",
88+
))
89+
}
90+
}
7591
}
7692

7793
#[cfg(test)]
7894
mod tests {
7995
use crate::core::socket_addr::SocketAddr;
80-
use std::path::Path;
8196

8297
#[test]
83-
fn test_socket_addr() {
98+
fn test_tcp_socket_addr() {
8499
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
85100
let socket_addr = SocketAddr::from(addr);
86101
assert_eq!(format!("{}", socket_addr), "127.0.0.1:8080");
102+
}
87103

104+
#[cfg(not(target_os = "windows"))]
105+
#[test]
106+
fn test_unix_socket_addr() {
107+
use std::path::Path;
88108
let _ = std::fs::remove_file("/tmp/sock");
89109
let addr = std::os::unix::net::SocketAddr::from_pathname("/tmp/sock").unwrap();
90110
let socket_addr = SocketAddr::from(addr);

silent/src/core/stream.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@ use std::io::Error;
44
use std::pin::Pin;
55
use std::task::{Context, Poll};
66
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
7-
use tokio::net::{TcpStream, UnixStream};
7+
use tokio::net::TcpStream;
8+
#[cfg(not(target_os = "windows"))]
9+
use tokio::net::UnixStream;
810

911
pub enum Stream {
1012
TcpStream(TcpStream),
13+
#[cfg(not(target_os = "windows"))]
1114
UnixStream(UnixStream),
1215
}
1316

1417
impl Stream {
1518
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
1619
match self {
1720
Stream::TcpStream(s) => Ok(s.peer_addr()?.into()),
21+
#[cfg(not(target_os = "windows"))]
1822
Stream::UnixStream(s) => Ok(SocketAddr::Unix(s.peer_addr()?.into())),
1923
}
2024
}
@@ -28,6 +32,7 @@ impl AsyncRead for Stream {
2832
) -> Poll<io::Result<()>> {
2933
match self.get_mut() {
3034
Stream::TcpStream(s) => Pin::new(s).poll_read(cx, buf),
35+
#[cfg(not(target_os = "windows"))]
3136
Stream::UnixStream(s) => Pin::new(s).poll_read(cx, buf),
3237
}
3338
}
@@ -41,20 +46,23 @@ impl AsyncWrite for Stream {
4146
) -> Poll<Result<usize, Error>> {
4247
match self.get_mut() {
4348
Stream::TcpStream(s) => Pin::new(s).poll_write(cx, buf),
49+
#[cfg(not(target_os = "windows"))]
4450
Stream::UnixStream(s) => Pin::new(s).poll_write(cx, buf),
4551
}
4652
}
4753

4854
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
4955
match self.get_mut() {
5056
Stream::TcpStream(s) => Pin::new(s).poll_flush(cx),
57+
#[cfg(not(target_os = "windows"))]
5158
Stream::UnixStream(s) => Pin::new(s).poll_flush(cx),
5259
}
5360
}
5461

5562
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
5663
match self.get_mut() {
5764
Stream::TcpStream(s) => Pin::new(s).poll_shutdown(cx),
65+
#[cfg(not(target_os = "windows"))]
5866
Stream::UnixStream(s) => Pin::new(s).poll_shutdown(cx),
5967
}
6068
}

silent/src/service/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::route::RouteService;
99
use crate::scheduler::{SCHEDULER, Scheduler, middleware::SchedulerMiddleware};
1010
use crate::service::serve::Serve;
1111
use std::net::SocketAddr;
12+
#[cfg(not(target_os = "windows"))]
1213
use std::path::Path;
1314
use tokio::signal;
1415
use tokio::task::JoinSet;
@@ -52,6 +53,7 @@ impl Server {
5253
self
5354
}
5455

56+
#[cfg(not(target_os = "windows"))]
5557
#[inline]
5658
pub fn bind_unix<P: AsRef<Path>>(mut self, path: P) -> Self {
5759
self.listeners_builder.bind_unix(path);

0 commit comments

Comments
 (0)