summaryrefslogtreecommitdiffstats
path: root/vendor/fd-lock/src/sys/windows/utils.rs
blob: 43656a0838575c5b5ab44f4729ca0d65953aa89a (plain)
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
use std::io;
use std::mem;

use windows_sys::Win32::Foundation::BOOL;
use windows_sys::Win32::System::IO::OVERLAPPED;

/// A wrapper around `OVERLAPPED` to provide "rustic" accessors and
/// initializers.
pub(crate) struct Overlapped(OVERLAPPED);

impl Overlapped {
    /// Creates a new zeroed out instance of an overlapped I/O tracking state.
    ///
    /// This is suitable for passing to methods which will then later get
    /// notified via an I/O Completion Port.
    pub(crate) fn zero() -> Overlapped {
        Overlapped(unsafe { mem::zeroed() })
    }

    /// Gain access to the raw underlying data
    pub(crate) fn raw(&self) -> *mut OVERLAPPED {
        &self.0 as *const _ as *mut _
    }
}

/// Convert a system call which returns a `BOOL` to an `io::Result`.
pub(crate) fn syscall(status: BOOL) -> std::io::Result<()> {
    if status == 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(())
    }
}