use std::io;
use std::os::windows::io::{AsHandle, BorrowedHandle, OwnedHandle};
use std::time::Duration;
use win_sync_sys::{WaitOutcome, wait_for_single_object};
use durable_ioring::Waitable;
#[cfg(test)]
mod tests;
#[derive(Debug)]
pub struct CompletionEvent(OwnedHandle);
impl CompletionEvent {
#[must_use]
pub(crate) fn new(handle: OwnedHandle) -> Self {
Self(handle)
}
#[must_use]
pub fn into_handle(self) -> OwnedHandle {
self.0
}
}
impl AsHandle for CompletionEvent {
fn as_handle(&self) -> BorrowedHandle<'_> {
self.0.as_handle()
}
}
impl Waitable for CompletionEvent {
fn wait(&mut self, timeout: Option<Duration>) -> io::Result<bool> {
match wait_for_single_object(&self.0, timeout)? {
WaitOutcome::Signalled => Ok(true),
WaitOutcome::TimedOut => Ok(false),
other => Err(io::Error::other(format!(
"the notification's wait ended as {other:?}"
))),
}
}
}