com_impl/lib.rs
1#![cfg(windows)]
2
3//! Implements a COM Object struct with automatic reference counting and implements
4//! IUnknown for you. This covers the most common use cases of creating COM objects
5//! from Rust. Supports generic parameters!
6//!
7//! ```
8//! use winapi::ctypes::c_void;
9//! use winapi::shared::winerror::{ERROR_INVALID_INDEX, HRESULT, HRESULT_FROM_WIN32, S_OK};
10//! use winapi::um::dwrite::{IDWriteFontFileStream, IDWriteFontFileStreamVtbl};
11//! use wio::com::ComPtr;
12//!
13//! #[repr(C)]
14//! #[derive(com_impl::ComImpl)]
15//! #[interfaces(IDWriteFontFileStream)]
16//! pub struct FileStream {
17//! vtbl: com_impl::VTable<IDWriteFontFileStreamVtbl>,
18//! refcount: com_impl::Refcount,
19//! write_time: u64,
20//! file_data: Vec<u8>,
21//! }
22//!
23//! impl FileStream {
24//! pub fn new(write_time: u64, data: Vec<u8>) -> ComPtr<IDWriteFontFileStream> {
25//! let ptr = FileStream::create_raw(write_time, data);
26//! let ptr = ptr as *mut IDWriteFontFileStream;
27//! unsafe { ComPtr::from_raw(ptr) }
28//! }
29//! }
30//!
31//! #[com_impl::com_impl]
32//! unsafe impl IDWriteFontFileStream for FileStream {
33//! unsafe fn get_file_size(&self, size: *mut u64) -> HRESULT {
34//! *size = self.file_data.len() as u64;
35//! S_OK
36//! }
37//!
38//! unsafe fn get_last_write_time(&self, write_time: *mut u64) -> HRESULT {
39//! *write_time = self.write_time;
40//! S_OK
41//! }
42//!
43//! unsafe fn read_file_fragment(
44//! &self,
45//! start: *mut *const c_void,
46//! offset: u64,
47//! size: u64,
48//! ctx: *mut *mut c_void,
49//! ) -> HRESULT {
50//! if offset > std::isize::MAX as u64 || size > std::isize::MAX as u64 {
51//! return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
52//! }
53//!
54//! let offset = offset as usize;
55//! let size = size as usize;
56//!
57//! if offset + size > self.file_data.len() {
58//! return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX);
59//! }
60//!
61//! *start = self.file_data.as_ptr().offset(offset as isize) as *const c_void;
62//! *ctx = std::ptr::null_mut();
63//!
64//! S_OK
65//! }
66//!
67//! unsafe fn release_file_fragment(&self, _ctx: *mut c_void) {
68//! // Nothing to do
69//! }
70//! }
71//!
72//! fn main() {
73//! let ptr = FileStream::new(100, vec![0xDE, 0xAF, 0x00, 0xF0, 0x01]);
74//!
75//! // Do things with ptr
76//! }
77//! ```
78
79extern crate derive_com_impl;
80extern crate winapi;
81
82use std::sync::atomic::{AtomicUsize, Ordering};
83
84pub use derive_com_impl::{com_impl, ComImpl};
85
86#[repr(transparent)]
87/// Wrapper for the C++ VTable member of a COM object.
88///
89/// When you're using `#[derive(ComImpl)]`, this should be the first member of your struct.
90pub struct VTable<T> {
91 pub ptr: *const T,
92}
93
94impl<T> VTable<T> {
95 pub fn new(ptr: &'static T) -> Self {
96 VTable { ptr }
97 }
98}
99
100impl<T> std::fmt::Debug for VTable<T> {
101 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
102 fmt.debug_tuple("VTable").field(&self.ptr).finish()
103 }
104}
105
106/// Trait that allows accessing the VTable for all of the COM interfaces your object
107/// implements.
108pub unsafe trait BuildVTable<T: 'static> {
109 const VTBL: T;
110 fn static_vtable() -> VTable<T>;
111}
112
113#[derive(Debug)]
114/// Refcounter object for automatic COM Object implementations. Atomically keeps track of
115/// the reference count so that the implementation of IUnknown can properly deallocate
116/// the object when all reference counts are gone.
117pub struct Refcount {
118 count: AtomicUsize,
119}
120
121impl Default for Refcount {
122 fn default() -> Self {
123 Refcount {
124 count: AtomicUsize::new(1),
125 }
126 }
127}
128
129impl Refcount {
130 #[inline]
131 /// `fetch_add(1, Acquire) + 1`
132 pub unsafe fn add_ref(&self) -> u32 {
133 self.count.fetch_add(1, Ordering::Acquire) as u32 + 1
134 }
135
136 #[inline]
137 /// `fetch_sub(1, Release) - 1`
138 pub unsafe fn release(&self) -> u32 {
139 self.count.fetch_sub(1, Ordering::Release) as u32 - 1
140 }
141}