| 1 |
use std::os::windows::io::RawHandle; |
| 2 |
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE}; |
| 3 |
|
| 4 |
/// Wrapper around a Windows HANDLE so that we close it upon drop in all scenarios |
| 5 |
#[derive(Debug)] |
| 6 |
pub struct Handle(HANDLE); |
| 7 |
|
| 8 |
impl Handle { |
| 9 |
#[inline] |
| 10 |
pub fn new(handle: HANDLE) -> Self { |
| 11 |
Self(handle) |
| 12 |
} |
| 13 |
|
| 14 |
pub fn raw(&self) -> HANDLE { |
| 15 |
self.0 |
| 16 |
} |
| 17 |
|
| 18 |
pub fn into_raw(self) -> RawHandle { |
| 19 |
let ret = self.0; |
| 20 |
// This is super important so that drop is not called! |
| 21 |
std::mem::forget(self); |
| 22 |
ret as RawHandle |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl Drop for Handle { |
| 27 |
fn drop(&mut self) { |
| 28 |
unsafe { CloseHandle(self.0) }; |
| 29 |
} |
| 30 |
} |