mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-07-23 04:55:31 +00:00
Replace SepBy with Itertools
This commit is contained in:
parent
42a1692629
commit
1c359ab634
7 changed files with 30 additions and 94 deletions
|
@ -1,5 +1,5 @@
|
|||
//! Missing batteries for standard libraries.
|
||||
use std::{cell::Cell, fmt, time::Instant};
|
||||
use std::time::Instant;
|
||||
|
||||
mod macros;
|
||||
|
||||
|
@ -8,69 +8,6 @@ pub fn is_ci() -> bool {
|
|||
option_env!("CI").is_some()
|
||||
}
|
||||
|
||||
pub trait SepBy: Sized {
|
||||
/// Returns an `impl fmt::Display`, which joins elements via a separator.
|
||||
fn sep_by(self, sep: &str) -> SepByBuilder<'_, Self>;
|
||||
}
|
||||
|
||||
impl<I> SepBy for I
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: fmt::Display,
|
||||
{
|
||||
fn sep_by(self, sep: &str) -> SepByBuilder<'_, Self> {
|
||||
SepByBuilder::new(sep, self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SepByBuilder<'a, I> {
|
||||
sep: &'a str,
|
||||
prefix: &'a str,
|
||||
suffix: &'a str,
|
||||
iter: Cell<Option<I>>,
|
||||
}
|
||||
|
||||
impl<'a, I> SepByBuilder<'a, I> {
|
||||
fn new(sep: &'a str, iter: I) -> SepByBuilder<'a, I> {
|
||||
SepByBuilder { sep, prefix: "", suffix: "", iter: Cell::new(Some(iter)) }
|
||||
}
|
||||
|
||||
pub fn prefix(mut self, prefix: &'a str) -> Self {
|
||||
self.prefix = prefix;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn suffix(mut self, suffix: &'a str) -> Self {
|
||||
self.suffix = suffix;
|
||||
self
|
||||
}
|
||||
|
||||
/// Set both suffix and prefix.
|
||||
pub fn surround_with(self, prefix: &'a str, suffix: &'a str) -> Self {
|
||||
self.prefix(prefix).suffix(suffix)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> fmt::Display for SepByBuilder<'_, I>
|
||||
where
|
||||
I: Iterator,
|
||||
I::Item: fmt::Display,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(self.prefix)?;
|
||||
let mut first = true;
|
||||
for item in self.iter.take().unwrap() {
|
||||
if !first {
|
||||
f.write_str(self.sep)?;
|
||||
}
|
||||
first = false;
|
||||
fmt::Display::fmt(&item, f)?;
|
||||
}
|
||||
f.write_str(self.suffix)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn timeit(label: &'static str) -> impl Drop {
|
||||
struct Guard {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue