diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-07-13 15:33:32 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-07-13 15:33:32 +0100 |
commit | 853440775d72974585ca3fe39f9688a4d4302dd3 (patch) | |
tree | 89272d8f03145df99fe68f0e35f8892dc4ab2c72 /crates/stdx | |
parent | 4b1c3724364ff538acdcb44bdd23d501ef54cff3 (diff) | |
parent | 693ac892f2db5db1ce7cf86db7bf6207b3515c42 (diff) |
Merge #5342
5342: Don't copy-paste `impl_froms` into every crate r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/src/lib.rs | 21 | ||||
-rw-r--r-- | crates/stdx/src/macros.rs | 40 |
2 files changed, 42 insertions, 19 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 3b605c79d..988853ed2 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -1,30 +1,13 @@ | |||
1 | //! Missing batteries for standard libraries. | 1 | //! Missing batteries for standard libraries. |
2 | use std::{cell::Cell, fmt, time::Instant}; | 2 | use std::{cell::Cell, fmt, time::Instant}; |
3 | 3 | ||
4 | mod macros; | ||
5 | |||
4 | #[inline(always)] | 6 | #[inline(always)] |
5 | pub fn is_ci() -> bool { | 7 | pub fn is_ci() -> bool { |
6 | option_env!("CI").is_some() | 8 | option_env!("CI").is_some() |
7 | } | 9 | } |
8 | 10 | ||
9 | #[macro_export] | ||
10 | macro_rules! eprintln { | ||
11 | ($($tt:tt)*) => {{ | ||
12 | if $crate::is_ci() { | ||
13 | panic!("Forgot to remove debug-print?") | ||
14 | } | ||
15 | std::eprintln!($($tt)*) | ||
16 | }} | ||
17 | } | ||
18 | |||
19 | /// Appends formatted string to a `String`. | ||
20 | #[macro_export] | ||
21 | macro_rules! format_to { | ||
22 | ($buf:expr) => (); | ||
23 | ($buf:expr, $lit:literal $($arg:tt)*) => { | ||
24 | { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); } | ||
25 | }; | ||
26 | } | ||
27 | |||
28 | pub trait SepBy: Sized { | 11 | pub trait SepBy: Sized { |
29 | /// Returns an `impl fmt::Display`, which joins elements via a separator. | 12 | /// Returns an `impl fmt::Display`, which joins elements via a separator. |
30 | fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self>; | 13 | fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self>; |
diff --git a/crates/stdx/src/macros.rs b/crates/stdx/src/macros.rs new file mode 100644 index 000000000..bf298460f --- /dev/null +++ b/crates/stdx/src/macros.rs | |||
@@ -0,0 +1,40 @@ | |||
1 | //! Convenience macros. | ||
2 | #[macro_export] | ||
3 | macro_rules! eprintln { | ||
4 | ($($tt:tt)*) => {{ | ||
5 | if $crate::is_ci() { | ||
6 | panic!("Forgot to remove debug-print?") | ||
7 | } | ||
8 | std::eprintln!($($tt)*) | ||
9 | }} | ||
10 | } | ||
11 | |||
12 | /// Appends formatted string to a `String`. | ||
13 | #[macro_export] | ||
14 | macro_rules! format_to { | ||
15 | ($buf:expr) => (); | ||
16 | ($buf:expr, $lit:literal $($arg:tt)*) => { | ||
17 | { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); } | ||
18 | }; | ||
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 | } | ||