windows-future 0.100.0

Windows async types
Documentation
mod imp {
    use super::super::*;

    pub struct Waiter(HANDLE);
    pub struct WaiterSignaler(HANDLE);
    unsafe impl Send for WaiterSignaler {}

    impl Waiter {
        pub fn new() -> Result<(Self, WaiterSignaler)> {
            unsafe {
                let handle = CreateEventW(core::ptr::null(), 1, 0, core::ptr::null());
                if handle.is_null() {
                    Err(Error::from_thread())
                } else {
                    Ok((Self(handle), WaiterSignaler(handle)))
                }
            }
        }

        // Waits for the `WaiterSignaler` to signal and then closes the handle.
        pub fn wait(self) {
            unsafe {
                WaitForSingleObject(self.0, 0xFFFFFFFF);
            }
        }
    }

    impl WaiterSignaler {
        /// # Safety
        ///
        /// The associated `Waiter` must not have been dropped.
        pub unsafe fn signal(&self) {
            // https://github.com/microsoft/windows-rs/pull/374#discussion_r535313344
            unsafe {
                SetEvent(self.0);
            }
        }
    }

    impl Drop for Waiter {
        fn drop(&mut self) {
            unsafe {
                CloseHandle(self.0);
            }
        }
    }
}

pub use imp::*;