rdif_base/
_macros.rs

1/// Defines a driver type that wraps a boxed trait object.
2///
3/// $name: driver name
4///
5/// $tr: driver trait path
6#[macro_export]
7macro_rules! def_driver {
8    ($name:ident, $tr:path) => {
9        $crate::paste! {
10            pub mod [<$name:lower>]{
11                use super::*;
12                pub struct $name(alloc::boxed::Box<dyn $tr>);
13
14                impl $name {
15                    pub fn new<T: $tr>(driver: T) -> Self {
16                        Self(alloc::boxed::Box::new(driver))
17                    }
18
19                    pub fn typed_ref<T: $tr>(&self) -> Option<&T> {
20                        self.raw_any()?.downcast_ref()
21                    }
22
23                    pub fn typed_mut<T: $tr>(&mut self) -> Option<&mut T> {
24                        self.raw_any_mut()?.downcast_mut()
25                    }
26                }
27
28                impl $crate::DriverGeneric for $name {
29                    fn open(&mut self) -> Result<(), rdif_base::KError> {
30                        self.0.open()
31                    }
32
33                    fn close(&mut self) -> Result<(), rdif_base::KError> {
34                        self.0.close()
35                    }
36
37                    fn raw_any(&self) -> Option<&dyn core::any::Any> {
38                        Some( self.0.as_ref() as &dyn core::any::Any )
39                    }
40
41                    fn raw_any_mut(&mut self) -> Option<&mut dyn core::any::Any> {
42                        Some( self.0.as_mut() as &mut dyn core::any::Any )
43                    }
44                }
45
46                impl core::ops::Deref for $name {
47                    type Target = dyn $tr;
48
49                    fn deref(&self) -> &Self::Target {
50                        self.0.as_ref()
51                    }
52                }
53
54                impl core::ops::DerefMut for $name {
55                    fn deref_mut(&mut self) -> &mut Self::Target {
56                        self.0.as_mut()
57                    }
58                }
59            }
60            pub use [<$name:lower>]::$name;
61        }
62    };
63}
64
65// / Defines a driver type that wraps a boxed trait object.
66// /
67// / $name: driver name
68// /
69// / $t: driver trait path
70// #[macro_export(local_inner_macros)]
71// macro_rules! def_driver_rdif {
72//     ($name:ident) => {
73//         paste::paste! {
74//             def_driver!($name, [<rdif_ $name:lower>]::Interface, [<rdif_ $name:lower>]);
75//         }
76//     };
77// }