-
Notifications
You must be signed in to change notification settings - Fork 531
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for freeing handles automatically (#3013)
- Loading branch information
Showing
32 changed files
with
520 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/// Custom code to free a handle. | ||
/// | ||
/// This is similar to the `Drop` trait, and may be used to implement `Drop`, but allows handles | ||
/// to be dropped depending on context. | ||
pub trait Free { | ||
/// Calls the handle's free function. | ||
/// | ||
/// # 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. | ||
#[repr(transparent)] | ||
#[derive(Clone, PartialEq, Eq, Default, Debug)] | ||
pub struct Owned<T: Free>(T); | ||
|
||
impl<T: Free> Owned<T> { | ||
/// Takes ownership of the handle. | ||
/// | ||
/// # 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) { | ||
unsafe { self.0.free() }; | ||
} | ||
} | ||
|
||
impl<T: Free> std::ops::Deref for Owned<T> { | ||
type Target = T; | ||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T: Free> std::ops::DerefMut for Owned<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.