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
use crate::error::check;
use crate::Error;
use crate::VariableProfileTable;
use std::ffi::CString;
use std::path::Path;
use std::ptr;
pub struct ModelData {
pub(crate) handle: menoh_sys::menoh_model_data_handle,
}
impl ModelData {
pub fn from_onnx<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let path = CString::new::<&str>(&path.as_ref().to_string_lossy())?;
let mut handle = ptr::null_mut();
unsafe {
check(menoh_sys::menoh_make_model_data_from_onnx(
path.as_ptr(),
&mut handle,
))?
};
Ok(Self { handle })
}
pub fn from_onnx_bytes(data: &[u8]) -> Result<Self, Error> {
let mut handle = ptr::null_mut();
unsafe {
check(menoh_sys::menoh_make_model_data_from_onnx_data_on_memory(
data.as_ptr(),
data.len() as _,
&mut handle,
))?
};
Ok(Self { handle })
}
pub fn optimize(&mut self, variable_profile_table: &VariableProfileTable) -> Result<(), Error> {
unsafe {
check(menoh_sys::menoh_model_data_optimize(
self.handle,
variable_profile_table.handle,
))
}
}
}
impl Drop for ModelData {
fn drop(&mut self) {
unsafe { menoh_sys::menoh_delete_model_data(self.handle) }
}
}
#[cfg(test)]
mod tests;