| 1 |
use super::*; |
| 2 |
use core::ffi::c_void; |
| 3 |
use core::ptr::NonNull; |
| 4 |
|
| 5 |
/// All COM interfaces (and thus WinRT classes and interfaces) implement |
| 6 |
/// [IUnknown](https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown) |
| 7 |
/// under the hood to provide reference-counted lifetime management as well as the ability |
| 8 |
/// to query for additional interfaces that the object may implement. |
| 9 |
#[repr(transparent)] |
| 10 |
pub struct IUnknown(NonNull<c_void>); |
| 11 |
|
| 12 |
#[doc(hidden)] |
| 13 |
#[repr(C)] |
| 14 |
#[allow(non_camel_case_types)] |
| 15 |
pub struct IUnknown_Vtbl { |
| 16 |
pub QueryInterface: unsafe extern "system" fn( |
| 17 |
this: *mut c_void, |
| 18 |
iid: *const GUID, |
| 19 |
interface: *mut *mut c_void, |
| 20 |
) -> HRESULT, |
| 21 |
pub AddRef: unsafe extern "system" fn(this: *mut c_void) -> u32, |
| 22 |
pub Release: unsafe extern "system" fn(this: *mut c_void) -> u32, |
| 23 |
} |
| 24 |
|
| 25 |
unsafe impl Interface for IUnknown { |
| 26 |
type Vtable = IUnknown_Vtbl; |
| 27 |
const IID: GUID = GUID::from_u128(0x00000000_0000_0000_c000_000000000046); |
| 28 |
} |
| 29 |
|
| 30 |
impl Clone for IUnknown { |
| 31 |
fn clone(&self) -> Self { |
| 32 |
unsafe { |
| 33 |
(self.vtable().AddRef)(core::mem::transmute_copy(self)); |
| 34 |
} |
| 35 |
|
| 36 |
Self(self.0) |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl Drop for IUnknown { |
| 41 |
fn drop(&mut self) { |
| 42 |
unsafe { |
| 43 |
(self.vtable().Release)(core::mem::transmute_copy(self)); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl PartialEq for IUnknown { |
| 49 |
fn eq(&self, other: &Self) -> bool { |
| 50 |
// First we test for ordinary pointer equality. If two COM interface pointers have the |
| 51 |
// same pointer value, then they are the same object. This can save us a lot of time, |
| 52 |
// since calling QueryInterface is much more expensive than a single pointer comparison. |
| 53 |
// |
| 54 |
// However, interface pointers may have different values and yet point to the same object. |
| 55 |
// Since COM objects may implement multiple interfaces, COM identity can only |
| 56 |
// be determined by querying for `IUnknown` explicitly and then comparing the |
| 57 |
// pointer values. This works since `QueryInterface` is required to return |
| 58 |
// the same pointer value for queries for `IUnknown`. |
| 59 |
self.as_raw() == other.as_raw() |
| 60 |
|| self.cast::<IUnknown>().unwrap().0 == other.cast::<IUnknown>().unwrap().0 |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl Eq for IUnknown {} |
| 65 |
|
| 66 |
impl core::fmt::Debug for IUnknown { |
| 67 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 68 |
f.debug_tuple("IUnknown").field(&self.as_raw()).finish() |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/// The `#[implement]` macro generates implementations of this trait for the types |
| 73 |
/// that it generates, e.g. `MyApp_Impl`, |
| 74 |
/// |
| 75 |
/// `ComObject` uses this trait to interact with boxed COM objects. |
| 76 |
#[doc(hidden)] |
| 77 |
pub trait IUnknownImpl { |
| 78 |
/// The contained user type, e.g. `MyApp`. Also known as the "inner" type. |
| 79 |
type Impl; |
| 80 |
|
| 81 |
/// Get a reference to the backing implementation. |
| 82 |
fn get_impl(&self) -> &Self::Impl; |
| 83 |
|
| 84 |
/// Get a mutable reference to the contained (inner) object. |
| 85 |
fn get_impl_mut(&mut self) -> &mut Self::Impl; |
| 86 |
|
| 87 |
/// Consumes the box and returns the contained (inner) object. This is the opposite of `new_box`. |
| 88 |
fn into_inner(self) -> Self::Impl; |
| 89 |
|
| 90 |
/// The classic `QueryInterface` method from COM. |
| 91 |
/// |
| 92 |
/// # Safety |
| 93 |
/// |
| 94 |
/// This function is safe to call as long as the interface pointer is non-null and valid for writes |
| 95 |
/// of an interface pointer. |
| 96 |
unsafe fn QueryInterface(&self, iid: *const GUID, interface: *mut *mut c_void) -> HRESULT; |
| 97 |
|
| 98 |
/// Increments the reference count of the interface |
| 99 |
fn AddRef(&self) -> u32; |
| 100 |
|
| 101 |
/// Decrements the reference count causing the interface's memory to be freed when the count is 0 |
| 102 |
/// |
| 103 |
/// # Safety |
| 104 |
/// |
| 105 |
/// This function should only be called when the interface pointer is no longer used as calling `Release` |
| 106 |
/// on a non-aliased interface pointer and then using that interface pointer may result in use after free. |
| 107 |
/// |
| 108 |
/// This function takes `*mut Self` because the object may be freed by the time this method returns. |
| 109 |
/// Taking `&self` would violate Rust's rules on reference lifetime. |
| 110 |
unsafe fn Release(self_: *mut Self) -> u32; |
| 111 |
|
| 112 |
/// Returns `true` if the reference count of the box is equal to 1. |
| 113 |
fn is_reference_count_one(&self) -> bool; |
| 114 |
|
| 115 |
/// Gets the trust level of the current object. |
| 116 |
unsafe fn GetTrustLevel(&self, value: *mut i32) -> HRESULT; |
| 117 |
|
| 118 |
/// Given a reference to an inner type, returns a reference to the outer shared type. |
| 119 |
/// |
| 120 |
/// # Safety |
| 121 |
/// |
| 122 |
/// This function should only be called from methods that implement COM interfaces, i.e. |
| 123 |
/// implementations of methods on `IFoo_Impl` traits. |
| 124 |
// TODO: This can be made safe, if IFoo_Impl are moved to the Object_Impl types. |
| 125 |
// That requires some substantial redesign, though. |
| 126 |
unsafe fn from_inner_ref(inner: &Self::Impl) -> &Self; |
| 127 |
|
| 128 |
/// Gets a borrowed reference to an interface that is implemented by this ComObject. |
| 129 |
/// |
| 130 |
/// The returned reference does not have an additional reference count. |
| 131 |
/// You can AddRef it by calling to_owned(). |
| 132 |
#[inline(always)] |
| 133 |
fn as_interface<I: Interface>(&self) -> InterfaceRef<'_, I> |
| 134 |
where |
| 135 |
Self: ComObjectInterface<I>, |
| 136 |
{ |
| 137 |
<Self as ComObjectInterface<I>>::as_interface_ref(self) |
| 138 |
} |
| 139 |
|
| 140 |
/// Gets an owned (counted) reference to an interface that is implemented by this ComObject. |
| 141 |
#[inline(always)] |
| 142 |
fn to_interface<I: Interface>(&self) -> I |
| 143 |
where |
| 144 |
Self: ComObjectInterface<I>, |
| 145 |
{ |
| 146 |
<Self as ComObjectInterface<I>>::as_interface_ref(self).to_owned() |
| 147 |
} |
| 148 |
|
| 149 |
/// Creates a new owned reference to this object. |
| 150 |
/// |
| 151 |
/// # Safety |
| 152 |
/// |
| 153 |
/// This function can only be safely called by `<Foo>_Impl` objects that are embedded in a |
| 154 |
/// `ComObject`. Since we only allow safe Rust code to access these objects using a `ComObject` |
| 155 |
/// or a `&<Foo>_Impl` that points within a `ComObject`, this is safe. |
| 156 |
fn to_object(&self) -> ComObject<Self::Impl> |
| 157 |
where |
| 158 |
Self::Impl: ComObjectInner<Outer = Self>; |
| 159 |
|
| 160 |
/// The distance from the start of `<Foo>_Impl` to the `this` field within it, measured in |
| 161 |
/// pointer-sized elements. The `this` field contains the `MyApp` instance. |
| 162 |
const INNER_OFFSET_IN_POINTERS: usize; |
| 163 |
} |
| 164 |
|
| 165 |
impl IUnknown_Vtbl { |
| 166 |
pub const fn new<T: IUnknownImpl, const OFFSET: isize>() -> Self { |
| 167 |
unsafe extern "system" fn QueryInterface<T: IUnknownImpl, const OFFSET: isize>( |
| 168 |
this: *mut c_void, |
| 169 |
iid: *const GUID, |
| 170 |
interface: *mut *mut c_void, |
| 171 |
) -> HRESULT { |
| 172 |
let this = (this as *mut *mut c_void).offset(OFFSET) as *mut T; |
| 173 |
(*this).QueryInterface(iid, interface) |
| 174 |
} |
| 175 |
unsafe extern "system" fn AddRef<T: IUnknownImpl, const OFFSET: isize>( |
| 176 |
this: *mut c_void, |
| 177 |
) -> u32 { |
| 178 |
let this = (this as *mut *mut c_void).offset(OFFSET) as *mut T; |
| 179 |
(*this).AddRef() |
| 180 |
} |
| 181 |
unsafe extern "system" fn Release<T: IUnknownImpl, const OFFSET: isize>( |
| 182 |
this: *mut c_void, |
| 183 |
) -> u32 { |
| 184 |
let this = (this as *mut *mut c_void).offset(OFFSET) as *mut T; |
| 185 |
T::Release(this) |
| 186 |
} |
| 187 |
Self { |
| 188 |
QueryInterface: QueryInterface::<T, OFFSET>, |
| 189 |
AddRef: AddRef::<T, OFFSET>, |
| 190 |
Release: Release::<T, OFFSET>, |
| 191 |
} |
| 192 |
} |
| 193 |
} |