#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChipInfo {
pub brand: String,
pub model: String,
pub os_version: String,
pub ane: bool,
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod sys {
use std::os::raw::{c_char, c_int};
unsafe extern "C" {
pub fn rlx_coreml_ane_available() -> c_int;
pub fn rlx_coreml_chip_brand(buf: *mut c_char, len: c_int);
pub fn rlx_coreml_chip_model(buf: *mut c_char, len: c_int);
pub fn rlx_coreml_os_version(buf: *mut c_char, len: c_int);
}
pub fn fill_string(f: unsafe extern "C" fn(*mut c_char, c_int)) -> String {
let mut buf = vec![0u8; 256];
unsafe { f(buf.as_mut_ptr() as *mut c_char, buf.len() as c_int) };
let end = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
buf.truncate(end);
String::from_utf8_lossy(&buf).into_owned()
}
}
pub fn is_available() -> bool {
cfg!(any(target_os = "macos", target_os = "ios"))
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub fn ane_available() -> bool {
unsafe { sys::rlx_coreml_ane_available() != 0 }
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
pub fn ane_available() -> bool {
false
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub fn chip_info() -> ChipInfo {
ChipInfo {
brand: sys::fill_string(sys::rlx_coreml_chip_brand),
model: sys::fill_string(sys::rlx_coreml_chip_model),
os_version: sys::fill_string(sys::rlx_coreml_os_version),
ane: ane_available(),
}
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
pub fn chip_info() -> ChipInfo {
ChipInfo {
brand: "unknown".into(),
model: "unknown".into(),
os_version: "unknown".into(),
ane: false,
}
}