dx_core 0.3.0

Core functionality required by all the dx-rs crates. dx-rs is a DirectX wrapper for rust.
Documentation
/*
 * Copyright 2015 Joshua R. Rodgers
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#![macro_use]

use unknown::IUnknown;
use std::ops::Deref;

pub struct ComRef<T> where T: IUnknown {
    _ref: *const T,
}

impl<T: IUnknown> ComRef<T> {
    pub fn new(value: *const T) -> ComRef<T> {
        ComRef { _ref: value }
    }
}

impl<T: IUnknown> Deref for ComRef<T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &T {
        unsafe { &(*self._ref) }
    }
}

impl<T: IUnknown> Drop for ComRef<T> {
    fn drop(&mut self) {
        if self._ref.is_null() { return; }

        self.release();
    }
}

impl<T: IUnknown> Clone for ComRef<T> {
    #[inline]
    fn clone(&self) -> ComRef<T> {
        self.add_ref();
        ComRef::new(self._ref)
    }
}

impl<T: IUnknown> ::std::borrow::Borrow<T> for ComRef<T> {
    fn borrow(&self) -> &T {
        &*self
    }
}

unsafe impl<T: IUnknown> Send for ComRef<T> {}