use candle_core::Error as CandleError;
#[cfg(target_os = "macos")]
use objc2::rc::{autoreleasepool, Retained};
#[cfg(target_os = "macos")]
use objc2_core_ml::{MLModel, MLState};
#[cfg(target_os = "macos")]
pub struct CoreMLState {
inner: Retained<MLState>,
}
#[cfg(not(target_os = "macos"))]
pub struct CoreMLState {
_phantom: std::marker::PhantomData<()>,
}
impl std::fmt::Debug for CoreMLState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CoreMLState").finish_non_exhaustive()
}
}
#[cfg(target_os = "macos")]
impl CoreMLState {
pub(crate) fn new(model: &Retained<MLModel>) -> Result<Self, CandleError> {
autoreleasepool(|_| {
let state = unsafe { model.newState() };
Ok(CoreMLState { inner: state })
})
}
pub(crate) fn inner(&self) -> &MLState {
&self.inner
}
}
#[cfg(not(target_os = "macos"))]
impl CoreMLState {
pub(crate) fn new(_model: &()) -> Result<Self, CandleError> {
Err(CandleError::Msg(
"CoreML state is only available on macOS".to_string(),
))
}
}