Git Product home page Git Product logo

com-impl-rs's Introduction

Implement COM interfaces in Rust

DEPRECATED: this crate has been superseded by com-rs, which is supported by Microsoft.

This crate provides a procedural macro which helps with implementing COM interfaces in Rust.

It is currently able to implement interfaces with single-inheritance COM hierarchies, and handles constructing the vtable automatically.

Note: if you only want to use COM in Rust, you can simply use winapi. This crate is for implementing an interface's methods from within Rust.

Requires Rust nightly, for now!

Usage

The following example shows how this crate can be used.

At the crate level

You must add winapi as a dependency to your crate, and at the very least enable the winerror and unknwnbase features.

[dependencies.winapi]
version = "0.3"
features = ["winerror", "unknwnbase"]

For every interface you want to implement

You must manually import the interfaces you are implementing and their vtables.

use winapi::shared::dxgi::{IDXGIObject, IDXGIObjectVtbl};
use winapi::um::unknwnbase::{IUnknown, IUnknownVtbl};
use some::other::{Interface, InterfaceVtbl};

It is very important for the vtable to be correctly defined, otherwise external code using your interface could misbehave. Use the RIDL! macro from winapi for maximum compatibility.

Then you need to import the procedural macros and the ComInterface trait exported by this crate.

use com_impl::{ComInterface, interface, implementation};

Define your structure. You must specifiy the final interface you want to implement.

#[interface(IDXGIFactory)]
struct MyInterface {}

For each interface in the inheritance chain, you must have a new implementation.

// The custom attribute's parameter is the interface you are implementing.
// In this case `IUnknown`.
#[implementation(IUnknown)]
impl MyInterface {
    // COM functions follow the PascalCase calling convention.
    // You implement a PascalCase function by using the snake_case name.

    // For example, this one implements `QueryInterface`.
    // Note: the macro automatically adds `unsafe extern "system"` to the function definition.
    fn query_interface(&self) -> HRESULT { /* ... */ }
    fn add_ref(&mut self) -> ULONG { /* ... */ }
    fn release(&mut self) -> ULONG { /* ... */ }
}

// Now we implement IDXGIObject.
#[implementation(IDXGIObject)]
impl MyInterface {
    fn get_parent(&mut self, riid: REFIID, parent: *mut c_void) -> HRESULT { /* ... */ }

    // ... Implement the other methods here ...
}

If we had specified NextInterface instead of IDXGIObject when defining the struct, we could continue the implementation chain here.

/// `NextInterface` is implemented here.
#[implementation(NextInterface)]
impl MyInterface {
    // ... New functions added by NextInterface ...
}

To implement the constructor for your type, use the generated Self::create_vtable function to fill in the generated __vtable field.

impl MyInterface {
    // This is an example constructor.
    fn new() -> Self {
        Self {
            __vtable: Box::new(Self::create_vtable()),
            /* other fields */
        }
    }
}

Check out the tests directory for more examples.

FAQ

  • Q: Does it auto-implement IUnknown's methods?

    A: No. See the test directory for an example on how to do this manually. Also see the various blog posts giving practical advice on implementing COM stuff.

  • Q: Is it safe?

    A: Not very safe. Rust proc macros are limited in their access to the type system. We don't exactly have a full reflection system, so you must make sure your method implementation match the method signatures.

  • Q: How to handle memory management?

    A: You have to implement an internal atomic reference counter yourself, and then remember to allocate any COM objects on the heap, to be able to easily share references to them.

    See the test code for an idea on how to use Box.

Issues

  • Even if your struct is empty, you must still declare it with brackets: struct Something { }

  • Structs with unnamed fields (e.g. struct Example(u32, i32);) are not supported.

  • A struct can only implement one interface hierarchy. You cannot have a single object implementing multiple disjoint interfaces.

License

This code is licensed under the Mozilla Public License version 2.0.

com-impl-rs's People

Contributors

gabrielmajeri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

com-impl-rs's Issues

Better error reporting

There are many places in the code where either

  • Rust will panic with an unhelpful error message or

  • the error message will be somewhat helpful, but the error span is all wrong

Type check the functions

Currently we perform absolutely no checks that the implemented method matches the COM interface's declaration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.