Skip to main content

openvino/
layout.rs

1use crate::{cstr, drop_using_function, try_unsafe, util::Result};
2use openvino_sys::{ov_layout_create, ov_layout_free, ov_layout_t};
3
4/// See [`ov_layout_t`](https://docs.openvino.ai/2024/api/c_cpp_api/group__ov__layout__c__api.html).
5pub struct Layout {
6    ptr: *mut ov_layout_t,
7}
8drop_using_function!(Layout, ov_layout_free);
9
10impl Layout {
11    /// Get a pointer to the [`ov_layout_t`].
12    #[inline]
13    pub(crate) fn as_mut_ptr(&mut self) -> *mut ov_layout_t {
14        self.ptr
15    }
16
17    /// Creates a new layout with the given description.
18    pub fn new(layout_desc: &str) -> Result<Self> {
19        let layout_desc = cstr!(layout_desc);
20        let mut layout = std::ptr::null_mut();
21        try_unsafe!(ov_layout_create(
22            layout_desc.as_ptr(),
23            std::ptr::addr_of_mut!(layout)
24        ))?;
25        Ok(Self { ptr: layout })
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use crate::LoadingError;
32
33    use super::*;
34
35    #[test]
36    fn test_new_layout() {
37        openvino_sys::library::load()
38            .map_err(LoadingError::SystemFailure)
39            .unwrap();
40        let layout_desc = "NCHW";
41        let layout = Layout::new(layout_desc).unwrap();
42        assert!(!layout.ptr.is_null());
43    }
44}