ort2 0.1.2

onnxruntime wrapper c/c++ api
Documentation
use std::{
    ffi::{CStr, CString},
    ptr::{null, null_mut},
};

use ort2_sys::{self as ffi, OrtAllocatorType, OrtMemType};

use crate::{api::ok, error::Result};

pub struct MemoryInfo {
    inner: *mut ffi::OrtMemoryInfo,
}

impl MemoryInfo {
    pub fn new(
        name: impl AsRef<str>,
        typ: OrtAllocatorType,
        id: i32,
        mem_typ: OrtMemType,
    ) -> Result<Self> {
        let name = CString::new(name.as_ref())?;
        let mut inner = null_mut();
        ok!(
            CreateMemoryInfo,
            name.as_ptr(),
            typ,
            id,
            mem_typ,
            &mut inner
        )?;
        Ok(MemoryInfo { inner })
    }

    pub fn new_cpu(typ: OrtAllocatorType, mem_typ: OrtMemType) -> Result<Self> {
        let mut inner = null_mut();
        ok!(CreateCpuMemoryInfo, typ, mem_typ, &mut inner)?;
        Ok(Self { inner })
    }

    pub fn inner(&self) -> *mut ffi::OrtMemoryInfo {
        self.inner
    }

    pub fn mem_typ(&self) -> Result<ffi::OrtMemType> {
        let mut typ = OrtMemType::OrtMemTypeDefault;
        ok!(MemoryInfoGetMemType, self.inner, &mut typ)?;
        Ok(typ)
    }

    pub fn name(&self) -> Result<&CStr> {
        let mut name = null();
        ok!(MemoryInfoGetName, self.inner, &mut name)?;
        Ok(unsafe { CStr::from_ptr(name) })
    }

    pub fn typ(&self) -> Result<OrtAllocatorType> {
        let mut typ = OrtAllocatorType::OrtInvalidAllocator;
        ok!(MemoryInfoGetType, self.inner, &mut typ)?;
        Ok(typ)
    }

    pub fn id(&self) -> Result<i32> {
        let mut id = 0i32;
        ok!(MemoryInfoGetId, self.inner, &mut id)?;
        Ok(id)
    }
}

impl Default for MemoryInfo {
    fn default() -> Self {
        Self::new_cpu(
            OrtAllocatorType::OrtArenaAllocator,
            OrtMemType::OrtMemTypeDefault,
        )
        .expect("failed to create memory info")
    }
}