Derive Macro strum::EnumTryAs

source ยท
#[derive(EnumTryAs)]
{
    // Attributes available to this derive:
    #[strum]
}
Expand description

Generated try_as_*() methods for all tuple-style variants. E.g. Message.try_as_write().

These methods will only be generated for tuple-style variants, not for named or unit variants.

use strum_macros::EnumTryAs;

#[derive(EnumTryAs, Debug)]
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

assert_eq!(
    Message::Write(String::from("Hello")).try_as_write(),
    Some(String::from("Hello"))
);
assert_eq!(
    Message::ChangeColor(1, 2, 3).try_as_change_color(),
    Some((1, 2, 3))
);