Skip to main content

enum_dispatch

Macro enum_dispatch 

Source
macro_rules! enum_dispatch {
    (@match [$($variant:ident),+]: $fnc:ident, $self:ident, $combined_args:tt) => { ... };
    (@fnc [$($variant:ident),+]: $vis:vis fn $fnc:ident($self:ident: $sty:ty $(, $arg:ident: $t:ty)*) -> $ret:ty) => { ... };
    ($variants:tt; $($vis:vis fn $fnc:ident$args:tt -> $ret:ty;)+) => { ... };
}
Expand description

Generates methods on an enum that forward each call to the inner value of every variant.

Given a list of variants and a set of method signatures, this expands to a match over self that dispatches to the identically-named method on each variant’s inner type, saving the boilerplate of writing one match arm per variant per method.

impl AnyStreamInfo {
    enum_dispatch!(
        [Byte, Text];
        pub fn id(self: &Self) -> &str;
        pub fn total_length(self: &Self) -> Option<u64>;
    );
}