| 1 |
use crate::imp::Box; |
| 2 |
use crate::{AsImpl, IUnknown, IUnknownImpl, Interface, InterfaceRef}; |
| 3 |
use core::any::Any; |
| 4 |
use core::borrow::Borrow; |
| 5 |
use core::ops::Deref; |
| 6 |
use core::ptr::NonNull; |
| 7 |
|
| 8 |
/// Identifies types that can be placed in [`ComObject`]. |
| 9 |
/// |
| 10 |
/// This trait links types that can be placed in `ComObject` with the types generated by the |
| 11 |
/// `#[implement]` macro. The `#[implement]` macro generates implementations of this trait. |
| 12 |
/// The generated types contain the vtable layouts and refcount-related fields for the COM |
| 13 |
/// object implementation. |
| 14 |
/// |
| 15 |
/// This trait is an implementation detail of the Windows crates. |
| 16 |
/// User code should not deal directly with this trait. |
| 17 |
/// |
| 18 |
/// This trait is sort of the reverse of [`IUnknownImpl`]. This trait allows user code to use |
| 19 |
/// [`ComObject<T>`] instead of `ComObject<T_Impl>`. |
| 20 |
pub trait ComObjectInner: Sized { |
| 21 |
/// The generated `<foo>_Impl` type (aka the "boxed" type or "outer" type). |
| 22 |
type Outer: IUnknownImpl<Impl = Self>; |
| 23 |
|
| 24 |
/// Moves an instance of this type into a new ComObject box and returns it. |
| 25 |
/// |
| 26 |
/// # Safety |
| 27 |
/// |
| 28 |
/// It is important that safe Rust code never be able to acquire an owned instance of a |
| 29 |
/// generated "outer" COM object type, e.g. `<foo>_Impl`. This would be unsafe because the |
| 30 |
/// `<foo>_Impl` object contains a reference count field and provides methods that adjust |
| 31 |
/// the reference count, and destroy the object when the reference count reaches zero. |
| 32 |
/// |
| 33 |
/// Safe Rust code must only be able to interact with these values by accessing them via a |
| 34 |
/// `ComObject` reference. `ComObject` handles adjusting reference counts and associates the |
| 35 |
/// lifetime of a `&<foo>_Impl` with the lifetime of the related `ComObject`. |
| 36 |
/// |
| 37 |
/// The `#[implement]` macro generates the implementation of this `into_object` method. |
| 38 |
/// The generated `into_object` method encapsulates the construction of the `<foo>_Impl` |
| 39 |
/// object and immediately places it into the heap and returns a `ComObject` reference to it. |
| 40 |
/// This ensures that our requirement -- that safe Rust code never own a `<foo>_Impl` value |
| 41 |
/// directly -- is met. |
| 42 |
fn into_object(self) -> ComObject<Self>; |
| 43 |
} |
| 44 |
|
| 45 |
/// Describes the COM interfaces implemented by a specific COM object. |
| 46 |
/// |
| 47 |
/// The `#[implement]` macro generates implementations of this trait. Implementations are attached |
| 48 |
/// to the "outer" types generated by `#[implement]`, e.g. the `MyApp_Impl` type. Each |
| 49 |
/// implementation knows how to locate the interface-specific field within `MyApp_Impl`. |
| 50 |
/// |
| 51 |
/// This trait is an implementation detail of the Windows crates. |
| 52 |
/// User code should not deal directly with this trait. |
| 53 |
pub trait ComObjectInterface<I: Interface> { |
| 54 |
/// Gets a borrowed interface that is implemented by `T`. |
| 55 |
fn as_interface_ref(&self) -> InterfaceRef<'_, I>; |
| 56 |
} |
| 57 |
|
| 58 |
/// A counted pointer to a type that implements COM interfaces, where the object has been |
| 59 |
/// placed in the heap (boxed). |
| 60 |
/// |
| 61 |
/// This type exists so that you can place an object into the heap and query for COM interfaces, |
| 62 |
/// without losing the safe reference to the implementation object. |
| 63 |
/// |
| 64 |
/// Because the pointer inside this type is known to be non-null, `Option<ComObject<T>>` should |
| 65 |
/// always have the same size as a single pointer. |
| 66 |
/// |
| 67 |
/// # Safety |
| 68 |
/// |
| 69 |
/// The contained `ptr` field is an owned, reference-counted pointer to a _pinned_ `Pin<Box<T::Outer>>`. |
| 70 |
/// Although this code does not currently use `Pin<T>`, it takes care not to expose any unsafe semantics |
| 71 |
/// to safe code. However, code that calls unsafe functions on [`ComObject`] must, like all unsafe code, |
| 72 |
/// understand and preserve invariants. |
| 73 |
#[repr(transparent)] |
| 74 |
pub struct ComObject<T: ComObjectInner> { |
| 75 |
ptr: NonNull<T::Outer>, |
| 76 |
} |
| 77 |
|
| 78 |
impl<T: ComObjectInner> ComObject<T> { |
| 79 |
/// Allocates a heap cell (box) and moves `value` into it. Returns a counted pointer to `value`. |
| 80 |
pub fn new(value: T) -> Self { |
| 81 |
T::into_object(value) |
| 82 |
} |
| 83 |
|
| 84 |
/// Creates a new `ComObject` that points to an existing boxed instance. |
| 85 |
/// |
| 86 |
/// # Safety |
| 87 |
/// |
| 88 |
/// The caller must ensure that `ptr` points to a valid, heap-allocated instance of `T::Outer`. |
| 89 |
/// Normally, this pointer comes from using `Box::into_raw(Box::new(...))`. |
| 90 |
/// |
| 91 |
/// The pointed-to box must have a reference count that is greater than zero. |
| 92 |
/// |
| 93 |
/// This function takes ownership of the existing pointer; it does not call `AddRef`. |
| 94 |
/// The reference count must accurately reflect all outstanding references to the box, |
| 95 |
/// including `ptr` in the count. |
| 96 |
pub unsafe fn from_raw(ptr: NonNull<T::Outer>) -> Self { |
| 97 |
Self { ptr } |
| 98 |
} |
| 99 |
|
| 100 |
/// Gets a reference to the shared object stored in the box. |
| 101 |
/// |
| 102 |
/// [`ComObject`] also implements [`Deref`], so you can often deref directly into the object. |
| 103 |
/// For those situations where using the [`Deref`] impl is inconvenient, you can use |
| 104 |
/// this method to explicitly get a reference to the contents. |
| 105 |
#[inline(always)] |
| 106 |
pub fn get(&self) -> &T { |
| 107 |
self.get_box().get_impl() |
| 108 |
} |
| 109 |
|
| 110 |
/// Gets a reference to the shared object's heap box. |
| 111 |
#[inline(always)] |
| 112 |
fn get_box(&self) -> &T::Outer { |
| 113 |
unsafe { self.ptr.as_ref() } |
| 114 |
} |
| 115 |
|
| 116 |
// Note that we _do not_ provide a way to get a mutable reference to the outer box. |
| 117 |
// It's ok to return `&mut T`, but not `&mut T::Outer`. That would allow someone to replace the |
| 118 |
// contents of the entire object (box and reference count), which could lead to UB. |
| 119 |
// This could maybe be solved by returning `Pin<&mut T::Outer>`, but that requires some |
| 120 |
// additional thinking. |
| 121 |
|
| 122 |
/// Gets a mutable reference to the object stored in the box, if the reference count |
| 123 |
/// is exactly 1. If there are multiple references to this object then this returns `None`. |
| 124 |
#[inline(always)] |
| 125 |
pub fn get_mut(&mut self) -> Option<&mut T> { |
| 126 |
if self.is_reference_count_one() { |
| 127 |
// SAFETY: We must only return &mut T, *NOT* &mut T::Outer. |
| 128 |
// Returning T::Outer would allow swapping the contents of the object, which would |
| 129 |
// allow (incorrectly) modifying the reference count. |
| 130 |
unsafe { Some(self.ptr.as_mut().get_impl_mut()) } |
| 131 |
} else { |
| 132 |
None |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/// If this object has only a single object reference (i.e. this [`ComObject`] is the only |
| 137 |
/// reference to the heap allocation), then this method will extract the inner `T` |
| 138 |
/// (and return it in an `Ok`) and then free the heap allocation. |
| 139 |
/// |
| 140 |
/// If there is more than one reference to this object, then this returns `Err(self)`. |
| 141 |
#[inline(always)] |
| 142 |
pub fn take(self) -> Result<T, Self> { |
| 143 |
if self.is_reference_count_one() { |
| 144 |
let outer_box: Box<T::Outer> = unsafe { core::mem::transmute(self) }; |
| 145 |
Ok(outer_box.into_inner()) |
| 146 |
} else { |
| 147 |
Err(self) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/// Casts to a given interface type. |
| 152 |
/// |
| 153 |
/// This always performs a `QueryInterface`, even if `T` is known to implement `I`. |
| 154 |
/// If you know that `T` implements `I`, then use [`Self::as_interface`] or [`Self::to_interface`] because |
| 155 |
/// those functions do not require a dynamic `QueryInterface` call. |
| 156 |
#[inline(always)] |
| 157 |
pub fn cast<I: Interface>(&self) -> windows_core::Result<I> |
| 158 |
where |
| 159 |
T::Outer: ComObjectInterface<IUnknown>, |
| 160 |
{ |
| 161 |
let unknown = self.as_interface::<IUnknown>(); |
| 162 |
unknown.cast() |
| 163 |
} |
| 164 |
|
| 165 |
/// Gets a borrowed reference to an interface that is implemented by `T`. |
| 166 |
/// |
| 167 |
/// The returned reference does not have an additional reference count. |
| 168 |
/// You can AddRef it by calling [`InterfaceRef::to_owned`]. |
| 169 |
#[inline(always)] |
| 170 |
pub fn as_interface<I: Interface>(&self) -> InterfaceRef<'_, I> |
| 171 |
where |
| 172 |
T::Outer: ComObjectInterface<I>, |
| 173 |
{ |
| 174 |
self.get_box().as_interface_ref() |
| 175 |
} |
| 176 |
|
| 177 |
/// Gets an owned (counted) reference to an interface that is implemented by this [`ComObject`]. |
| 178 |
#[inline(always)] |
| 179 |
pub fn to_interface<I: Interface>(&self) -> I |
| 180 |
where |
| 181 |
T::Outer: ComObjectInterface<I>, |
| 182 |
{ |
| 183 |
self.as_interface::<I>().to_owned() |
| 184 |
} |
| 185 |
|
| 186 |
/// Converts `self` into an interface that it implements. |
| 187 |
/// |
| 188 |
/// This does not need to adjust reference counts because `self` is consumed. |
| 189 |
#[inline(always)] |
| 190 |
pub fn into_interface<I: Interface>(self) -> I |
| 191 |
where |
| 192 |
T::Outer: ComObjectInterface<I>, |
| 193 |
{ |
| 194 |
unsafe { |
| 195 |
let raw = self.get_box().as_interface_ref().as_raw(); |
| 196 |
core::mem::forget(self); |
| 197 |
I::from_raw(raw) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/// This casts the given COM interface to [`&dyn Any`]. It returns a reference to the "outer" |
| 202 |
/// object, e.g. `MyApp_Impl`, not the inner `MyApp` object. |
| 203 |
/// |
| 204 |
/// `T` must be a type that has been annotated with `#[implement]`; this is checked at |
| 205 |
/// compile-time by the generic constraints of this method. However, note that the |
| 206 |
/// returned `&dyn Any` refers to the _outer_ implementation object that was generated by |
| 207 |
/// `#[implement]`, i.e. the `MyApp_Impl` type, not the inner `MyApp` type. |
| 208 |
/// |
| 209 |
/// If the given object is not a Rust object, or is a Rust object but not `T`, or is a Rust |
| 210 |
/// object that contains non-static lifetimes, then this function will return `Err(E_NOINTERFACE)`. |
| 211 |
/// |
| 212 |
/// The returned value is an owned (counted) reference; this function calls `AddRef` on the |
| 213 |
/// underlying COM object. If you do not need an owned reference, then you can use the |
| 214 |
/// [`Interface::cast_object_ref`] method instead, and avoid the cost of `AddRef` / `Release`. |
| 215 |
pub fn cast_from<I>(interface: &I) -> crate::Result<Self> |
| 216 |
where |
| 217 |
I: Interface, |
| 218 |
T::Outer: Any + 'static + IUnknownImpl<Impl = T>, |
| 219 |
{ |
| 220 |
interface.cast_object() |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
impl<T: ComObjectInner + Default> Default for ComObject<T> { |
| 225 |
fn default() -> Self { |
| 226 |
Self::new(T::default()) |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
impl<T: ComObjectInner> Drop for ComObject<T> { |
| 231 |
fn drop(&mut self) { |
| 232 |
unsafe { |
| 233 |
T::Outer::Release(self.ptr.as_ptr()); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
impl<T: ComObjectInner> Clone for ComObject<T> { |
| 239 |
#[inline(always)] |
| 240 |
fn clone(&self) -> Self { |
| 241 |
unsafe { |
| 242 |
self.ptr.as_ref().AddRef(); |
| 243 |
Self { ptr: self.ptr } |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
impl<T: ComObjectInner> AsRef<T> for ComObject<T> |
| 249 |
where |
| 250 |
IUnknown: From<T> + AsImpl<T>, |
| 251 |
{ |
| 252 |
#[inline(always)] |
| 253 |
fn as_ref(&self) -> &T { |
| 254 |
self.get() |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
impl<T: ComObjectInner> Deref for ComObject<T> { |
| 259 |
type Target = T::Outer; |
| 260 |
|
| 261 |
#[inline(always)] |
| 262 |
fn deref(&self) -> &Self::Target { |
| 263 |
self.get_box() |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// There is no DerefMut implementation because we cannot statically guarantee |
| 268 |
// that the reference count is 1, which is a requirement for getting exclusive |
| 269 |
// access to the contents of the object. Use get_mut() for dynamically-checked |
| 270 |
// exclusive access. |
| 271 |
|
| 272 |
impl<T: ComObjectInner> From<T> for ComObject<T> { |
| 273 |
fn from(value: T) -> ComObject<T> { |
| 274 |
ComObject::new(value) |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
// Delegate hashing, if implemented. |
| 279 |
impl<T: ComObjectInner + core::hash::Hash> core::hash::Hash for ComObject<T> { |
| 280 |
fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 281 |
self.get().hash(state); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
// If T is Send (or Sync) then the ComObject<T> is also Send (or Sync). |
| 286 |
// Since the actual object storage is in the heap, the object is never moved. |
| 287 |
unsafe impl<T: ComObjectInner + Send> Send for ComObject<T> {} |
| 288 |
unsafe impl<T: ComObjectInner + Sync> Sync for ComObject<T> {} |
| 289 |
|
| 290 |
impl<T: ComObjectInner + PartialEq> PartialEq for ComObject<T> { |
| 291 |
fn eq(&self, other: &ComObject<T>) -> bool { |
| 292 |
let inner_self: &T = self.get(); |
| 293 |
let other_self: &T = other.get(); |
| 294 |
inner_self == other_self |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
impl<T: ComObjectInner + Eq> Eq for ComObject<T> {} |
| 299 |
|
| 300 |
impl<T: ComObjectInner + PartialOrd> PartialOrd for ComObject<T> { |
| 301 |
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
| 302 |
let inner_self: &T = self.get(); |
| 303 |
let other_self: &T = other.get(); |
| 304 |
<T as PartialOrd>::partial_cmp(inner_self, other_self) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
impl<T: ComObjectInner + Ord> Ord for ComObject<T> { |
| 309 |
fn cmp(&self, other: &Self) -> core::cmp::Ordering { |
| 310 |
let inner_self: &T = self.get(); |
| 311 |
let other_self: &T = other.get(); |
| 312 |
<T as Ord>::cmp(inner_self, other_self) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
impl<T: ComObjectInner + core::fmt::Debug> core::fmt::Debug for ComObject<T> { |
| 317 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 318 |
<T as core::fmt::Debug>::fmt(self.get(), f) |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
impl<T: ComObjectInner + core::fmt::Display> core::fmt::Display for ComObject<T> { |
| 323 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 324 |
<T as core::fmt::Display>::fmt(self.get(), f) |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
impl<T: ComObjectInner> Borrow<T> for ComObject<T> { |
| 329 |
fn borrow(&self) -> &T { |
| 330 |
self.get() |
| 331 |
} |
| 332 |
} |