use executorch_sys as sys;
use crate::util::IntoRust;
pub type DeviceIndex = i8;
#[repr(i8)]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum DeviceType {
Cpu = sys::ET_DeviceType::ET_DeviceType_CPU as i8,
Cuda = sys::ET_DeviceType::ET_DeviceType_CUDA as i8,
}
impl IntoRust for sys::ET_DeviceType {
type RsType = DeviceType;
fn rs(self) -> DeviceType {
match self {
sys::ET_DeviceType::ET_DeviceType_CPU => DeviceType::Cpu,
sys::ET_DeviceType::ET_DeviceType_CUDA => DeviceType::Cuda,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Device {
type_: DeviceType,
index: DeviceIndex,
}
impl Device {
pub fn new(type_: DeviceType, index: DeviceIndex) -> Self {
Self { type_, index }
}
pub fn type_(&self) -> DeviceType {
self.type_
}
pub fn index(&self) -> DeviceIndex {
self.index
}
pub fn is_cpu(&self) -> bool {
self.type_ == DeviceType::Cpu
}
}
impl IntoRust for sys::ET_Device {
type RsType = Device;
fn rs(self) -> Device {
Device {
type_: self.type_.rs(),
index: self.index,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cpu_device_roundtrip() {
let dev = Device::new(DeviceType::Cpu, 0);
assert_eq!(dev.type_(), DeviceType::Cpu);
assert_eq!(dev.index(), 0);
assert!(dev.is_cpu());
}
}