#[cfg(feature = "win32")]
use windows::Win32::Foundation::{CloseHandle, HANDLE};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RawHandle(pub isize);
impl RawHandle {
pub fn is_valid(&self) -> bool {
self.0 != 0 && self.0 != -1
}
#[cfg(feature = "win32")]
pub fn as_windows_handle(&self) -> HANDLE {
HANDLE(self.0)
}
}
#[derive(Debug)]
pub struct Handle {
raw: RawHandle,
}
impl Handle {
pub unsafe fn from_raw(raw: isize) -> Self {
Handle {
raw: RawHandle(raw),
}
}
#[cfg(feature = "win32")]
pub unsafe fn from_windows_handle(handle: HANDLE) -> Self {
Handle {
raw: RawHandle(handle.0),
}
}
pub fn raw(&self) -> RawHandle {
self.raw
}
pub fn is_valid(&self) -> bool {
self.raw.is_valid()
}
#[cfg(feature = "win32")]
pub fn as_windows_handle(&self) -> HANDLE {
self.raw.as_windows_handle()
}
pub fn into_raw(self) -> isize {
let raw = self.raw.0;
std::mem::forget(self); raw
}
}
impl Drop for Handle {
fn drop(&mut self) {
#[cfg(feature = "win32")]
{
if self.is_valid() {
unsafe {
let _ = CloseHandle(self.as_windows_handle());
}
}
}
}
}
#[derive(Debug)]
pub struct ProcessHandle(Handle);
impl ProcessHandle {
pub unsafe fn from_raw(raw: isize) -> Self {
ProcessHandle(Handle::from_raw(raw))
}
#[cfg(feature = "win32")]
pub unsafe fn from_windows_handle(handle: HANDLE) -> Self {
ProcessHandle(Handle::from_windows_handle(handle))
}
pub fn as_handle(&self) -> &Handle {
&self.0
}
pub fn into_raw(self) -> isize {
self.0.into_raw()
}
}
#[derive(Debug)]
pub struct ThreadHandle(Handle);
impl ThreadHandle {
pub unsafe fn from_raw(raw: isize) -> Self {
ThreadHandle(Handle::from_raw(raw))
}
#[cfg(feature = "win32")]
pub unsafe fn from_windows_handle(handle: HANDLE) -> Self {
ThreadHandle(Handle::from_windows_handle(handle))
}
pub fn as_handle(&self) -> &Handle {
&self.0
}
pub fn into_raw(self) -> isize {
self.0.into_raw()
}
}