arrayfire 3.3.2

ArrayFire is a high performance software library for parallel computing with an easy-to-use API. Its array based function set makes parallel programming simple. ArrayFire's multiple backends (CUDA, OpenCL and native CPU) make it platform independent and highly portable. A few lines of code in ArrayFire can replace dozens of lines of parallel computing code, saving you valuable time and lowering development costs. This crate provides Rust bindings for ArrayFire library.
extern crate num;

use defines::{AfError, ColorMap, ConvDomain, ConvMode, DType, InterpType, MatProp, MatchType};
use std::mem;
use self::num::Complex;

impl From<i32> for AfError {
    fn from(t: i32) -> AfError {
        assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32);
        unsafe { mem::transmute(t) }
    }
}

impl From<i32> for DType {
    fn from(t: i32) -> DType {
        assert!(DType::F32 as i32 <= t && t <= DType::U64 as i32);
        unsafe { mem::transmute(t) }
    }
}

impl From<i32> for InterpType {
    fn from(t: i32) -> InterpType {
        assert!(InterpType::NEAREST as i32 <= t && t <= InterpType::CUBIC as i32);
        unsafe { mem::transmute(t) }
    }
}

impl From<i32> for ConvMode {
    fn from(t: i32) -> ConvMode {
        assert!(ConvMode::DEFAULT as i32 <= t && t <= ConvMode::EXPAND as i32);
        unsafe { mem::transmute(t) }
    }
}

impl From<i32> for ConvDomain {
    fn from(t: i32) -> ConvDomain {
        assert!(ConvDomain::AUTO as i32 <= t && t <= ConvDomain::FREQUENCY as i32);
        unsafe { mem::transmute(t) }
    }
}

impl From<i32> for MatchType {
    fn from(t: i32) -> MatchType {
        assert!(MatchType::SAD as i32 <= t && t <= MatchType::SHD as i32);
        unsafe { mem::transmute(t) }
    }
}

pub fn to_u32(t: MatProp) -> u32 {
    match t {
        MatProp::NONE       =>  0,
        MatProp::TRANS      =>  1,
        MatProp::CTRANS     =>  2,
        MatProp::UPPER      =>  32,
        MatProp::LOWER      =>  64,
        MatProp::DIAGUNIT  =>  128,
        MatProp::SYM        =>  512,
        MatProp::POSDEF     =>  1024,
        MatProp::ORTHOG     =>  2048,
        MatProp::TRIDIAG   =>  4096,
        MatProp::BLOCKDIAG =>  8192,
    }
}

impl From<i32> for ColorMap {
    fn from(t: i32) -> ColorMap {
        assert!(ColorMap::DEFAULT as i32 <= t && t <= ColorMap::BLUE as i32);
        unsafe { mem::transmute(t) }
    }
}

/// Types of the data that can be generated using ArrayFire data generation functions.
///
/// The trait HasAfEnum has been defined internally for the following types. We strongly suggest
/// not to implement this trait in your program for user defined types because ArrayFire functions
/// will only work for the following data types currently. Any such trait implementation for types
/// other than the ones listed below will result in undefined behavior.
///
/// - f32
/// - f64
/// - num::Complex\<f32\>
/// - num::Complex\<f64\>
/// - bool
/// - i32
/// - u32
/// - u8
/// - i64
/// - u64
/// - i16
/// - u16
///
pub trait HasAfEnum {
    fn get_af_dtype() -> DType;
}

macro_rules! impl_has_af_enum {
    ($rust_t: ty, $af_dtype: expr) => (
        impl HasAfEnum for $rust_t {
            fn get_af_dtype() -> DType {
                $af_dtype
            }
        }
    )
}

impl_has_af_enum!(f32, DType::F32);
impl_has_af_enum!(Complex<f32>, DType::C32);
impl_has_af_enum!(f64, DType::F64);
impl_has_af_enum!(Complex<f64>, DType::C64);
// FIXME: Rust bool may become incompatible in memory layout with C-ABI
// Currently, it is of size 1-byte
impl_has_af_enum!(bool, DType::B8);
impl_has_af_enum!(i32, DType::S32);
impl_has_af_enum!(u32, DType::U32);
impl_has_af_enum!(u8, DType::U8);
impl_has_af_enum!(i64, DType::S64);
impl_has_af_enum!(u64, DType::U64);
impl_has_af_enum!(i16, DType::S16);
impl_has_af_enum!(u16, DType::U16);