1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::fmt;
use std::io;
use std::mem;
use std::ptr;
use winapi::shared::ntdef::{HANDLE, NULL};
use winapi::um::minwinbase::*;
use winapi::um::synchapi::*;
pub struct Overlapped(OVERLAPPED);
impl fmt::Debug for Overlapped {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "OVERLAPPED")
}
}
unsafe impl Send for Overlapped {}
unsafe impl Sync for Overlapped {}
impl Overlapped {
pub fn zero() -> Overlapped {
Overlapped(unsafe { mem::zeroed() })
}
pub fn initialize_with_autoreset_event() -> io::Result<Overlapped> {
let event = unsafe { CreateEventW(ptr::null_mut(), 0i32, 0i32, ptr::null()) };
if event == NULL {
return Err(io::Error::last_os_error());
}
let mut overlapped = Self::zero();
overlapped.set_event(event);
Ok(overlapped)
}
pub unsafe fn from_raw<'a>(ptr: *mut OVERLAPPED) -> &'a mut Overlapped {
&mut *(ptr as *mut Overlapped)
}
pub fn raw(&self) -> *mut OVERLAPPED {
&self.0 as *const _ as *mut _
}
pub fn set_offset(&mut self, offset: u64) {
let s = unsafe { self.0.u.s_mut() };
s.Offset = offset as u32;
s.OffsetHigh = (offset >> 32) as u32;
}
pub fn offset(&self) -> u64 {
let s = unsafe { self.0.u.s() };
(s.Offset as u64) | ((s.OffsetHigh as u64) << 32)
}
pub fn set_event(&mut self, event: HANDLE) {
self.0.hEvent = event;
}
pub fn event(&self) -> HANDLE {
self.0.hEvent
}
}