|
1 | 1 | #![doc(html_logo_url = "https://raw.githubusercontent.com/pykeio/ort/v2/docs/icon.png")]
|
2 | 2 | #![cfg_attr(docsrs, feature(doc_cfg))]
|
3 | 3 | #![allow(clippy::tabs_in_doc_comments, clippy::arc_with_non_send_sync)]
|
| 4 | +#![allow(clippy::macro_metavars_in_unsafe)] |
4 | 5 | #![warn(clippy::unwrap_used)]
|
5 | 6 |
|
6 | 7 | //! <div align=center>
|
@@ -165,23 +166,23 @@ pub fn info() -> &'static str {
|
165 | 166 | /// ```
|
166 | 167 | /// # use std::ffi::CStr;
|
167 | 168 | /// # fn main() -> ort::Result<()> {
|
168 |
| -/// let api = ort::api().as_ptr(); |
169 |
| -/// let build_info = unsafe { CStr::from_ptr((*api).GetBuildInfoString.unwrap()()) }; |
| 169 | +/// let api = ort::api(); |
| 170 | +/// let build_info = unsafe { CStr::from_ptr(api.GetBuildInfoString.unwrap()()) }; |
170 | 171 | /// println!("{}", build_info.to_string_lossy());
|
171 | 172 | /// // ORT Build Info: git-branch=HEAD, git-commit-id=4573740, build type=Release, cmake cxx flags: /DWIN32 /D_WINDOWS /EHsc /EHsc /wd26812 -DEIGEN_HAS_C99_MATH -DCPUINFO_SUPPORTED
|
172 | 173 | /// # Ok(())
|
173 | 174 | /// # }
|
174 | 175 | /// ```
|
175 | 176 | ///
|
176 | 177 | /// For the full list of ONNX Runtime APIs, consult the [`ort_sys::OrtApi`] struct and the [ONNX Runtime C API](https://onnxruntime.ai/docs/api/c/struct_ort_api.html).
|
177 |
| -pub fn api() -> NonNull<ort_sys::OrtApi> { |
| 178 | +pub fn api() -> &'static ort_sys::OrtApi { |
178 | 179 | struct ApiPointer(NonNull<ort_sys::OrtApi>);
|
179 | 180 | unsafe impl Send for ApiPointer {}
|
180 | 181 | unsafe impl Sync for ApiPointer {}
|
181 | 182 |
|
182 | 183 | static G_ORT_API: OnceLock<ApiPointer> = OnceLock::new();
|
183 | 184 |
|
184 |
| - G_ORT_API |
| 185 | + let ptr = G_ORT_API |
185 | 186 | .get_or_init(|| {
|
186 | 187 | #[cfg(feature = "load-dynamic")]
|
187 | 188 | unsafe {
|
@@ -227,55 +228,52 @@ pub fn api() -> NonNull<ort_sys::OrtApi> {
|
227 | 228 | ApiPointer(NonNull::new(api.cast_mut()).expect("Failed to initialize ORT API"))
|
228 | 229 | }
|
229 | 230 | })
|
230 |
| - .0 |
| 231 | + .0; |
| 232 | + unsafe { ptr.as_ref() } |
231 | 233 | }
|
232 | 234 |
|
| 235 | +#[macro_export] |
233 | 236 | macro_rules! ortsys {
|
234 | 237 | ($method:ident) => {
|
235 |
| - $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null"))) |
236 |
| - }; |
237 |
| - (unsafe $method:ident) => { |
238 |
| - unsafe { $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null"))) } |
| 238 | + $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null"))) |
239 | 239 | };
|
240 | 240 | ($method:ident($($n:expr),+ $(,)?)) => {
|
241 |
| - $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) |
| 241 | + $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) |
242 | 242 | };
|
243 | 243 | (unsafe $method:ident($($n:expr),+ $(,)?)) => {
|
244 |
| - unsafe { $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) } |
| 244 | + unsafe { $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) } |
245 | 245 | };
|
246 | 246 | ($method:ident($($n:expr),+ $(,)?).expect($e:expr)) => {
|
247 |
| - $crate::error::status_to_result($crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+)).expect($e) |
| 247 | + $crate::error::status_to_result($crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+)).expect($e) |
248 | 248 | };
|
249 | 249 | (unsafe $method:ident($($n:expr),+ $(,)?).expect($e:expr)) => {
|
250 |
| - $crate::error::status_to_result(unsafe { $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) }).expect($e) |
| 250 | + $crate::error::status_to_result(unsafe { $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) }).expect($e) |
251 | 251 | };
|
252 | 252 | ($method:ident($($n:expr),+ $(,)?); nonNull($($check:expr),+ $(,)?)$(;)?) => {
|
253 |
| - $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+); |
| 253 | + $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+); |
254 | 254 | $($crate::error::assert_non_null_pointer($check, stringify!($method))?;)+
|
255 | 255 | };
|
256 | 256 | (unsafe $method:ident($($n:expr),+ $(,)?); nonNull($($check:expr),+ $(,)?)$(;)?) => {{
|
257 |
| - let _x = unsafe { $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) }; |
| 257 | + let _x = unsafe { $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) }; |
258 | 258 | $($crate::error::assert_non_null_pointer($check, stringify!($method)).unwrap();)+
|
259 | 259 | _x
|
260 | 260 | }};
|
261 | 261 | ($method:ident($($n:expr),+ $(,)?)?) => {
|
262 |
| - $crate::error::status_to_result($crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+))?; |
| 262 | + $crate::error::status_to_result($crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+))?; |
263 | 263 | };
|
264 | 264 | (unsafe $method:ident($($n:expr),+ $(,)?)?) => {
|
265 |
| - $crate::error::status_to_result(unsafe { $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) })?; |
| 265 | + $crate::error::status_to_result(unsafe { $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) })?; |
266 | 266 | };
|
267 | 267 | ($method:ident($($n:expr),+ $(,)?)?; nonNull($($check:expr),+ $(,)?)$(;)?) => {
|
268 |
| - $crate::error::status_to_result($crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+))?; |
| 268 | + $crate::error::status_to_result($crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+))?; |
269 | 269 | $($crate::error::assert_non_null_pointer($check, stringify!($method))?;)+
|
270 | 270 | };
|
271 | 271 | (unsafe $method:ident($($n:expr),+ $(,)?)?; nonNull($($check:expr),+ $(,)?)$(;)?) => {{
|
272 |
| - $crate::error::status_to_result(unsafe { $crate::api().as_ref().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) })?; |
| 272 | + $crate::error::status_to_result(unsafe { $crate::api().$method.unwrap_or_else(|| unreachable!(concat!("Method `", stringify!($method), "` is null")))($($n),+) })?; |
273 | 273 | $($crate::error::assert_non_null_pointer($check, stringify!($method))?;)+
|
274 | 274 | }};
|
275 | 275 | }
|
276 | 276 |
|
277 |
| -pub(crate) use ortsys; |
278 |
| - |
279 | 277 | pub(crate) fn char_p_to_string(raw: *const c_char) -> Result<String> {
|
280 | 278 | if raw.is_null() {
|
281 | 279 | return Ok(String::new());
|
|
0 commit comments