Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 61d67dd

Browse files
committed
Add InterfaceRc::from_raw
1 parent 7201c8e commit 61d67dd

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

src/interface_ptr.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::ComInterface;
22

3-
use std::ptr::NonNull;
4-
5-
use std::ffi::c_void;
63
use std::marker::PhantomData;
4+
use std::ptr::NonNull;
75

86
/// A transparent ptr to a COM interface.
97
///
@@ -13,7 +11,7 @@ use std::marker::PhantomData;
1311
///
1412
/// [`InterfaceRc`]: struct.InterfaceRc.html
1513
pub struct InterfacePtr<T: ?Sized + ComInterface> {
16-
ptr: NonNull<c_void>,
14+
ptr: NonNull<*mut <T as ComInterface>::VTable>,
1715
phantom: PhantomData<T>,
1816
}
1917

@@ -32,7 +30,7 @@ impl<T: ?Sized + ComInterface> InterfacePtr<T> {
3230
/// # Panics
3331
///
3432
/// Panics if `ptr` is null
35-
pub unsafe fn new(ptr: *mut c_void) -> InterfacePtr<T> {
33+
pub unsafe fn new(ptr: *mut *mut <T as ComInterface>::VTable) -> InterfacePtr<T> {
3634
InterfacePtr {
3735
ptr: NonNull::new(ptr).expect("ptr was null"),
3836
phantom: PhantomData,
@@ -41,7 +39,7 @@ impl<T: ?Sized + ComInterface> InterfacePtr<T> {
4139

4240
/// Gets the underlying interface ptr. This ptr is only guarnteed to live for
4341
/// as long as the current `InterfacePtr` is alive.
44-
pub fn as_raw(&self) -> *mut c_void {
42+
pub fn as_raw(&self) -> *mut *mut <T as ComInterface>::VTable {
4543
self.ptr.as_ptr()
4644
}
4745
}

src/interface_rc.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ impl<T: ?Sized + ComInterface> InterfaceRc<T> {
2121
InterfaceRc { ptr }
2222
}
2323

24+
pub unsafe fn from_raw(ptr: *mut *mut <T as ComInterface>::VTable) -> Self {
25+
Self::new(InterfacePtr::new(ptr))
26+
}
27+
2428
/// Gets the underlying interface ptr. This ptr is only guarnteed to live for
2529
/// as long as the current `InterfaceRc` is alive.
26-
pub fn as_raw(&self) -> *mut c_void {
30+
pub fn as_raw(&self) -> *mut *mut <T as ComInterface>::VTable {
2731
self.ptr.as_raw()
2832
}
2933

@@ -41,7 +45,9 @@ impl<T: ?Sized + ComInterface> InterfaceRc<T> {
4145
return None;
4246
}
4347
assert!(!ppv.is_null(), "The pointer to the interface returned from a successful call to QueryInterface was null");
44-
Some(InterfaceRc::new(unsafe { InterfacePtr::new(ppv) }))
48+
Some(InterfaceRc::new(unsafe {
49+
InterfacePtr::new(ppv as *mut *mut _)
50+
}))
4551
}
4652
}
4753

src/interfaces/iclass_factory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ffi::c_void;
44

55
use crate::{
66
interfaces::iunknown::{IUnknown, IUnknownVPtr},
7-
ComInterface, InterfacePtr, InterfaceRc,
7+
ComInterface, InterfaceRc,
88
};
99

1010
#[com_interface("00000001-0000-0000-c000-000000000046")]
@@ -27,6 +27,6 @@ impl InterfaceRc<dyn IClassFactory> {
2727
// TODO: decide what failures are possible
2828
return None;
2929
}
30-
Some(InterfaceRc::new(unsafe { InterfacePtr::new(ppv) }))
30+
Some(unsafe { InterfaceRc::from_raw(ppv as *mut _) })
3131
}
3232
}

src/runtime.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ impl ApartmentThreadedRuntime {
5050
return Err(hr);
5151
}
5252

53-
Ok(InterfaceRc::new(unsafe {
54-
InterfacePtr::new(class_factory)
55-
}))
53+
Ok(unsafe { InterfaceRc::from_raw(class_factory as *mut *mut _) })
5654
}
5755

5856
pub fn create_instance<T: ComInterface + ?Sized>(
@@ -91,7 +89,7 @@ impl ApartmentThreadedRuntime {
9189
return Err(hr);
9290
}
9391

94-
Ok(InterfacePtr::new(instance))
92+
Ok(InterfacePtr::new(instance as *mut _))
9593
}
9694
}
9795

src/sys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub const COINIT_APARTMENTTHREADED: u32 = 0x2;
2121
pub const CLSCTX_INPROC_SERVER: u32 = 0x1;
2222

2323
#[repr(C)]
24-
#[derive(PartialEq)]
24+
#[derive(Copy, Clone, PartialEq)]
2525
pub struct IID {
2626
pub data1: u32,
2727
pub data2: u16,

0 commit comments

Comments
 (0)