aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-13 15:16:53 +0100
committerAleksey Kladov <[email protected]>2020-07-13 15:21:02 +0100
commit693ac892f2db5db1ce7cf86db7bf6207b3515c42 (patch)
tree89272d8f03145df99fe68f0e35f8892dc4ab2c72 /crates/stdx/src
parent6b4cf5b7d8043469c9856f6578d282f9532d7fe0 (diff)
Don't copy-paste `impl_froms` into every crate
Diffstat (limited to 'crates/stdx/src')
-rw-r--r--crates/stdx/src/macros.rs21
1 files changed, 21 insertions, 0 deletions
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 {
17 { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); } 17 { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); }
18 }; 18 };
19} 19}
20
21// Generates `From` impls for `Enum E { Foo(Foo), Bar(Bar) }` enums
22#[macro_export]
23macro_rules! impl_from {
24 ($($variant:ident $(($($sub_variant:ident),*))?),* for $enum:ident) => {
25 $(
26 impl From<$variant> for $enum {
27 fn from(it: $variant) -> $enum {
28 $enum::$variant(it)
29 }
30 }
31 $($(
32 impl From<$sub_variant> for $enum {
33 fn from(it: $sub_variant) -> $enum {
34 $enum::$variant($variant::$sub_variant(it))
35 }
36 }
37 )*)?
38 )*
39 }
40}