Skip to content

Commit ddce2d6

Browse files
authored
Reduce cfg flags in actix_server::socket (#325)
1 parent 0a11cf5 commit ddce2d6

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

actix-server/src/socket.rs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,7 @@ pub(crate) use {
1212
use std::{fmt, io};
1313

1414
use actix_rt::net::TcpStream;
15-
use mio::event::Source;
16-
use mio::net::TcpStream as MioTcpStream;
17-
use mio::{Interest, Registry, Token};
18-
19-
#[cfg(windows)]
20-
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
21-
#[cfg(unix)]
22-
use {
23-
actix_rt::net::UnixStream,
24-
mio::net::{SocketAddr as MioSocketAddr, UnixStream as MioUnixStream},
25-
std::os::unix::io::{FromRawFd, IntoRawFd},
26-
};
15+
use mio::{event::Source, Interest, Registry, Token};
2716

2817
pub(crate) enum MioListener {
2918
Tcp(MioTcpListener),
@@ -131,7 +120,7 @@ impl fmt::Display for MioListener {
131120
pub(crate) enum SocketAddr {
132121
Tcp(StdSocketAddr),
133122
#[cfg(unix)]
134-
Uds(MioSocketAddr),
123+
Uds(mio::net::SocketAddr),
135124
}
136125

137126
impl fmt::Display for SocketAddr {
@@ -156,57 +145,70 @@ impl fmt::Debug for SocketAddr {
156145

157146
#[derive(Debug)]
158147
pub enum MioStream {
159-
Tcp(MioTcpStream),
148+
Tcp(mio::net::TcpStream),
160149
#[cfg(unix)]
161-
Uds(MioUnixStream),
150+
Uds(mio::net::UnixStream),
162151
}
163152

164153
/// helper trait for converting mio stream to tokio stream.
165154
pub trait FromStream: Sized {
166155
fn from_mio(sock: MioStream) -> io::Result<Self>;
167156
}
168157

169-
// FIXME: This is a workaround and we need an efficient way to convert between mio and tokio stream
170-
#[cfg(unix)]
171-
impl FromStream for TcpStream {
172-
fn from_mio(sock: MioStream) -> io::Result<Self> {
173-
match sock {
174-
MioStream::Tcp(mio) => {
175-
let raw = IntoRawFd::into_raw_fd(mio);
176-
// SAFETY: This is a in place conversion from mio stream to tokio stream.
177-
TcpStream::from_std(unsafe { FromRawFd::from_raw_fd(raw) })
178-
}
179-
MioStream::Uds(_) => {
180-
panic!("Should not happen, bug in server impl");
158+
#[cfg(windows)]
159+
mod win_impl {
160+
use super::*;
161+
162+
use std::os::windows::io::{FromRawSocket, IntoRawSocket};
163+
164+
// FIXME: This is a workaround and we need an efficient way to convert between mio and tokio stream
165+
impl FromStream for TcpStream {
166+
fn from_mio(sock: MioStream) -> io::Result<Self> {
167+
match sock {
168+
MioStream::Tcp(mio) => {
169+
let raw = IntoRawSocket::into_raw_socket(mio);
170+
// SAFETY: This is a in place conversion from mio stream to tokio stream.
171+
TcpStream::from_std(unsafe { FromRawSocket::from_raw_socket(raw) })
172+
}
181173
}
182174
}
183175
}
184176
}
185177

186-
// FIXME: This is a workaround and we need an efficient way to convert between mio and tokio stream
187-
#[cfg(windows)]
188-
impl FromStream for TcpStream {
189-
fn from_mio(sock: MioStream) -> io::Result<Self> {
190-
match sock {
191-
MioStream::Tcp(mio) => {
192-
let raw = IntoRawSocket::into_raw_socket(mio);
193-
// SAFETY: This is a in place conversion from mio stream to tokio stream.
194-
TcpStream::from_std(unsafe { FromRawSocket::from_raw_socket(raw) })
178+
#[cfg(unix)]
179+
mod unix_impl {
180+
use super::*;
181+
182+
use std::os::unix::io::{FromRawFd, IntoRawFd};
183+
184+
use actix_rt::net::UnixStream;
185+
186+
// FIXME: This is a workaround and we need an efficient way to convert between mio and tokio stream
187+
impl FromStream for TcpStream {
188+
fn from_mio(sock: MioStream) -> io::Result<Self> {
189+
match sock {
190+
MioStream::Tcp(mio) => {
191+
let raw = IntoRawFd::into_raw_fd(mio);
192+
// SAFETY: This is a in place conversion from mio stream to tokio stream.
193+
TcpStream::from_std(unsafe { FromRawFd::from_raw_fd(raw) })
194+
}
195+
MioStream::Uds(_) => {
196+
panic!("Should not happen, bug in server impl");
197+
}
195198
}
196199
}
197200
}
198-
}
199201

200-
// FIXME: This is a workaround and we need an efficient way to convert between mio and tokio stream
201-
#[cfg(unix)]
202-
impl FromStream for UnixStream {
203-
fn from_mio(sock: MioStream) -> io::Result<Self> {
204-
match sock {
205-
MioStream::Tcp(_) => panic!("Should not happen, bug in server impl"),
206-
MioStream::Uds(mio) => {
207-
let raw = IntoRawFd::into_raw_fd(mio);
208-
// SAFETY: This is a in place conversion from mio stream to tokio stream.
209-
UnixStream::from_std(unsafe { FromRawFd::from_raw_fd(raw) })
202+
// FIXME: This is a workaround and we need an efficient way to convert between mio and tokio stream
203+
impl FromStream for UnixStream {
204+
fn from_mio(sock: MioStream) -> io::Result<Self> {
205+
match sock {
206+
MioStream::Tcp(_) => panic!("Should not happen, bug in server impl"),
207+
MioStream::Uds(mio) => {
208+
let raw = IntoRawFd::into_raw_fd(mio);
209+
// SAFETY: This is a in place conversion from mio stream to tokio stream.
210+
UnixStream::from_std(unsafe { FromRawFd::from_raw_fd(raw) })
211+
}
210212
}
211213
}
212214
}

0 commit comments

Comments
 (0)