livekit_utils/
enum_dispatch.rs

1
2// TODO(theomonnom): Match the complete function signature like:
3//  - pub(crate) fn update_info(&self, info: ParticipantInfo) -> ();
4#[macro_export]
5macro_rules! enum_dispatch {
6    // This arm is used to avoid nested loops with the arguments
7    // The arguments are transformed to $combined_args TokenTree
8    (@match $self:ident $fnc:ident $combined_args:tt [$($variant:ident),+]) => {
9        match $self {
10            $(
11                Self::$variant(inner) => inner.$fnc$combined_args,
12            )+
13        }
14    };
15
16    ($vis:vis$(,)? $fnc:ident, $self:ty, [$($arg:ident: $t:ty),*], $ret:ty, [$($variant:ident),+]) => {
17        $vis fn $fnc(self: $self, $($arg: $t),*) -> $ret {
18            enum_dispatch!(@match self $fnc ($($arg,)*) [$($variant),+])
19        }
20    };
21
22    ($variants:tt $(fnc!($vis:vis$(,)? $fnc:ident, $self:ty, $args:tt, $ret:ty);)+) => {
23        $(
24            enum_dispatch!($vis, $fnc, $self, $args, $ret, $variants);
25        )*
26    };
27}