use std::ptr::NonNull;
use litert_sys as sys;
use crate::{check, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
pub struct Accelerators(sys::LiteRtHwAcceleratorSet);
impl Accelerators {
pub const NONE: Self = Self(sys::kLiteRtHwAcceleratorNone as _);
pub const CPU: Self = Self(sys::kLiteRtHwAcceleratorCpu as _);
pub const GPU: Self = Self(sys::kLiteRtHwAcceleratorGpu as _);
pub const NPU: Self = Self(sys::kLiteRtHwAcceleratorNpu as _);
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
#[must_use]
pub const fn bits(self) -> sys::LiteRtHwAcceleratorSet {
self.0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0 && other.0 != 0
}
}
impl std::ops::BitOr for Accelerators {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
self.union(rhs)
}
}
impl Default for Accelerators {
fn default() -> Self {
Self::CPU
}
}
pub struct CompilationOptions {
ptr: NonNull<sys::LiteRtOptionsT>,
}
impl std::fmt::Debug for CompilationOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompilationOptions")
.field("ptr", &self.ptr.as_ptr())
.finish()
}
}
impl CompilationOptions {
pub fn new() -> Result<Self> {
let mut raw: sys::LiteRtOptions = std::ptr::null_mut();
check(unsafe { sys::LiteRtCreateOptions(&mut raw) })?;
let ptr = NonNull::new(raw).ok_or(crate::Error::NullPointer)?;
let mut this = Self { ptr };
this.set_accelerators(Accelerators::CPU)?;
Ok(this)
}
pub fn set_accelerators(&mut self, accelerators: Accelerators) -> Result<()> {
check(unsafe {
sys::LiteRtSetOptionsHardwareAccelerators(self.ptr.as_ptr(), accelerators.bits())
})
}
pub fn with_accelerators(mut self, accelerators: Accelerators) -> Result<Self> {
self.set_accelerators(accelerators)?;
Ok(self)
}
pub(crate) fn as_raw(&self) -> sys::LiteRtOptions {
self.ptr.as_ptr()
}
}
impl Drop for CompilationOptions {
fn drop(&mut self) {
unsafe { sys::LiteRtDestroyOptions(self.ptr.as_ptr()) }
}
}
unsafe impl Send for CompilationOptions {}