use std::fs::OpenOptions;
use std::io;
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::{AsHandle, BorrowedHandle, OwnedHandle};
use std::path::Path;
const FILE_FLAG_OVERLAPPED: u32 = 0x4000_0000;
#[derive(Debug)]
pub struct UnassociatedEndpoint {
handle: OwnedHandle,
}
impl UnassociatedEndpoint {
pub fn open(
path: impl AsRef<Path>,
read: bool,
write: bool,
extra_flags: u32,
) -> io::Result<Self> {
let file = OpenOptions::new()
.read(read)
.write(write)
.custom_flags(FILE_FLAG_OVERLAPPED | extra_flags)
.open(path)?;
Ok(unsafe { Self::assume_overlapped(OwnedHandle::from(file)) })
}
#[must_use]
pub unsafe fn assume_overlapped(handle: OwnedHandle) -> Self {
Self { handle }
}
#[must_use]
pub fn handle(&self) -> BorrowedHandle<'_> {
self.handle.as_handle()
}
#[must_use]
pub fn into_handle(self) -> OwnedHandle {
self.handle
}
}
#[cfg(test)]
mod tests;