diff options
author | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
---|---|---|
committer | Dmitry <[email protected]> | 2020-08-14 19:32:05 +0100 |
commit | 178c3e135a2a249692f7784712492e7884ae0c00 (patch) | |
tree | ac6b769dbf7162150caa0c1624786a4dd79ff3be /crates/stdx | |
parent | 06ff8e6c760ff05f10e868b5d1f9d79e42fbb49c (diff) | |
parent | c2594daf2974dbd4ce3d9b7ec72481764abaceb5 (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'crates/stdx')
-rw-r--r-- | crates/stdx/Cargo.toml | 4 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 65 |
2 files changed, 3 insertions, 66 deletions
diff --git a/crates/stdx/Cargo.toml b/crates/stdx/Cargo.toml index 4c0b85861..b186b46f2 100644 --- a/crates/stdx/Cargo.toml +++ b/crates/stdx/Cargo.toml | |||
@@ -1,9 +1,9 @@ | |||
1 | [package] | 1 | [package] |
2 | name = "stdx" | 2 | name = "stdx" |
3 | version = "0.1.0" | 3 | version = "0.0.0" |
4 | license = "MIT OR Apache-2.0" | ||
4 | authors = ["rust-analyzer developers"] | 5 | authors = ["rust-analyzer developers"] |
5 | edition = "2018" | 6 | edition = "2018" |
6 | license = "MIT OR Apache-2.0" | ||
7 | 7 | ||
8 | [lib] | 8 | [lib] |
9 | doctest = false | 9 | doctest = false |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index b65875c96..3c5027fe5 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | //! Missing batteries for standard libraries. | 1 | //! Missing batteries for standard libraries. |
2 | use std::{cell::Cell, fmt, time::Instant}; | 2 | use std::time::Instant; |
3 | 3 | ||
4 | mod macros; | 4 | mod macros; |
5 | 5 | ||
@@ -8,69 +8,6 @@ pub fn is_ci() -> bool { | |||
8 | option_env!("CI").is_some() | 8 | option_env!("CI").is_some() |
9 | } | 9 | } |
10 | 10 | ||
11 | pub trait SepBy: Sized { | ||
12 | /// Returns an `impl fmt::Display`, which joins elements via a separator. | ||
13 | fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self>; | ||
14 | } | ||
15 | |||
16 | impl<I> SepBy for I | ||
17 | where | ||
18 | I: Iterator, | ||
19 | I::Item: fmt::Display, | ||
20 | { | ||
21 | fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self> { | ||
22 | SepByBuilder::new(sep, self) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | pub struct SepByBuilder<'a, I> { | ||
27 | sep: &'a str, | ||
28 | prefix: &'a str, | ||
29 | suffix: &'a str, | ||
30 | iter: Cell<Option<I>>, | ||
31 | } | ||
32 | |||
33 | impl<'a, I> SepByBuilder<'a, I> { | ||
34 | fn new(sep: &'a str, iter: I) -> SepByBuilder<'a, I> { | ||
35 | SepByBuilder { sep, prefix: "", suffix: "", iter: Cell::new(Some(iter)) } | ||
36 | } | ||
37 | |||
38 | pub fn prefix(mut self, prefix: &'a str) -> Self { | ||
39 | self.prefix = prefix; | ||
40 | self | ||
41 | } | ||
42 | |||
43 | pub fn suffix(mut self, suffix: &'a str) -> Self { | ||
44 | self.suffix = suffix; | ||
45 | self | ||
46 | } | ||
47 | |||
48 | /// Set both suffix and prefix. | ||
49 | pub fn surround_with(self, prefix: &'a str, suffix: &'a str) -> Self { | ||
50 | self.prefix(prefix).suffix(suffix) | ||
51 | } | ||
52 | } | ||
53 | |||
54 | impl<I> fmt::Display for SepByBuilder<'_, I> | ||
55 | where | ||
56 | I: Iterator, | ||
57 | I::Item: fmt::Display, | ||
58 | { | ||
59 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
60 | f.write_str(self.prefix)?; | ||
61 | let mut first = true; | ||
62 | for item in self.iter.take().unwrap() { | ||
63 | if !first { | ||
64 | f.write_str(self.sep)?; | ||
65 | } | ||
66 | first = false; | ||
67 | fmt::Display::fmt(&item, f)?; | ||
68 | } | ||
69 | f.write_str(self.suffix)?; | ||
70 | Ok(()) | ||
71 | } | ||
72 | } | ||
73 | |||
74 | #[must_use] | 11 | #[must_use] |
75 | pub fn timeit(label: &'static str) -> impl Drop { | 12 | pub fn timeit(label: &'static str) -> impl Drop { |
76 | struct Guard { | 13 | struct Guard { |