Skip to content

Commit f41a47d

Browse files
committed
optimize #[cfg]
1 parent 0f8b724 commit f41a47d

21 files changed

+234
-236
lines changed

examples/uds.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! A example to show how to use UnixStream.
22
3-
use local_sync::oneshot::channel;
4-
use monoio::{
5-
io::{AsyncReadRent, AsyncWriteRentExt},
6-
net::{UnixListener, UnixStream},
3+
#[cfg(unix)]
4+
use {
5+
local_sync::oneshot::channel,
6+
monoio::{
7+
io::{AsyncReadRent, AsyncWriteRentExt},
8+
net::{UnixListener, UnixStream},
9+
},
710
};
811

912
const ADDRESS: &str = "/tmp/monoio-unix-test.sock";

monoio/src/builder.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::{io, marker::PhantomData};
22

33
#[cfg(all(target_os = "linux", feature = "iouring"))]
44
use crate::driver::IoUringDriver;
5-
#[cfg(all(unix, feature = "legacy"))]
5+
#[cfg(feature = "legacy")]
66
use crate::driver::LegacyDriver;
7-
#[cfg(all(unix, any(feature = "legacy", feature = "iouring")))]
7+
#[cfg(any(feature = "legacy", feature = "iouring"))]
88
use crate::utils::thread_id::gen_id;
99
use crate::{
1010
driver::Driver,
@@ -80,14 +80,14 @@ macro_rules! direct_build {
8080
direct_build!(IoUringDriver);
8181
#[cfg(all(target_os = "linux", feature = "iouring"))]
8282
direct_build!(TimeDriver<IoUringDriver>);
83-
#[cfg(all(unix, feature = "legacy"))]
83+
#[cfg(feature = "legacy")]
8484
direct_build!(LegacyDriver);
85-
#[cfg(all(unix, feature = "legacy"))]
85+
#[cfg(feature = "legacy")]
8686
direct_build!(TimeDriver<LegacyDriver>);
8787

8888
// ===== builder impl =====
8989

90-
#[cfg(all(unix, feature = "legacy"))]
90+
#[cfg(feature = "legacy")]
9191
impl Buildable for LegacyDriver {
9292
fn build(this: RuntimeBuilder<Self>) -> io::Result<Runtime<LegacyDriver>> {
9393
let thread_id = gen_id();
@@ -192,7 +192,7 @@ impl RuntimeBuilder<FusionDriver> {
192192
}
193193

194194
/// Build the runtime.
195-
#[cfg(all(unix, not(all(target_os = "linux", feature = "iouring"))))]
195+
#[cfg(not(all(target_os = "linux", feature = "iouring")))]
196196
pub fn build(self) -> io::Result<crate::FusionRuntime<LegacyDriver>> {
197197
let builder = RuntimeBuilder::<LegacyDriver> {
198198
entries: self.entries,
@@ -248,7 +248,7 @@ impl RuntimeBuilder<TimeDriver<FusionDriver>> {
248248
}
249249

250250
/// Build the runtime.
251-
#[cfg(all(unix, not(all(target_os = "linux", feature = "iouring"))))]
251+
#[cfg(not(all(target_os = "linux", feature = "iouring")))]
252252
pub fn build(self) -> io::Result<crate::FusionRuntime<TimeDriver<LegacyDriver>>> {
253253
let builder = RuntimeBuilder::<TimeDriver<LegacyDriver>> {
254254
entries: self.entries,
@@ -280,7 +280,7 @@ mod time_wrap {
280280

281281
#[cfg(all(target_os = "linux", feature = "iouring"))]
282282
impl time_wrap::TimeWrapable for IoUringDriver {}
283-
#[cfg(all(unix, feature = "legacy"))]
283+
#[cfg(feature = "legacy")]
284284
impl time_wrap::TimeWrapable for LegacyDriver {}
285285
#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
286286
impl time_wrap::TimeWrapable for FusionDriver {}

monoio/src/driver/mod.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::{
2626
};
2727

2828
#[allow(unreachable_pub)]
29-
#[cfg(all(feature = "legacy", unix))]
29+
#[cfg(feature = "legacy")]
3030
pub use self::legacy::LegacyDriver;
3131
#[cfg(feature = "legacy")]
3232
use self::legacy::LegacyInner;
@@ -108,10 +108,7 @@ impl Inner {
108108
not(all(target_os = "linux", feature = "iouring"))
109109
))]
110110
_ => {
111-
#[cfg(unix)]
112111
util::feature_panic();
113-
#[cfg(windows)]
114-
unimplemented!();
115112
}
116113
}
117114
}
@@ -124,8 +121,6 @@ impl Inner {
124121
cx: &mut Context<'_>,
125122
) -> Poll<CompletionMeta> {
126123
match self {
127-
#[cfg(windows)]
128-
_ => unimplemented!(),
129124
#[cfg(all(target_os = "linux", feature = "iouring"))]
130125
Inner::Uring(this) => UringInner::poll_op(this, index, cx),
131126
#[cfg(feature = "legacy")]
@@ -147,8 +142,6 @@ impl Inner {
147142
cx: &mut Context<'_>,
148143
) -> Poll<CompletionMeta> {
149144
match self {
150-
#[cfg(windows)]
151-
_ => unimplemented!(),
152145
#[cfg(all(target_os = "linux", feature = "iouring"))]
153146
Inner::Uring(this) => UringInner::poll_legacy_op(this, data, cx),
154147
#[cfg(feature = "legacy")]
@@ -166,8 +159,6 @@ impl Inner {
166159
#[allow(unused)]
167160
fn drop_op<T: 'static>(&self, index: usize, data: &mut Option<T>) {
168161
match self {
169-
#[cfg(windows)]
170-
_ => unimplemented!(),
171162
#[cfg(all(target_os = "linux", feature = "iouring"))]
172163
Inner::Uring(this) => UringInner::drop_op(this, index, data),
173164
#[cfg(feature = "legacy")]
@@ -185,8 +176,6 @@ impl Inner {
185176
#[allow(unused)]
186177
pub(super) unsafe fn cancel_op(&self, op_canceller: &op::OpCanceller) {
187178
match self {
188-
#[cfg(windows)]
189-
_ => unimplemented!(),
190179
#[cfg(all(target_os = "linux", feature = "iouring"))]
191180
Inner::Uring(this) => UringInner::cancel_op(this, op_canceller.index),
192181
#[cfg(feature = "legacy")]

monoio/src/driver/op/connect.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,16 @@ impl OpAble for Connect {
116116
}
117117
}
118118

119+
#[cfg(unix)]
119120
pub(crate) struct ConnectUnix {
120121
/// Holds a strong ref to the FD, preventing the file from being closed
121122
/// while the operation is in-flight.
122123
pub(crate) fd: SharedFd,
123-
#[cfg(unix)]
124124
socket_addr: Box<(libc::sockaddr_un, libc::socklen_t)>,
125125
}
126126

127+
#[cfg(unix)]
127128
impl Op<ConnectUnix> {
128-
#[cfg(unix)]
129-
130129
/// Submit a request to connect.
131130
pub(crate) fn connect_unix(
132131
socket: SharedFd,
@@ -140,6 +139,7 @@ impl Op<ConnectUnix> {
140139
}
141140
}
142141

142+
#[cfg(unix)]
143143
impl OpAble for ConnectUnix {
144144
#[cfg(all(target_os = "linux", feature = "iouring"))]
145145
fn uring_op(&mut self) -> io_uring::squeue::Entry {
@@ -157,7 +157,7 @@ impl OpAble for ConnectUnix {
157157
None
158158
}
159159

160-
#[cfg(all(any(feature = "legacy", feature = "poll-io"), unix))]
160+
#[cfg(any(feature = "legacy", feature = "poll-io"))]
161161
fn legacy_call(&mut self) -> io::Result<u32> {
162162
match crate::syscall_u32!(connect(
163163
self.fd.raw_fd(),
@@ -168,11 +168,6 @@ impl OpAble for ConnectUnix {
168168
_ => Ok(self.fd.raw_fd() as u32),
169169
}
170170
}
171-
172-
#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
173-
fn legacy_call(&mut self) -> io::Result<u32> {
174-
unimplemented!()
175-
}
176171
}
177172

178173
/// A type with the same memory layout as `libc::sockaddr`. Used in converting Rust level

monoio/src/driver/op/read.rs

+27-16
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ use std::io;
22

33
#[cfg(all(target_os = "linux", feature = "iouring"))]
44
use io_uring::{opcode, types};
5+
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
6+
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};
57
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
68
use {
7-
crate::syscall,
89
std::ffi::c_void,
9-
std::os::windows::io::AsRawSocket,
10-
windows_sys::Win32::Networking::WinSock::{recv, WSAGetLastError, WSARecv, SOCKET_ERROR},
10+
windows_sys::Win32::{
11+
Foundation::TRUE,
12+
Networking::WinSock::{WSAGetLastError, WSARecv, SOCKET_ERROR},
13+
Storage::FileSystem::ReadFile,
14+
System::IO::OVERLAPPED,
15+
},
1116
};
12-
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
13-
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};
1417

1518
use super::{super::shared_fd::SharedFd, Op, OpAble};
1619
#[cfg(any(feature = "legacy", feature = "poll-io"))]
@@ -102,20 +105,28 @@ impl<T: IoBufMut> OpAble for Read<T> {
102105

103106
#[cfg(all(any(feature = "legacy", feature = "poll-io"), windows))]
104107
fn legacy_call(&mut self) -> io::Result<u32> {
105-
let fd = self.fd.as_raw_socket();
108+
let fd = self.fd.raw_handle();
106109
let seek_offset = libc::off_t::try_from(self.offset)
107110
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
108-
syscall!(
109-
recv(
111+
let mut bytes_read = 0;
112+
let ret = unsafe {
113+
let mut overlapped: OVERLAPPED = std::mem::zeroed();
114+
overlapped.Anonymous.Anonymous.Offset = (seek_offset as i64 & 0xFFFFFFFF) as u32;
115+
overlapped.Anonymous.Anonymous.OffsetHigh =
116+
((seek_offset as i64 >> 32) & 0xFFFFFFFF) as u32;
117+
ReadFile(
110118
fd as _,
111-
(self.buf.write_ptr().cast::<c_void>() as usize + seek_offset as usize)
112-
as *mut c_void as *mut _,
113-
self.buf.bytes_total() as i32 - seek_offset,
114-
0
115-
),
116-
PartialOrd::ge,
117-
0
118-
)
119+
self.buf.write_ptr().cast::<c_void>(),
120+
self.buf.bytes_total() as u32,
121+
&mut bytes_read,
122+
&mut overlapped,
123+
)
124+
};
125+
if TRUE == ret {
126+
Ok(bytes_read)
127+
} else {
128+
Err(io::Error::last_os_error())
129+
}
119130
}
120131
}
121132

monoio/src/driver/op/write.rs

+26-15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ use std::io;
22

33
#[cfg(all(target_os = "linux", feature = "iouring"))]
44
use io_uring::{opcode, types};
5+
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
6+
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};
57
#[cfg(all(windows, any(feature = "legacy", feature = "poll-io")))]
68
use {
7-
crate::syscall,
8-
std::ffi::c_void,
99
std::os::windows::io::AsRawSocket,
10-
windows_sys::Win32::Networking::WinSock::{send, WSAGetLastError, WSASend, SOCKET_ERROR},
10+
windows_sys::Win32::{
11+
Foundation::TRUE,
12+
Networking::WinSock::{WSAGetLastError, WSASend, SOCKET_ERROR},
13+
Storage::FileSystem::WriteFile,
14+
System::IO::OVERLAPPED,
15+
},
1116
};
12-
#[cfg(all(unix, any(feature = "legacy", feature = "poll-io")))]
13-
use {crate::syscall_u32, std::os::unix::prelude::AsRawFd};
1417

1518
use super::{super::shared_fd::SharedFd, Op, OpAble};
1619
#[cfg(any(feature = "legacy", feature = "poll-io"))]
@@ -92,17 +95,25 @@ impl<T: IoBuf> OpAble for Write<T> {
9295
let fd = self.fd.as_raw_socket();
9396
let seek_offset = libc::off_t::try_from(self.offset)
9497
.map_err(|_| io::Error::new(io::ErrorKind::Other, "offset too big"))?;
95-
syscall!(
96-
send(
98+
let mut bytes_write = 0;
99+
let ret = unsafe {
100+
let mut overlapped: OVERLAPPED = std::mem::zeroed();
101+
overlapped.Anonymous.Anonymous.Offset = (seek_offset as i64 & 0xFFFFFFFF) as u32;
102+
overlapped.Anonymous.Anonymous.OffsetHigh =
103+
((seek_offset as i64 >> 32) & 0xFFFFFFFF) as u32;
104+
WriteFile(
97105
fd as _,
98-
(self.buf.read_ptr().cast::<c_void>() as usize + seek_offset as usize)
99-
as *mut c_void as *mut _,
100-
self.buf.bytes_init() as i32 - seek_offset,
101-
0
102-
),
103-
PartialOrd::ge,
104-
0
105-
)
106+
self.buf.read_ptr(),
107+
self.buf.bytes_init() as u32,
108+
&mut bytes_write,
109+
&mut overlapped,
110+
)
111+
};
112+
if TRUE == ret {
113+
Ok(bytes_write)
114+
} else {
115+
Err(io::Error::last_os_error())
116+
}
106117
}
107118
}
108119

monoio/src/driver/poll.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{io, os::fd::AsRawFd, task::Context, time::Duration};
1+
use std::{io, task::Context, time::Duration};
22

33
use super::{ready::Direction, scheduled_io::ScheduledIo};
44
use crate::{driver::op::CompletionMeta, utils::slab::Slab};
@@ -100,7 +100,8 @@ impl Poll {
100100
}
101101
}
102102

103-
impl AsRawFd for Poll {
103+
#[cfg(unix)]
104+
impl std::os::fd::AsRawFd for Poll {
104105
#[inline]
105106
fn as_raw_fd(&self) -> std::os::fd::RawFd {
106107
self.poll.as_raw_fd()

monoio/src/driver/shared_fd.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl SharedFd {
242242
}
243243

244244
#[cfg(windows)]
245-
pub(crate) fn new(fd: RawSocket) -> io::Result<SharedFd> {
245+
pub(crate) fn new<const FORCE_LEGACY: bool>(fd: RawSocket) -> io::Result<SharedFd> {
246246
const RW_INTERESTS: mio::Interest = mio::Interest::READABLE.add(mio::Interest::WRITABLE);
247247

248248
let mut fd = RawFd::new(fd);
@@ -272,7 +272,7 @@ impl SharedFd {
272272
let state = CURRENT.with(|inner| match inner {
273273
#[cfg(all(target_os = "linux", feature = "iouring"))]
274274
super::Inner::Uring(_) => State::Uring(UringState::Init),
275-
#[cfg(all(unix, feature = "legacy"))]
275+
#[cfg(feature = "legacy")]
276276
super::Inner::Legacy(_) => State::Legacy(None),
277277
#[cfg(all(
278278
not(feature = "legacy"),
@@ -339,10 +339,10 @@ impl SharedFd {
339339
let mut state = unsafe { MaybeUninit::uninit().assume_init() };
340340
std::mem::swap(&mut inner_skip_drop.state, &mut state);
341341

342-
#[cfg(all(unix, feature = "legacy"))]
342+
#[cfg(feature = "legacy")]
343343
let state = unsafe { &*state.get() };
344344

345-
#[cfg(all(unix, feature = "legacy"))]
345+
#[cfg(feature = "legacy")]
346346
#[allow(irrefutable_let_patterns)]
347347
if let State::Legacy(idx) = state {
348348
if CURRENT.is_set() {
@@ -564,7 +564,7 @@ fn drop_legacy(mut fd: RawFd, idx: Option<usize>) {
564564
}
565565
#[cfg(all(unix, feature = "legacy"))]
566566
let _ = unsafe { std::fs::File::from_raw_fd(fd) };
567-
#[cfg(windows)]
567+
#[cfg(all(windows, feature = "legacy"))]
568568
let _ = unsafe { OwnedSocket::from_raw_socket(fd.socket) };
569569
}
570570

@@ -573,7 +573,7 @@ fn drop_uring_legacy(fd: RawFd, idx: Option<usize>) {
573573
if CURRENT.is_set() {
574574
CURRENT.with(|inner| {
575575
match inner {
576-
#[cfg(all(unix, feature = "legacy"))]
576+
#[cfg(feature = "legacy")]
577577
super::Inner::Legacy(_) => {
578578
unreachable!("close uring fd with legacy runtime")
579579
}

monoio/src/driver/util.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ pub(super) fn cstr(p: &Path) -> io::Result<CString> {
88
Ok(CString::new(p.as_os_str().as_bytes())?)
99
}
1010
#[cfg(windows)]
11-
{
12-
unimplemented!()
13-
}
11+
Ok(CString::new(p.as_os_str().as_encoded_bytes())?)
1412
}
1513

1614
// Convert Duration to Timespec

monoio/src/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ pub use builder::{Buildable, RuntimeBuilder};
3939
pub use driver::Driver;
4040
#[cfg(all(target_os = "linux", feature = "iouring"))]
4141
pub use driver::IoUringDriver;
42-
#[cfg(all(unix, feature = "legacy"))]
42+
#[cfg(feature = "legacy")]
4343
pub use driver::LegacyDriver;
4444
#[cfg(feature = "macros")]
4545
pub use monoio_macros::{main, test, test_all};
4646
pub use runtime::{spawn, Runtime};
47-
#[cfg(all(
48-
unix,
49-
any(all(target_os = "linux", feature = "iouring"), feature = "legacy")
50-
))]
47+
#[cfg(any(all(target_os = "linux", feature = "iouring"), feature = "legacy"))]
5148
pub use {builder::FusionDriver, runtime::FusionRuntime};
5249

5350
/// Start a monoio runtime.

0 commit comments

Comments
 (0)