Skip to content

Commit 57e4c2a

Browse files
FaultyRAMThomasdezeeuw
authored andcommitted
refactor: remove dependency on lazy_static
This was only used in `src/sys/windows/afd.rs` for a set of objects that could instead be defined using constant expressions.
1 parent a07d38b commit 57e4c2a

File tree

2 files changed

+36
-39
lines changed

2 files changed

+36
-39
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ libc = "0.2.69"
4141
miow = "0.3.3"
4242
winapi = { version = "0.3", features = ["winsock2", "mswsock"] }
4343
ntapi = "0.3"
44-
lazy_static = "1.4.0"
4544

4645
[dev-dependencies]
4746
env_logger = { version = "0.6.2", default-features = false }

src/sys/windows/afd.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,19 @@
1-
use lazy_static::lazy_static;
21
use ntapi::ntioapi::{IO_STATUS_BLOCK_u, IO_STATUS_BLOCK};
32
use ntapi::ntioapi::{NtCancelIoFileEx, NtDeviceIoControlFile};
43
use ntapi::ntrtl::RtlNtStatusToDosError;
5-
use std::ffi::OsStr;
64
use std::fmt;
75
use std::fs::File;
86
use std::io;
97
use std::mem::size_of;
10-
use std::os::windows::ffi::OsStrExt;
118
use std::os::windows::io::AsRawHandle;
129
use std::ptr::null_mut;
1310
use winapi::shared::ntdef::{
14-
HANDLE, LARGE_INTEGER, NTSTATUS, OBJECT_ATTRIBUTES, PVOID, ULONG, UNICODE_STRING,
11+
HANDLE, LARGE_INTEGER, NTSTATUS, PVOID, ULONG,
1512
};
1613
use winapi::shared::ntstatus::{STATUS_NOT_FOUND, STATUS_PENDING, STATUS_SUCCESS};
1714

1815
const IOCTL_AFD_POLL: ULONG = 0x00012024;
1916

20-
lazy_static! {
21-
static ref AFD_HELPER_NAME: Vec<u16> = {
22-
OsStr::new("\\Device\\Afd\\Mio")
23-
.encode_wide()
24-
.collect::<Vec<_>>()
25-
};
26-
}
27-
28-
struct UnicodeString(UNICODE_STRING);
29-
unsafe impl Send for UnicodeString {}
30-
unsafe impl Sync for UnicodeString {}
31-
32-
struct ObjectAttributes(OBJECT_ATTRIBUTES);
33-
unsafe impl Send for ObjectAttributes {}
34-
unsafe impl Sync for ObjectAttributes {}
35-
36-
lazy_static! {
37-
static ref AFD_OBJ_NAME: UnicodeString = UnicodeString(UNICODE_STRING {
38-
// Lengths are calced in bytes
39-
Length: (AFD_HELPER_NAME.len() * 2) as u16,
40-
MaximumLength: (AFD_HELPER_NAME.len() * 2) as u16,
41-
Buffer: AFD_HELPER_NAME.as_ptr() as *mut _,
42-
});
43-
static ref AFD_HELPER_ATTRIBUTES: ObjectAttributes = ObjectAttributes(OBJECT_ATTRIBUTES {
44-
Length: size_of::<OBJECT_ATTRIBUTES>() as ULONG,
45-
RootDirectory: null_mut() as HANDLE,
46-
ObjectName: &AFD_OBJ_NAME.0 as *const _ as *mut _,
47-
Attributes: 0 as ULONG,
48-
SecurityDescriptor: null_mut() as PVOID,
49-
SecurityQualityOfService: null_mut() as PVOID,
50-
});
51-
}
52-
5317
/// Winsock2 AFD driver instance.
5418
///
5519
/// All operations are unsafe due to IO_STATUS_BLOCK parameter are being used by Afd driver during STATUS_PENDING before I/O Completion Port returns its result.
@@ -156,11 +120,45 @@ cfg_net! {
156120
use std::mem::zeroed;
157121
use std::os::windows::io::{FromRawHandle, RawHandle};
158122
use std::sync::atomic::{AtomicUsize, Ordering};
123+
use winapi::shared::ntdef::{OBJECT_ATTRIBUTES, UNICODE_STRING, USHORT, WCHAR};
159124
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
160125
use winapi::um::winbase::{SetFileCompletionNotificationModes, FILE_SKIP_SET_EVENT_ON_HANDLE};
161126
use winapi::um::winnt::SYNCHRONIZE;
162127
use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE};
163128

129+
const AFD_HELPER_ATTRIBUTES: OBJECT_ATTRIBUTES = OBJECT_ATTRIBUTES {
130+
Length: size_of::<OBJECT_ATTRIBUTES>() as ULONG,
131+
RootDirectory: null_mut(),
132+
ObjectName: &AFD_OBJ_NAME as *const _ as *mut _,
133+
Attributes: 0,
134+
SecurityDescriptor: null_mut(),
135+
SecurityQualityOfService: null_mut(),
136+
};
137+
138+
const AFD_OBJ_NAME: UNICODE_STRING = UNICODE_STRING {
139+
Length: (AFD_HELPER_NAME.len() * size_of::<WCHAR>()) as USHORT,
140+
MaximumLength: (AFD_HELPER_NAME.len() * size_of::<WCHAR>()) as USHORT,
141+
Buffer: AFD_HELPER_NAME.as_ptr() as *mut _,
142+
};
143+
144+
const AFD_HELPER_NAME: &[WCHAR] = &[
145+
'\\' as _,
146+
'D' as _,
147+
'e' as _,
148+
'v' as _,
149+
'i' as _,
150+
'c' as _,
151+
'e' as _,
152+
'\\' as _,
153+
'A' as _,
154+
'f' as _,
155+
'd' as _,
156+
'\\' as _,
157+
'M' as _,
158+
'i' as _,
159+
'o' as _
160+
];
161+
164162
static NEXT_TOKEN: AtomicUsize = AtomicUsize::new(0);
165163

166164
impl AfdPollInfo {
@@ -182,7 +180,7 @@ cfg_net! {
182180
let status = NtCreateFile(
183181
&mut afd_helper_handle as *mut _,
184182
SYNCHRONIZE,
185-
&AFD_HELPER_ATTRIBUTES.0 as *const _ as *mut _,
183+
&AFD_HELPER_ATTRIBUTES as *const _ as *mut _,
186184
&mut iosb,
187185
null_mut(),
188186
0 as ULONG,

0 commit comments

Comments
 (0)