components 0.1.0

Library for interacting with COM (Component Object Model) on Windows.
Documentation
use iunknown::ComInterface;
use std::ops::Deref;
use std::ptr;

pub struct ComPtr<T: ComInterface> {
    instance: *const T,
}

impl<T: ComInterface> ComPtr<T> {
    pub unsafe fn from_raw(instance: *const T) -> ComPtr<T> {
        ComPtr { instance: instance }
    }
}

impl<T: ComInterface> Drop for ComPtr<T> {
    fn drop(&mut self) {
        let temp = self.instance;
        if !self.instance.is_null() {
            self.instance = ptr::null();
            unsafe {
                let unk = (&*temp).iunknown();
                unk.release();
            }
        }
    }
}

impl<T: ComInterface> Deref for ComPtr<T> {
    type Target = T;

    fn deref(&self) -> &T {
        unsafe { &*self.instance }
    }
}

impl<T: ComInterface> Clone for ComPtr<T> {
    fn clone(&self) -> Self {
        let unk = self.iunknown();
        unsafe {
            unk.add_ref();
        }

        ComPtr {
            instance: self.instance,
        }
    }
}

unsafe impl<T: ComInterface> Send for ComPtr<T> {}
unsafe impl<T: ComInterface> Sync for ComPtr<T> {}