use crate::stub::Arc;
use core::{any::Any, cmp::Ordering};
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, new)]
pub struct DeviceId {
pub type_id: u16,
pub index_id: u16,
}
pub trait Device: Default + Clone + core::fmt::Debug + Send + Sync + 'static {
fn from_id(device_id: DeviceId) -> Self;
fn to_id(&self) -> DeviceId;
}
impl core::fmt::Display for DeviceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!(
"DeviceId(type={}, index={})",
self.type_id, self.index_id
))
}
}
impl Ord for DeviceId {
fn cmp(&self, other: &Self) -> Ordering {
match self.type_id.cmp(&other.type_id) {
Ordering::Equal => self.index_id.cmp(&other.index_id),
other => other,
}
}
}
impl PartialOrd for DeviceId {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
pub type ServerUtilitiesHandle = Arc<dyn Any + Send + Sync>;
pub trait DeviceService: Send + 'static {
fn init(device_id: DeviceId) -> Self;
fn utilities(&self) -> ServerUtilitiesHandle;
fn stage() -> DeviceServiceStage {
DeviceServiceStage::Downstream
}
}
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum DeviceServiceStage {
Upstream = 0,
Downstream = 1,
}