From 693ac892f2db5db1ce7cf86db7bf6207b3515c42 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Jul 2020 16:16:53 +0200 Subject: Don't copy-paste `impl_froms` into every crate --- crates/stdx/src/macros.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/macros.rs b/crates/stdx/src/macros.rs index 0f7690a67..bf298460f 100644 --- a/crates/stdx/src/macros.rs +++ b/crates/stdx/src/macros.rs @@ -17,3 +17,24 @@ macro_rules! format_to { { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); } }; } + +// Generates `From` impls for `Enum E { Foo(Foo), Bar(Bar) }` enums +#[macro_export] +macro_rules! impl_from { + ($($variant:ident $(($($sub_variant:ident),*))?),* for $enum:ident) => { + $( + impl From<$variant> for $enum { + fn from(it: $variant) -> $enum { + $enum::$variant(it) + } + } + $($( + impl From<$sub_variant> for $enum { + fn from(it: $sub_variant) -> $enum { + $enum::$variant($variant::$sub_variant(it)) + } + } + )*)? + )* + } +} -- cgit v1.2.3