Skip to content

Commit

Permalink
Remove some unnecessary ComPtr methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Boddlnagg committed Aug 3, 2019
1 parent 9e873c9 commit 1733e10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/cominterfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ComInterfaceAbi for w::um::unknwnbase::IUnknown {
}
impl ComInterface for IUnknown {
type TAbi = w::um::unknwnbase::IUnknown;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { IUnknown(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { IUnknown(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}

Expand All @@ -80,7 +80,7 @@ pub struct IRestrictedErrorInfo(ComPtr<ComAbi<w::um::restrictederrorinfo::IRestr
impl ComIid for IRestrictedErrorInfo { #[inline] fn iid() -> &'static Guid { &IID_IRestrictedErrorInfo } }
impl ComInterface for IRestrictedErrorInfo {
type TAbi = ComAbi<w::um::restrictederrorinfo::IRestrictedErrorInfoVtbl>;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { IRestrictedErrorInfo(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { IRestrictedErrorInfo(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}

Expand All @@ -106,6 +106,6 @@ impl std::ops::DerefMut for IAgileObject {
impl ComIid for IAgileObject { #[inline] fn iid() -> &'static Guid { &IID_IAgileObject } }
impl ComInterface for IAgileObject {
type TAbi = ComAbi<w::um::unknwnbase::IUnknownVtbl>;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { IAgileObject(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { IAgileObject(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}
35 changes: 5 additions & 30 deletions src/comptr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::{Deref, DerefMut};
use std::fmt;
use std::ptr;
use crate::{ComInterface, ComInterfaceAbi, RtInterface, IInspectable};
use crate::{ComInterface, ComInterfaceAbi};

use w::shared::minwindef::LPVOID;
use w::um::unknwnbase::IUnknown;
Expand Down Expand Up @@ -31,13 +30,6 @@ impl<Vtbl> Clone for ComAbi<Vtbl> {
#[repr(transparent)]
pub(crate) struct ComPtr<T: ComInterfaceAbi>(ptr::NonNull<T>);

impl<T: ComInterfaceAbi> fmt::Pointer for ComPtr<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.0, f)
}
}

pub(crate) trait ComPtrHelpers {
/// Changes the type of the underlying COM object to a different interface without doing `QueryInterface`.
/// This is a runtime no-op, but you need to be sure that the interface is compatible.
Expand All @@ -64,31 +56,14 @@ impl<T: ComInterfaceAbi> ComPtr<T> {
/// It takes ownership over the pointer which means it does __not__ call `AddRef`.
/// The wrapped pointer must not be null.
#[inline]
pub unsafe fn wrap_nonnull(ptr: *mut T) -> ComPtr<T> {
pub unsafe fn wrap(ptr: *mut T) -> ComPtr<T> {
debug_assert!(!ptr.is_null());
ComPtr(ptr::NonNull::new_unchecked(ptr))
}

/// Creates an optional `ComPtr` to wrap a raw pointer that may be null.
/// It takes ownership over the pointer which means it does __not__ call `AddRef`.
#[inline]
pub unsafe fn wrap(ptr: *mut T) -> Option<ComPtr<T>> {
if ptr.is_null() {
None
} else {
Some(ComPtr(ptr::NonNull::new_unchecked(ptr)))
}
}

/// Returns the underlying WinRT object as a reference to an `IInspectable` object.
#[inline]
fn as_inspectable(&self) -> &mut IInspectable where T: RtInterface {
unsafe { &mut *(self.0.as_ptr() as *mut IInspectable) }
}

/// Returns the underlying WinRT or COM object as a reference to an `IUnknown` object.
#[inline]
fn as_unknown(&self) -> &mut IUnknown {
pub fn as_unknown(&self) -> &mut IUnknown {
unsafe { &mut *(self.0.as_ptr() as *mut IUnknown) }
}

Expand All @@ -103,7 +78,7 @@ impl<T: ComInterfaceAbi> Clone for ComPtr<T> {
fn clone(&self) -> Self {
unsafe {
self.as_unknown().AddRef();
ComPtr::wrap_nonnull(self.0.as_ptr())
ComPtr::wrap(self.0.as_ptr())
}
}
}
Expand Down Expand Up @@ -163,7 +138,7 @@ impl<T> Drop for ComArray<T> where T: crate::RtType {
#[inline]
fn drop(&mut self) {
unsafe {
std::ptr::drop_in_place(&mut self[..]);
ptr::drop_in_place(&mut self[..]);
CoTaskMemFree(self.first.as_ptr() as LPVOID)
};
}
Expand Down
10 changes: 5 additions & 5 deletions src/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ macro_rules! RT_INTERFACE {
}
impl ComInterface for $interface {
type TAbi = crate::comptr::ComAbi<$vtbl>;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}
impl crate::RtType for $interface {
Expand Down Expand Up @@ -362,7 +362,7 @@ macro_rules! RT_INTERFACE {
}
impl ComInterface for $interface {
type TAbi = crate::comptr::ComAbi<$vtbl>;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}
impl crate::RtType for $interface {
Expand Down Expand Up @@ -408,7 +408,7 @@ macro_rules! RT_INTERFACE {
pub struct $interface<$t1: RtType>(ComPtr<crate::comptr::ComAbi<$vtbl<$t1>>>);
impl<$t1> ComInterface for $interface<$t1> where $t1: RtType {
type TAbi = crate::comptr::ComAbi<$vtbl<$t1>>;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}
impl<$t1> crate::RtType for $interface<$t1> where $t1: RtType{
Expand Down Expand Up @@ -453,7 +453,7 @@ macro_rules! RT_INTERFACE {
pub struct $interface<$t1: RtType, $t2: RtType>(ComPtr<crate::comptr::ComAbi<$vtbl<$t1, $t2>>>);
impl<$t1, $t2> ComInterface for $interface<$t1, $t2> where $t1: RtType, $t2: RtType {
type TAbi = crate::comptr::ComAbi<$vtbl<$t1, $t2>>;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $interface(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}
impl<$t1, $t2> crate::RtType for $interface<$t1, $t2> where $t1: RtType, $t2: RtType {
Expand Down Expand Up @@ -634,7 +634,7 @@ macro_rules! RT_CLASS {
unsafe impl crate::RtClassInterface for $cls {}
impl ComInterface for $cls {
type TAbi = <$interface as ComInterface>::TAbi;
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $cls(ComPtr::wrap_nonnull(ptr)) }
unsafe fn wrap_com(ptr: *mut Self::TAbi) -> Self { $cls(ComPtr::wrap(ptr)) }
fn get_abi(&self) -> &Self::TAbi { self.0.as_abi() }
}
impl ComIid for $cls {
Expand Down

0 comments on commit 1733e10

Please sign in to comment.