Skip to content
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

Adds unsafe helpers to cast from C++ or raw IUnknown pointers to Rust IUnknown values #2010

Merged
merged 4 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/libs/windows/src/core/unknown.rs
Expand Up @@ -7,6 +7,18 @@ use super::*;
#[repr(transparent)]
pub struct IUnknown(core::ptr::NonNull<core::ffi::c_void>);

impl IUnknown {
/// Creates an `IUnknown` value by taking ownership of the `raw` pointer.
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
pub unsafe fn from_raw_owned(raw: *mut core::ffi::c_void) -> Self {
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
std::mem::transmute(raw)
}

/// Creates a borrowed `IUnknown` that is valid so long as the `raw` pointer is valid.
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
pub unsafe fn from_raw_borrowed<'a>(raw: &'a *mut core::ffi::c_void) -> &'a Self {
std::mem::transmute(raw)
}
}

#[doc(hidden)]
#[repr(C)]
pub struct IUnknownVtbl {
Expand Down
2 changes: 2 additions & 0 deletions crates/tests/core/Cargo.toml
Expand Up @@ -7,6 +7,8 @@ edition = "2018"
[dependencies.windows]
path = "../../libs/windows"
features = [
"implement",
"interface",
"Win32_Foundation",
"Win32_System_WinRT",
]
Expand Down
54 changes: 54 additions & 0 deletions crates/tests/core/tests/unknown.rs
@@ -0,0 +1,54 @@
#![allow(non_snake_case)]

use windows::core::*;

#[interface("f7ea748b-8121-41c1-aaee-406ba6f148a9")]
unsafe trait ITest: IUnknown {
unsafe fn Test(&self) -> u32;
}

#[implement(ITest)]
struct Test {
drop: *mut u32,
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
}

impl ITest_Impl for Test {
unsafe fn Test(&self) -> u32 {
*self.drop
}
}

impl Drop for Test {
fn drop(&mut self) {
unsafe {
*self.drop += 1;
}
}
}

#[test]
fn test() {
unsafe {
let mut dropped = 0;
let test: ITest = Test { drop: &mut dropped }.into();

{
let raw_borrowed: *mut std::ffi::c_void = test.as_raw();
let unknown_borrowed: &IUnknown = IUnknown::from_raw_borrowed(&raw_borrowed);
assert_eq!(unknown_borrowed.as_raw(), test.as_raw());
let test_query: ITest = unknown_borrowed.cast().unwrap();
assert_eq!(test_query.Test(), 0);
}
{
let raw_owned: *mut std::ffi::c_void = std::mem::transmute(test.clone());
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
let unknown_owned: IUnknown = IUnknown::from_raw_owned(raw_owned);
assert_eq!(unknown_owned.as_raw(), test.as_raw());
let test_query: ITest = unknown_owned.cast().unwrap();
assert_eq!(test_query.Test(), 0);
}

assert_eq!(test.Test(), 0);
drop(test);
assert_eq!(dropped, 1);
}
}