Skip to main content

deepcl_common/
device.rs

1use core::cmp::Ordering;
2
3/// The device id.
4#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, new)]
5pub struct DeviceId {
6    /// The type id identifies the type of the device.
7    pub type_id: u16,
8    /// The index id identifies the device number.
9    pub index_id: u32,
10}
11
12/// Device trait for all deepcl devices.
13pub trait Device: Default + Clone + core::fmt::Debug + Send + Sync {
14    /// Create a device from its [id](DeviceId).
15    fn from_id(device_id: DeviceId) -> Self;
16    /// Retrieve the [device id](DeviceId) from the device.
17    fn to_id(&self) -> DeviceId;
18    /// Returns the number of devices available under the provided type id.
19    fn device_count(type_id: u16) -> usize;
20    /// Returns the total number of devices that can be handled by the runtime.
21    fn device_count_total() -> usize {
22        Self::device_count(0)
23    }
24}
25
26impl core::fmt::Display for DeviceId {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        f.write_fmt(format_args!("{self:?}"))
29    }
30}
31
32impl Ord for DeviceId {
33    fn cmp(&self, other: &Self) -> Ordering {
34        match self.type_id.cmp(&other.type_id) {
35            Ordering::Equal => self.index_id.cmp(&other.index_id),
36            other => other,
37        }
38    }
39}
40
41impl PartialOrd for DeviceId {
42    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
43        Some(self.cmp(other))
44    }
45}