Skip to main content

mnn_sys/
lib.rs

1use std::ffi::CStr;
2mod tracing;
3
4pub mod cpp {
5    #![allow(non_upper_case_globals)]
6    #![allow(non_camel_case_types)]
7    #![allow(non_snake_case)]
8    include!(concat!(env!("OUT_DIR"), "/mnn_cpp.rs"));
9}
10mod sys {
11    #![allow(non_upper_case_globals)]
12    #![allow(non_camel_case_types)]
13    #![allow(non_snake_case)]
14    #![allow(clippy::manual_c_str_literals)]
15    #![allow(clippy::suspicious_doc_comments)]
16    include!(concat!(env!("OUT_DIR"), "/mnn_c.rs"));
17}
18pub use sys::*;
19impl DimensionType {
20    pub const NHWC: Self = Self::TENSORFLOW;
21    pub const NCHW: Self = Self::CAFFE;
22    pub const NC4HW4: Self = Self::CAFFE_C4;
23}
24impl halide_type_t {
25    unsafe fn new(code: halide_type_code_t, bits: u8, lanes: u16) -> Self {
26        Self { code, bits, lanes }
27    }
28}
29
30pub fn halide_type_of<T: HalideType>() -> halide_type_t {
31    T::halide_type_of()
32}
33
34pub trait HalideType: seal::Sealed + MaybeDebug {
35    fn halide_type_of() -> halide_type_t;
36}
37
38// pub trait MaybeDebug {}
39// impl<T> MaybeDebug for T {}
40
41pub trait MaybeDebug: core::fmt::Debug {}
42impl<T: core::fmt::Debug> MaybeDebug for T {}
43
44mod seal {
45    pub trait Sealed {}
46}
47
48macro_rules! halide_types {
49    ($($t:ty => $ht:expr),*) => {
50        $(
51            impl seal::Sealed for $t {}
52            impl HalideType for $t {
53                fn halide_type_of() -> halide_type_t {
54                    unsafe {
55                        $ht
56                    }
57                }
58            }
59        )*
60    };
61}
62
63halide_types! {
64    f32 =>  halide_type_t::new(halide_type_code_t::halide_type_float, 32, 1),
65    f64 =>  halide_type_t::new(halide_type_code_t::halide_type_float, 64, 1),
66    bool => halide_type_t::new(halide_type_code_t::halide_type_uint, 1, 1),
67    u8 =>   halide_type_t::new(halide_type_code_t::halide_type_uint, 8,1),
68    u16 =>  halide_type_t::new(halide_type_code_t::halide_type_uint, 16,1),
69    u32 =>  halide_type_t::new(halide_type_code_t::halide_type_uint, 32,1),
70    u64 =>  halide_type_t::new(halide_type_code_t::halide_type_uint, 64,1),
71    i8 =>   halide_type_t::new(halide_type_code_t::halide_type_int, 8,1),
72    i16 =>  halide_type_t::new(halide_type_code_t::halide_type_int, 16,1),
73    i32 =>  halide_type_t::new(halide_type_code_t::halide_type_int, 32,1),
74    i64 =>  halide_type_t::new(halide_type_code_t::halide_type_int, 64,1)
75}
76
77impl Drop for CString {
78    fn drop(&mut self) {
79        unsafe { destroyCString(self.as_ptr_mut()) }
80    }
81}
82
83impl CString {
84    pub fn as_ptr(&self) -> *const CString {
85        core::ptr::addr_of!(*self)
86    }
87
88    pub fn as_ptr_mut(&mut self) -> *mut CString {
89        core::ptr::addr_of_mut!(*self)
90    }
91    /// # Safety
92    /// This function is unsafe because it dereferences a raw pointer.
93    pub unsafe fn to_cstr(&self) -> &CStr {
94        unsafe { std::ffi::CStr::from_ptr(self.data) }
95    }
96}
97
98impl AsRef<[i32]> for TensorShape {
99    fn as_ref(&self) -> &[i32] {
100        &self.shape[..self.size]
101    }
102}
103
104impl halide_type_code_t {
105    /// # Safety
106    /// This function is unsafe because this basically truansmutes an integer to an enum.
107    /// And if the enum is not valid, it will cause undefined behavior in rust.
108    pub unsafe fn from_u32(code: u32) -> Self {
109        unsafe { std::mem::transmute(code) }
110    }
111}