use std::io;
use std::ops::BitOr;
use std::os::windows::io::{AsHandle, AsRawHandle};
use windows_sys::Win32::Foundation::{
FALSE, GetHandleInformation, HANDLE_FLAG_INHERIT, HANDLE_FLAG_PROTECT_FROM_CLOSE,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct HandleFlags(u32);
impl HandleFlags {
pub const NONE: Self = Self(0);
pub const INHERIT: Self = Self(HANDLE_FLAG_INHERIT);
pub const PROTECT_FROM_CLOSE: Self = Self(HANDLE_FLAG_PROTECT_FROM_CLOSE);
#[must_use]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
}
impl BitOr for HandleFlags {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
pub fn get_handle_information(handle: impl AsHandle) -> io::Result<HandleFlags> {
let mut flags = 0u32;
let ok = unsafe { GetHandleInformation(handle.as_handle().as_raw_handle(), &mut flags) };
if ok == FALSE {
return Err(io::Error::last_os_error());
}
Ok(HandleFlags(flags))
}
#[cfg(test)]
mod tests;