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
}
}