Windows: Auto close HANDLE (#9429)

`HANDLE` is wrapped in a RAII struct.

Release Notes:

- N/A

Co-authored-by: Mikayla Maki <mikayla@zed.dev>
This commit is contained in:
白山風露 2024-03-20 00:21:01 +09:00 committed by GitHub
parent 868616d62e
commit 250528d28f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 20 deletions

View file

@ -1,4 +1,5 @@
use windows::Win32::{Foundation::*, UI::WindowsAndMessaging::*};
use util::ResultExt;
use windows::Win32::{Foundation::*, System::Threading::*, UI::WindowsAndMessaging::*};
pub(crate) trait HiLoWord {
fn hiword(&self) -> u16;
@ -69,6 +70,33 @@ pub(crate) unsafe fn set_window_long(
}
}
pub(crate) struct OwnedHandle(HANDLE);
impl OwnedHandle {
pub(crate) fn new(handle: HANDLE) -> Self {
Self(handle)
}
#[inline(always)]
pub(crate) fn to_raw(&self) -> HANDLE {
self.0
}
}
impl Drop for OwnedHandle {
fn drop(&mut self) {
if !self.0.is_invalid() {
unsafe { CloseHandle(self.0) }.log_err();
}
}
}
pub(crate) fn create_event() -> windows::core::Result<OwnedHandle> {
Ok(OwnedHandle::new(unsafe {
CreateEventW(None, false, false, None)?
}))
}
pub(crate) fn windows_credentials_target_name(url: &str) -> String {
format!("zed:url={}", url)
}