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#[allow(clippy::large_enum_variant)]
16pub enum AxDeviceEnum {
17 #[cfg(feature = "net")]
19 Net(AxNetDevice),
20 #[cfg(feature = "block")]
22 Block(AxBlockDevice),
23 #[cfg(feature = "display")]
25 Display(AxDisplayDevice),
26 #[cfg(feature = "input")]
28 Input(AxInputDevice),
29 #[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
72pub struct AxDeviceContainer<D>(SmallVec<[D; 1]>);
74
75impl<D> AxDeviceContainer<D> {
76 pub fn from_one(dev: D) -> Self {
78 Self(SmallVec::from_buf([dev]))
79 }
80
81 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}