Debian Codegraph @

third_party/rust/comedy/src/handle.rs

raw
1 // Licensed under the Apache License, Version 2.0
2 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
4 // All files in the project carrying such notice may not be copied, modified, or distributed
5 // except according to those terms.
6
7 //! Wrapping and automatically closing handles.
8
9 use winapi::shared::minwindef::DWORD;
10 use winapi::shared::ntdef::NULL;
11 use winapi::um::errhandlingapi::GetLastError;
12 use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
13 use winapi::um::winnt::HANDLE;
14
15 /// Check and automatically close a Windows `HANDLE`.
16 #[repr(transparent)]
17 #[derive(Debug)]
18 pub struct Handle(HANDLE);
19
20 impl Handle {
21 /// Take ownership of a `HANDLE`, which will be closed with `CloseHandle` upon drop.
22 /// Returns an error in case of `INVALID_HANDLE_VALUE` or `NULL`.
23 ///
24 /// # Safety
25 ///
26 /// `h` should be the only copy of the handle. `GetLastError()` is called to
27 /// return an error, so the last Windows API called on this thread should have been
28 /// what produced the invalid handle.
29 pub unsafe fn new(h: HANDLE) -> Result<Handle, DWORD> {
30 if h == NULL || h == INVALID_HANDLE_VALUE {
31 Err(GetLastError())
32 } else {
33 Ok(Handle(h))
34 }
35 }
36
37 /// Obtains the raw `HANDLE` without transferring ownership.
38 ///
39 /// Do __not__ close this handle because it is still owned by the `Handle`.
40 ///
41 /// Do __not__ use this handle beyond the lifetime of the `Handle`.
42 pub fn as_raw(&self) -> HANDLE {
43 self.0
44 }
45 }
46
47 impl Drop for Handle {
48 fn drop(&mut self) {
49 unsafe {
50 CloseHandle(self.0);
51 }
52 }
53 }
54
55 /// Call a function that returns a `HANDLE` (`NULL` or `INVALID_HANDLE_VALUE` on failure), wrap result.
56 ///
57 /// The handle is wrapped in a [`Handle`](handle/struct.Handle.html) which will automatically call
58 /// `CloseHandle()` on it. If the function fails, the error is retrieved via `GetLastError()` and
59 /// augmented with the name of the function and the file and line number of the macro usage.
60 ///
61 /// # Example
62 ///
63 /// ```no_run
64 /// # extern crate winapi;
65 /// # use std::ptr;
66 /// # use winapi::um::fileapi::FindFirstFileW;
67 /// # use winapi::um::minwinbase::WIN32_FIND_DATAW;
68 /// # use comedy::handle::Handle;
69 /// # use comedy::{call_handle_getter, Win32Error};
70 /// #
71 /// unsafe fn find_first(name: &[u16], data: &mut WIN32_FIND_DATAW) -> Result<Handle, Win32Error> {
72 /// call_handle_getter!(FindFirstFileW(name.as_ptr(), data))
73 /// }
74 /// ```
75 #[macro_export]
76 macro_rules! call_handle_getter {
77 ($f:ident ( $($arg:expr),* )) => {
78 {
79 use $crate::error::{ErrorCode, FileLine, ResultExt, Win32Error};
80 $crate::handle::Handle::new($f($($arg),*))
81 .map_err(Win32Error::new)
82 .function(stringify!($f))
83 .file_line(file!(), line!())
84 }
85 };
86
87 // support for trailing comma in argument list
88 ($f:ident ( $($arg:expr),+ , )) => {
89 $crate::call_valid_handle_getter!($f($($arg),*))
90 };
91 }