aboutsummaryrefslogtreecommitdiff
path: root/src/sys/windows/handle.rs
blob: 5b9ac0b6245d49dd0a6dc1d71fc44874e3c7504e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::os::windows::io::RawHandle;
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};

/// Wrapper around a Windows HANDLE so that we close it upon drop in all scenarios
#[derive(Debug)]
pub struct Handle(HANDLE);

impl Handle {
    #[inline]
    pub fn new(handle: HANDLE) -> Self {
        Self(handle)
    }

    pub fn raw(&self) -> HANDLE {
        self.0
    }

    pub fn into_raw(self) -> RawHandle {
        let ret = self.0;
        // This is super important so that drop is not called!
        std::mem::forget(self);
        ret as RawHandle
    }
}

impl Drop for Handle {
    fn drop(&mut self) {
        unsafe { CloseHandle(self.0) };
    }
}