Skip to content

Commit

Permalink
unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Apr 30, 2024
1 parent e386c51 commit 5986214
Show file tree
Hide file tree
Showing 27 changed files with 123 additions and 229 deletions.
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ pub fn gen_win_handle(writer: &Writer, def: metadata::TypeDef) -> TokenStream {

quote! {
impl windows_core::Free for #ident {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe { #result #name(*self #tail); }
#result #name(*self #tail);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions crates/libs/core/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
/// to be dropped depending on context.
pub trait Free {
/// Calls the handle's free function.
fn free(&mut self);
///
/// # Safety
/// The handle must be owned by the caller and safe to free.
unsafe fn free(&mut self);
}

/// A wrapper to provide ownership for handles to automatically drop via the handle's `Free` trait.
Expand All @@ -14,14 +17,17 @@ pub struct Owned<T: Free>(T);

impl<T: Free> Owned<T> {
/// Takes ownership of the handle.
pub fn new(x: T) -> Self {
///
/// # Safety
/// The handle must be owned by the caller and safe to free.
pub unsafe fn new(x: T) -> Self {
Self(x)
}
}

impl<T: Free> Drop for Owned<T> {
fn drop(&mut self) {
self.0.free();
unsafe { self.0.free() };
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,9 @@ impl ORHKEY {
}
}
impl windows_core::Free for ORHKEY {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = ORCloseKey(*self);
}
_ = ORCloseKey(*self);
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions crates/libs/windows/src/Windows/Win32/Devices/Bluetooth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2466,11 +2466,9 @@ impl HBLUETOOTH_DEVICE_FIND {
}
}
impl windows_core::Free for HBLUETOOTH_DEVICE_FIND {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = BluetoothFindDeviceClose(*self);
}
_ = BluetoothFindDeviceClose(*self);
}
}
}
Expand Down Expand Up @@ -2502,11 +2500,9 @@ impl HBLUETOOTH_RADIO_FIND {
}
}
impl windows_core::Free for HBLUETOOTH_RADIO_FIND {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = BluetoothFindRadioClose(*self);
}
_ = BluetoothFindRadioClose(*self);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8393,11 +8393,9 @@ impl HDEVINFO {
}
}
impl windows_core::Free for HDEVINFO {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = SetupDiDestroyDeviceInfoList(*self);
}
_ = SetupDiDestroyDeviceInfoList(*self);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/libs/windows/src/Windows/Win32/Devices/Display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6246,11 +6246,9 @@ impl HSEMAPHORE {
}
}
impl windows_core::Free for HSEMAPHORE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
EngDeleteSemaphore(*self);
}
EngDeleteSemaphore(*self);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/libs/windows/src/Windows/Win32/Devices/Usb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5969,11 +5969,9 @@ impl WINUSB_INTERFACE_HANDLE {
}
}
impl windows_core::Free for WINUSB_INTERFACE_HANDLE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = WinUsb_Free(*self);
}
_ = WinUsb_Free(*self);
}
}
}
Expand Down
30 changes: 10 additions & 20 deletions crates/libs/windows/src/Windows/Win32/Foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10786,11 +10786,9 @@ impl HANDLE {
}
}
impl windows_core::Free for HANDLE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = CloseHandle(*self);
}
_ = CloseHandle(*self);
}
}
}
Expand Down Expand Up @@ -10844,11 +10842,9 @@ impl HGLOBAL {
}
}
impl windows_core::Free for HGLOBAL {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = GlobalFree(*self);
}
_ = GlobalFree(*self);
}
}
}
Expand Down Expand Up @@ -10880,11 +10876,9 @@ impl HINSTANCE {
}
}
impl windows_core::Free for HINSTANCE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = FreeLibrary(*self);
}
_ = FreeLibrary(*self);
}
}
}
Expand Down Expand Up @@ -10922,11 +10916,9 @@ impl HLOCAL {
}
}
impl windows_core::Free for HLOCAL {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = LocalFree(*self);
}
_ = LocalFree(*self);
}
}
}
Expand Down Expand Up @@ -10980,11 +10972,9 @@ impl HMODULE {
}
}
impl windows_core::Free for HMODULE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = FreeLibrary(*self);
}
_ = FreeLibrary(*self);
}
}
}
Expand Down
48 changes: 16 additions & 32 deletions crates/libs/windows/src/Windows/Win32/Graphics/Gdi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10669,11 +10669,9 @@ impl HBITMAP {
}
}
impl windows_core::Free for HBITMAP {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteObject(*self);
}
_ = DeleteObject(*self);
}
}
}
Expand Down Expand Up @@ -10711,11 +10709,9 @@ impl HBRUSH {
}
}
impl windows_core::Free for HBRUSH {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteObject(*self);
}
_ = DeleteObject(*self);
}
}
}
Expand Down Expand Up @@ -10780,11 +10776,9 @@ impl HENHMETAFILE {
}
}
impl windows_core::Free for HENHMETAFILE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteEnhMetaFile(*self);
}
_ = DeleteEnhMetaFile(*self);
}
}
}
Expand Down Expand Up @@ -10816,11 +10810,9 @@ impl HFONT {
}
}
impl windows_core::Free for HFONT {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteObject(*self);
}
_ = DeleteObject(*self);
}
}
}
Expand Down Expand Up @@ -10885,11 +10877,9 @@ impl HMETAFILE {
}
}
impl windows_core::Free for HMETAFILE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteMetaFile(*self);
}
_ = DeleteMetaFile(*self);
}
}
}
Expand Down Expand Up @@ -10948,11 +10938,9 @@ impl HPALETTE {
}
}
impl windows_core::Free for HPALETTE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteObject(*self);
}
_ = DeleteObject(*self);
}
}
}
Expand Down Expand Up @@ -10990,11 +10978,9 @@ impl HPEN {
}
}
impl windows_core::Free for HPEN {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteObject(*self);
}
_ = DeleteObject(*self);
}
}
}
Expand Down Expand Up @@ -11032,11 +11018,9 @@ impl HRGN {
}
}
impl windows_core::Free for HRGN {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = DeleteObject(*self);
}
_ = DeleteObject(*self);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/libs/windows/src/Windows/Win32/Graphics/OpenGL/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3088,11 +3088,9 @@ impl HGLRC {
}
}
impl windows_core::Free for HGLRC {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = wglDeleteContext(*self);
}
_ = wglDeleteContext(*self);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7806,11 +7806,9 @@ impl LSA_HANDLE {
}
}
impl windows_core::Free for LSA_HANDLE {
fn free(&mut self) {
unsafe fn free(&mut self) {
if !self.is_invalid() {
unsafe {
_ = LsaClose(*self);
}
_ = LsaClose(*self);
}
}
}
Expand Down

0 comments on commit 5986214

Please sign in to comment.