Skip to main content

ComObject

Struct ComObject 

pub struct ComObject<T: ComObjectInner> { /* private fields */ }
Expand description

A counted pointer to a heap-allocated type that implements COM interfaces.

This type lets you place an object onto the heap and query for COM interfaces without losing the safe reference to the implementation object.

Because the pointer inside is non-null, Option<ComObject<T>> is the same size as a single pointer.

§Safety

The contained ptr is an owned, reference-counted pointer to a pinned Pin<Box<T::Outer>>. The implementation does not currently use Pin<T> directly but is careful not to expose unsafe semantics to safe code; callers of unsafe functions on ComObject must preserve these invariants.

Implementations§

§

impl<T: ComObjectInner> ComObject<T>

pub fn new(value: T) -> Self

Allocates a heap cell (box) and moves value into it. Returns a counted pointer to value.

pub unsafe fn from_raw(ptr: NonNull<T::Outer>) -> Self

Creates a new ComObject from an existing boxed instance.

§Safety

ptr must point to a valid, heap-allocated T::Outer (typically from Box::into_raw(Box::new(...))).

The pointed-to box must have a reference count greater than zero.

This takes ownership of the existing pointer; it does not call AddRef. The reference count must accurately reflect all outstanding references to the box, including ptr.

pub fn get(&self) -> &T

Gets a reference to the shared object stored in the box.

ComObject also implements Deref, so it is often more convenient to deref directly. Use this method when the Deref impl is inconvenient.

pub fn get_mut(&mut self) -> Option<&mut T>

Gets a mutable reference to the object stored in the box, if the reference count is exactly 1. Returns None if there are multiple references.

pub fn take(self) -> Result<T, Self>

If this object has only a single reference (i.e. this ComObject is the only reference to the heap allocation), extracts the inner T and frees the heap allocation. Returns Err(self) if there is more than one reference.

pub fn cast<I: Interface>(&self) -> Result<I>

Casts to the given interface type.

This always performs a QueryInterface, even if T is known to implement I. If you know that T implements I, use Self::as_interface or Self::to_interface instead to avoid the dynamic QueryInterface call.

pub fn as_interface<I: Interface>(&self) -> InterfaceRef<'_, I>

Gets a borrowed reference to an interface that is implemented by T.

The returned reference is not AddRefed; call InterfaceRef::to_owned to obtain an owned reference.

pub fn to_interface<I: Interface>(&self) -> I

Gets an owned (counted) reference to an interface that is implemented by this ComObject.

pub fn into_interface<I: Interface>(self) -> I

Converts self into an interface that it implements.

This does not need to adjust reference counts because self is consumed.

pub fn cast_from<I>(interface: &I) -> Result<Self>
where I: Interface, T::Outer: Any + 'static + IUnknownImpl<Impl = T>,

Casts the given COM interface to &dyn Any, returning a reference to the “outer” object (e.g. MyApp_Impl), not the inner MyApp.

T must be a type annotated with #[implement]; this is enforced at compile time by the generic constraints.

Returns Err(E_NOINTERFACE) if the object is not a Rust object, not T, or contains non-static lifetimes.

The returned value is an owned (counted) reference: this function calls AddRef. If you do not need an owned reference, use Interface::cast_object_ref instead to avoid the AddRef / Release overhead.

Trait Implementations§

§

impl<T: ComObjectInner> AsRef<T> for ComObject<T>

§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
§

impl<T: ComObjectInner> Borrow<T> for ComObject<T>

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T: ComObjectInner> Clone for ComObject<T>

§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T: ComObjectInner + Debug> Debug for ComObject<T>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
§

impl<T: ComObjectInner + Default> Default for ComObject<T>

§

fn default() -> Self

Returns the “default value” for a type. Read more
§

impl<T: ComObjectInner> Deref for ComObject<T>

§

type Target = <T as ComObjectInner>::Outer

The resulting type after dereferencing.
§

fn deref(&self) -> &Self::Target

Dereferences the value.
§

impl<T: ComObjectInner + Display> Display for ComObject<T>

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
§

impl<T: ComObjectInner> Drop for ComObject<T>

§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
§

impl<T: ComObjectInner + Eq> Eq for ComObject<T>

§

impl<T: ComObjectInner> From<T> for ComObject<T>

§

fn from(value: T) -> Self

Converts to this type from the input type.
§

impl<T: ComObjectInner + Hash> Hash for ComObject<T>

§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

impl<T: ComObjectInner + Ord> Ord for ComObject<T>

§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
§

impl<T: ComObjectInner + PartialEq> PartialEq for ComObject<T>

§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
§

impl<T: ComObjectInner + PartialOrd> PartialOrd for ComObject<T>

§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
§

impl<T: ComObjectInner + Send> Send for ComObject<T>

§

impl<T: ComObjectInner + Sync> Sync for ComObject<T>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.