shadow_rs/utility/enum_passthrough.rs
1/** A macro that defines a function for an enum, calling the same function on all enum variants.
2
3For example, the usage:
4
5```ignore
6enum_passthrough!(self, (cb_queue), Pipe, Socket;
7 pub fn close(&mut self, cb_queue: &mut EventQueue) -> SyscallResult
8);
9```
10
11expands to:
12
13```ignore
14pub fn close(&mut self, cb_queue: &mut EventQueue) -> SyscallResult {
15 match self {
16 Self::Pipe(x) => x.close(cb_queue),
17 Self::Socket(x) => x.close(cb_queue),
18 }
19}
20```
21**/
22macro_rules! enum_passthrough {
23 ($self:ident, $args2:tt, $($variant:ident),+; $v:vis fn $name:ident $args:tt $(-> $($rv:tt)+)?) => {
24 $v fn $name $args $(-> $($rv)+)? {
25 match $self {
26 $(
27 Self::$variant(x) => x.$name $args2,
28 )*
29 }
30 }
31 };
32}
33
34/** Like [`enum_passthrough!`], but allows generics. For example:
35
36```ignore
37enum_passthrough_generic!(self, (bytes, offset, cb_queue), Pipe, Socket;
38 pub fn read<W>(&mut self, bytes: W, offset: libc::off_t, cb_queue: &mut EventQueue) -> SyscallResult
39 where W: std::io::Write + std::io::Seek
40);
41```
42**/
43// This is currently unused, but keeping around for now since we may want it again in the future.
44#[allow(unused_macros)]
45macro_rules! enum_passthrough_generic {
46 ($self:ident, $args2:tt, $($variant:ident),+; $(#[$($mac:tt)+])? $v:vis fn $name:ident <$($generics:ident),+> $args:tt $(-> $($rv:tt)+)?) => {
47 $(#[$($mac)+])?
48 $v fn $name <$($generics)+> $args $(-> $($rv)+)? {
49 match $self {
50 $(
51 Self::$variant(x) => x.$name $args2,
52 )*
53 }
54 }
55 };
56}
57
58/** Like [`enum_passthrough!`], but calls `into()` on the return value. For example:
59
60```ignore
61enum_passthrough_into!(self, (cb_queue), Pipe, Socket;
62 pub fn close(&mut self, cb_queue: &mut EventQueue) -> SyscallResult
63);
64```
65**/
66macro_rules! enum_passthrough_into {
67 ($self:ident, $args2:tt, $($variant:ident),+; $(#[$($mac:tt)+])? $v:vis fn $name:ident $args:tt $(-> $($rv:tt)+)?) => {
68 $(#[$($mac)+])?
69 $v fn $name $args $(-> $($rv)+)? {
70 match $self {
71 $(
72 Self::$variant(x) => x.$name $args2.into(),
73 )*
74 }
75 }
76 };
77}