|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::convert::Infallible; |
| 3 | +use std::ffi::CString; |
| 4 | +use std::fmt::{Display, Formatter}; |
| 5 | +use std::str::FromStr; |
| 6 | + |
| 7 | +/// `DeviceType` represents accelerator devices. |
| 8 | +#[derive(Ord, PartialOrd, Eq, PartialEq, Hash, Debug)] |
| 9 | +pub enum DeviceType<'a> { |
| 10 | + /// [CPU Device](https://docs.openvino.ai/2024/openvino-workflow/running-inference/inference-devices-and-modes/cpu-device.html) |
| 11 | + CPU, |
| 12 | + /// [GPU Device](https://docs.openvino.ai/2024/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device.html) |
| 13 | + GPU, |
| 14 | + /// [NPU Device](https://docs.openvino.ai/2024/openvino-workflow/running-inference/inference-devices-and-modes/npu-device.html) |
| 15 | + NPU, |
| 16 | + /// [GNA Device](https://docs.openvino.ai/2023.3/openvino_docs_OV_UG_supported_plugins_GNA.html) |
| 17 | + #[deprecated = "Deprecated since OpenVINO 2024.0; use NPU device instead"] |
| 18 | + GNA, |
| 19 | + /// Arbitrary device. |
| 20 | + Other(Cow<'a, str>), |
| 21 | +} |
| 22 | + |
| 23 | +impl DeviceType<'_> { |
| 24 | + /// Creates a device type with owned string data. |
| 25 | + pub fn to_owned(&self) -> DeviceType<'static> { |
| 26 | + match self { |
| 27 | + DeviceType::CPU => DeviceType::CPU, |
| 28 | + DeviceType::GPU => DeviceType::GPU, |
| 29 | + DeviceType::NPU => DeviceType::NPU, |
| 30 | + #[allow(deprecated)] |
| 31 | + DeviceType::GNA => DeviceType::GNA, |
| 32 | + DeviceType::Other(s) => DeviceType::Other(Cow::Owned(s.clone().into_owned())), |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl AsRef<str> for DeviceType<'_> { |
| 38 | + fn as_ref(&self) -> &str { |
| 39 | + match self { |
| 40 | + DeviceType::CPU => "CPU", |
| 41 | + DeviceType::GPU => "GPU", |
| 42 | + DeviceType::NPU => "NPU", |
| 43 | + #[allow(deprecated)] |
| 44 | + DeviceType::GNA => "GNA", |
| 45 | + DeviceType::Other(s) => s, |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<'a> From<&'a DeviceType<'a>> for &'a str { |
| 51 | + fn from(value: &'a DeviceType) -> Self { |
| 52 | + value.as_ref() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<'a> From<DeviceType<'a>> for CString { |
| 57 | + fn from(value: DeviceType) -> Self { |
| 58 | + CString::new(value.as_ref()).expect("a valid C string") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<'a> From<&'a str> for DeviceType<'a> { |
| 63 | + fn from(s: &'a str) -> Self { |
| 64 | + match s { |
| 65 | + "CPU" => DeviceType::CPU, |
| 66 | + "GPU" => DeviceType::GPU, |
| 67 | + "NPU" => DeviceType::NPU, |
| 68 | + #[allow(deprecated)] |
| 69 | + "GNA" => DeviceType::GNA, |
| 70 | + s => DeviceType::Other(Cow::Borrowed(s)), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl FromStr for DeviceType<'static> { |
| 76 | + type Err = Infallible; |
| 77 | + |
| 78 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 79 | + Ok(DeviceType::from(s).to_owned()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl Display for DeviceType<'_> { |
| 84 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 85 | + f.write_str(self.into()) |
| 86 | + } |
| 87 | +} |
0 commit comments