1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::ffi::CStr;

mod sys {
    #![allow(non_upper_case_globals)]
    #![allow(non_camel_case_types)]
    #![allow(non_snake_case)]
    include!(concat!(env!("OUT_DIR"), "/mnn_c.rs"));
}
pub use sys::*;
impl DimensionType {
    pub const NHWC: Self = Self::TENSORFLOW;
    pub const NCHW: Self = Self::CAFFE;
    pub const NC4HW4: Self = Self::CAFFE_C4;
}
impl halide_type_t {
    unsafe fn new(code: halide_type_code_t, bits: u8, lanes: u16) -> Self {
        Self { code, bits, lanes }
    }
}

pub fn halide_type_of<T: HalideType>() -> halide_type_t {
    T::halide_type_of()
}

pub trait HalideType: seal::Sealed {
    fn halide_type_of() -> halide_type_t;
}
mod seal {
    pub trait Sealed {}
}

macro_rules! halide_types {
    ($($t:ty => $ht:expr),*) => {
        $(
            impl seal::Sealed for $t {}
            impl HalideType for $t {
                fn halide_type_of() -> halide_type_t {
                    unsafe {
                        $ht
                    }
                }
            }
        )*
    };
}

halide_types! {
    f32 =>  halide_type_t::new(halide_type_code_t::halide_type_float, 32, 1),
    f64 =>  halide_type_t::new(halide_type_code_t::halide_type_float, 64, 1),
    bool => halide_type_t::new(halide_type_code_t::halide_type_uint, 1, 1),
    u8 =>   halide_type_t::new(halide_type_code_t::halide_type_uint, 8,1),
    u16 =>  halide_type_t::new(halide_type_code_t::halide_type_uint, 16,1),
    u32 =>  halide_type_t::new(halide_type_code_t::halide_type_uint, 32,1),
    u64 =>  halide_type_t::new(halide_type_code_t::halide_type_uint, 64,1),
    i8 =>   halide_type_t::new(halide_type_code_t::halide_type_int, 8,1),
    i16 =>  halide_type_t::new(halide_type_code_t::halide_type_int, 16,1),
    i32 =>  halide_type_t::new(halide_type_code_t::halide_type_int, 32,1),
    i64 =>  halide_type_t::new(halide_type_code_t::halide_type_int, 64,1)
}

impl Drop for CString {
    fn drop(&mut self) {
        unsafe { destroyCString(self.as_ptr_mut()) }
    }
}

impl CString {
    pub fn as_ptr(&self) -> *const CString {
        core::ptr::addr_of!(*self)
    }

    pub fn as_ptr_mut(&mut self) -> *mut CString {
        core::ptr::addr_of_mut!(*self)
    }
    /// # Safety
    /// This function is unsafe because it dereferences a raw pointer.
    pub unsafe fn to_cstr(&self) -> &CStr {
        unsafe { std::ffi::CStr::from_ptr(self.data) }
    }
}

impl AsRef<[i32]> for TensorShape {
    fn as_ref(&self) -> &[i32] {
        &self.shape[..self.size]
    }
}

impl halide_type_code_t {
    /// # Safety
    /// This function is unsafe because this basically truansmutes an integer to an enum.
    /// And if the enum is not valid, it will cause undefined behavior in rust.
    pub unsafe fn from_u32(code: u32) -> Self {
        unsafe { std::mem::transmute(code) }
    }
}