com_rs

Struct ComPtr

Source
pub struct ComPtr<T: ComInterface> { /* private fields */ }
Expand description

Wrapper type for COM interface pointers.

§Usage

§Passing COM pointers to/from FFI methods

ComPtr<T> has the following methods for accessing the underlying pointer:

  • as_ptr returns the raw pointer *const T
  • as_mut_ptr returns a mutable reference to the raw pointer &mut *mut T

The AsComPtr trait defines which pointer types can be returned by these methods. These methods should be used carefully to ensure the returned pointers do not outlive the ComPtr object.

extern crate com_rs;
use com_rs::*;

fn create_iunknown_object(p: *mut *mut IUnknown) { }
fn use_iunknown_object(p: *const IUnknown) { }

fn main() {
    let mut unknown: ComPtr<IUnknown> = ComPtr::new();
    create_iunknown_object(unknown.as_mut_ptr());
    use_iunknown_object(unknown.as_ptr());
}

§Reference Counting

ComPtr implements the Clone and Drop traits, which call the IUnknown::add_ref and IUnknown::release methods respectively to handle the internal reference counting.

§Accessing COM interface methods

ComPtr<T> coerces into T using the Deref trait, allowing interface methods to be called directly. However, dereferencing a ComPtr containing a null pointer in this way results in a panic. All method calls should be guarded with is_null checks to prevent this.

let mut ptr: ComPtr<IUnknown> = ComPtr::new();
create_iunknown_object(ptr.as_mut_ptr());
if !ptr.is_null() {
    // This is just for demonstration, don't call these directly
    unsafe { ptr.add_ref() };
    unsafe { ptr.release() };
}

§Conversion using From

ComPtr<T> also implements the From trait for conversion between different COM interfaces. This is a wrapper around the IUnknown::query_interface method which automatically uses the IID of the target type.

let mut unknown: ComPtr<IUnknown> = ComPtr::new();
create_iunknown_object(unknown.as_mut_ptr());
let other: ComPtr<IFoobar> = ComPtr::from(&unknown);

This will try to query the IFoobar interface on the unknown object. If the interface is unavailable (or unknown is null), the returned object will be null.

Implementations§

Source§

impl<T: ComInterface> ComPtr<T>

Source

pub fn new() -> ComPtr<T>

Constructs a new ComPtr<T>.

Source

pub fn as_ptr<U>(&self) -> *const U
where T: AsComPtr<U>,

Returns the raw pointer as type U. Depends on the AsComPtr trait.

Source

pub fn as_mut_ptr<U>(&mut self) -> &mut *mut U
where T: AsComPtr<U>,

Returns a mutable reference to the raw pointer. Depends on the ‘AsComPtr’ trait.

Source

pub fn is_null(&self) -> bool

Returns true if the contained interface pointer is null. This should always be checked before calling any methods on the contained interface.

Source

pub fn iid(&self) -> IID

Return the IID associated with type T.

Trait Implementations§

Source§

impl<T: ComInterface> Clone for ComPtr<T>

Source§

fn clone(&self) -> ComPtr<T>

Clones the ComPtr<T>. Increments the internal reference counter by calling IUnknown::add_ref on the contained COM object (if the pointer is non-null).

1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ComInterface> Debug for ComPtr<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: ComInterface> Deref for ComPtr<T>

Source§

fn deref<'a>(&'a self) -> &'a T

Dereference into contained interface T to call methods directly.

§Panics

If the contained pointer is null, any dereference will result in a panic. Use the is_null method before dereferencing.

Source§

type Target = T

The resulting type after dereferencing.
Source§

impl<T: ComInterface> Drop for ComPtr<T>

Source§

fn drop(&mut self)

Drops the ComPtr<T>. Decrements the internal reference counter by calling IUnknown::release on the contained COM object (if the pointer is non-null).

Source§

impl<'a, T, U> From<&'a ComPtr<T>> for ComPtr<U>

Source§

fn from(other: &'a ComPtr<T>) -> ComPtr<U>

Create a ComPtr of a different interface type U. Calls IUnknown::query_interface and returns a new ComPtr<U> object. If the requested interface is unavailable, the returned ComPtr<U> will contain a null pointer.

Auto Trait Implementations§

§

impl<T> Freeze for ComPtr<T>

§

impl<T> RefUnwindSafe for ComPtr<T>
where T: RefUnwindSafe,

§

impl<T> !Send for ComPtr<T>

§

impl<T> !Sync for ComPtr<T>

§

impl<T> Unpin for ComPtr<T>

§

impl<T> UnwindSafe for ComPtr<T>
where T: RefUnwindSafe,

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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<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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.