[][src]Macro enum_derive_2018::EnumInnerAsTrait

macro_rules! EnumInnerAsTrait {
    ($arg:tt $vis:vis enum $name:ident { $($body:tt)* }) => { ... };
    (
        @expand ($vis:vis $fn_name:ident -> &mut $tr:ty), $($tail:tt)*
    ) => { ... };
    (
        @expand ($vis:vis $fn_name:ident -> &$tr:ty), $($tail:tt)*
    ) => { ... };
    (
        @expand_inner
        ($vis:vis), $fn_name:ident, (mut), $tr:ty,
        $ty_name:ident,
        ($($var_names:ident($_var_tys:ty),)*)
    ) => { ... };
    (
        @expand_inner
        ($vis:vis), $fn_name:ident, (), $tr:ty,
        $ty_name:ident,
        ($($var_names:ident($_var_tys:ty),)*)
    ) => { ... };
}

Derives a method for an unary enum returning a borrowed pointer to the inner value, cast to a trait object.

EnumInnerAsTrait! accepts a single deriving form that specifies the name of the method to be derived, whether the borrow should be mutable, and the trait of interest. For example:

macro_attr! {
    #[derive(EnumInnerAsTrait!(pub as_display -> &dyn std::fmt::Display))]
    enum Value {
        U32(u32),
        U64(u64),
    }
}

let s = format!("{}", Value::U64(42).as_display());
assert_eq!(&s[..], "42");