diff options
author | Aleksey Kladov <[email protected]> | 2020-07-13 15:16:53 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-13 15:21:02 +0100 |
commit | 693ac892f2db5db1ce7cf86db7bf6207b3515c42 (patch) | |
tree | 89272d8f03145df99fe68f0e35f8892dc4ab2c72 /crates/stdx | |
parent | 6b4cf5b7d8043469c9856f6578d282f9532d7fe0 (diff) |
Don't copy-paste `impl_froms` into every crate
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/src/macros.rs | 21 |
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] | ||
23 | macro_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 | } | ||