-
Notifications
You must be signed in to change notification settings - Fork 497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RAII type for HANDLES? #2222
Comments
No. The Win32 metadata, which defines |
Ok, good to know I wasn't missing something. Has this already been discussed? Is this in scope for v1.0? |
Ideally, the Win32 metadata can get improved support for ownership semantics. @riverar and I have discussed this at some length. He may have some thoughts as well. But I probably have to come up with some way to infer lifetime/ownership. This is a general problem, particularly challenging with DirectX APIs where there are structs with COM interface pointer fields and it's not always clear who owns what. |
Metadata has quite a bit of work ahead of it to make this work, or at least get it into a state where we can start experimentation. For example, we're still tossing around candidates for how these attributes look microsoft/win32metadata#389. And we haven't nailed down a fix for free functions with multiple parameters microsoft/win32metadata#423. Or how to deal with functions that return either freeable and non-freeable resources based on other inputs microsoft/win32metadata#423 (comment). |
It seems that there are several different problems some of affecting legacy APIs while others are affecting more modern ones. Is the idea to fix all at once, or can these be prioritized at all? The D3D APIs I think should be pretty trivial - the COM pointers in D3D12 structs are all weak references. |
By weak reference I assume you don't mean a weak reference? 😉 I was going to apply some sort of borrow semantics to indicate that they're not owned. That seems almost universal for Win32 structs (but not WinRT structs) so I may just make that work universally, but lifetime may be hard to figure out so I don't know how practical that is (#1896) so worst case they may have to just be For APIs that return resources, like |
Here is a simple OwnedHandle: use windows::Win32::Foundation::{HANDLE, CloseHandle};
/// You should only use this wrapper if the handle can be closed by `CloseHandle`.
#[derive(PartialEq, Eq, Debug)]
pub struct OwnedHandle(pub HANDLE);
impl From<HANDLE> for OwnedHandle {
fn from(handle: HANDLE) -> Self {
Self(handle)
}
}
impl Drop for OwnedHandle {
fn drop(&mut self) {
unsafe { CloseHandle(self.0) };
}
} |
@Chaoses-Ib (and @NiiightmareXD, #2638) there is already a stabilized Rust type that encapsulates While it currently seems to be impossible for |
Don't think we have any plans to adopt those |
@riverar perhaps it is worth to voice your concerns (if you haven't already done so) upstream? |
See #3013 for a solution. |
Is there an RAII type for HANDLEs that'll call CloseHandle when dropped? As far as I can tell the current HANDLE requires an explicit call to CloseHandle.
The text was updated successfully, but these errors were encountered: