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) }
}
}
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);
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);