aboutsummaryrefslogtreecommitdiff
path: root/crates/stdx
diff options
context:
space:
mode:
Diffstat (limited to 'crates/stdx')
-rw-r--r--crates/stdx/Cargo.toml4
-rw-r--r--crates/stdx/src/lib.rs65
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]
2name = "stdx" 2name = "stdx"
3version = "0.1.0" 3version = "0.0.0"
4license = "MIT OR Apache-2.0"
4authors = ["rust-analyzer developers"] 5authors = ["rust-analyzer developers"]
5edition = "2018" 6edition = "2018"
6license = "MIT OR Apache-2.0"
7 7
8[lib] 8[lib]
9doctest = false 9doctest = 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.
2use std::{cell::Cell, fmt, time::Instant}; 2use std::time::Instant;
3 3
4mod macros; 4mod 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
11pub 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
16impl<I> SepBy for I
17where
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
26pub struct SepByBuilder<'a, I> {
27 sep: &'a str,
28 prefix: &'a str,
29 suffix: &'a str,
30 iter: Cell<Option<I>>,
31}
32
33impl<'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
54impl<I> fmt::Display for SepByBuilder<'_, I>
55where
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]
75pub fn timeit(label: &'static str) -> impl Drop { 12pub fn timeit(label: &'static str) -> impl Drop {
76 struct Guard { 13 struct Guard {