Skip to main content

ax_driver/structs/
mod.rs

1#![allow(unused_imports)]
2
3use core::ops::{Deref, DerefMut};
4
5use ax_driver_base::{BaseDriverOps, DeviceType};
6use smallvec::SmallVec;
7
8#[cfg_attr(feature = "dyn", path = "dyn.rs")]
9#[cfg_attr(not(feature = "dyn"), path = "static.rs")]
10mod imp;
11
12pub use imp::*;
13
14/// A unified enum that represents different categories of devices.
15#[allow(clippy::large_enum_variant)]
16pub enum AxDeviceEnum {
17    /// Network card device.
18    #[cfg(feature = "net")]
19    Net(AxNetDevice),
20    /// Block storage device.
21    #[cfg(feature = "block")]
22    Block(AxBlockDevice),
23    /// Graphic display device.
24    #[cfg(feature = "display")]
25    Display(AxDisplayDevice),
26    /// Graphic input device.
27    #[cfg(feature = "input")]
28    Input(AxInputDevice),
29    /// Vsock device.
30    #[cfg(feature = "vsock")]
31    Vsock(AxVsockDevice),
32}
33
34impl BaseDriverOps for AxDeviceEnum {
35    #[inline]
36    #[allow(unreachable_patterns)]
37    fn device_type(&self) -> DeviceType {
38        match self {
39            #[cfg(feature = "net")]
40            Self::Net(_) => DeviceType::Net,
41            #[cfg(feature = "block")]
42            Self::Block(_) => DeviceType::Block,
43            #[cfg(feature = "display")]
44            Self::Display(_) => DeviceType::Display,
45            #[cfg(feature = "input")]
46            Self::Input(_) => DeviceType::Input,
47            #[cfg(feature = "vsock")]
48            Self::Vsock(_) => DeviceType::Vsock,
49            _ => unreachable!(),
50        }
51    }
52
53    #[inline]
54    #[allow(unreachable_patterns)]
55    fn device_name(&self) -> &str {
56        match self {
57            #[cfg(feature = "net")]
58            Self::Net(dev) => dev.device_name(),
59            #[cfg(feature = "block")]
60            Self::Block(dev) => dev.device_name(),
61            #[cfg(feature = "display")]
62            Self::Display(dev) => dev.device_name(),
63            #[cfg(feature = "input")]
64            Self::Input(dev) => dev.device_name(),
65            #[cfg(feature = "vsock")]
66            Self::Vsock(dev) => dev.device_name(),
67            _ => unreachable!(),
68        }
69    }
70}
71
72/// A structure that contains all device drivers of a certain category.
73pub struct AxDeviceContainer<D>(SmallVec<[D; 1]>);
74
75impl<D> AxDeviceContainer<D> {
76    /// Constructs the container from one device.
77    pub fn from_one(dev: D) -> Self {
78        Self(SmallVec::from_buf([dev]))
79    }
80
81    /// Takes one device out of the container (will remove it from the
82    /// container).
83    pub fn take_one(&mut self) -> Option<D> {
84        self.0.pop()
85    }
86}
87
88impl<D> Deref for AxDeviceContainer<D> {
89    type Target = SmallVec<[D; 1]>;
90
91    fn deref(&self) -> &Self::Target {
92        &self.0
93    }
94}
95
96impl<D> DerefMut for AxDeviceContainer<D> {
97    fn deref_mut(&mut self) -> &mut Self::Target {
98        &mut self.0
99    }
100}
101
102impl<D> Default for AxDeviceContainer<D> {
103    fn default() -> Self {
104        Self(Default::default())
105    }
106}