microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.23k stars 476 forks source link

Provide an explicit `ComObject<T>` type that represents a heap-allocated COM object #3043

Closed sivadeilra closed 3 months ago

sivadeilra commented 3 months ago

This adds a new type, ComObject<T>, which represents a type that was annotated with #[implement] and which has been placed in the heap. ComObject is conceptually similar to the ATL/MFC CComObject type.

The ComObject type and the #[implement] macro work together. They provide efficient (zero-cost) ways to cast to the interfaces that T implements. There are no runtime checks (and no addref/release), since we know at compile time that T implements those interfaces.

ComObject is a counted reference to the heap allocation that contains T. Its Clone implementation increments the reference count and its Drop implementation decrements the reference count.

Various other improvements;