From 6344a7f362b19eaf71547766135ece160aa3389e Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Mon, 10 Aug 2020 15:05:01 +0300 Subject: Fix clippy warnings --- crates/stdx/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index b65875c96..00bfcd29e 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -10,7 +10,7 @@ pub fn is_ci() -> bool { pub trait SepBy: Sized { /// Returns an `impl fmt::Display`, which joins elements via a separator. - fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self>; + fn sep_by(self, sep: &str) -> SepByBuilder<'_, Self>; } impl SepBy for I @@ -18,7 +18,7 @@ where I: Iterator, I::Item: fmt::Display, { - fn sep_by<'a>(self, sep: &'a str) -> SepByBuilder<'a, Self> { + fn sep_by(self, sep: &str) -> SepByBuilder<'_, Self> { SepByBuilder::new(sep, self) } } -- cgit v1.2.3 From 1c359ab634edb81b51e3c7eadfb83d46c926e890 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 15:04:06 +0200 Subject: Replace SepBy with Itertools --- crates/stdx/src/lib.rs | 65 +------------------------------------------------- 1 file changed, 1 insertion(+), 64 deletions(-) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 00bfcd29e..3c5027fe5 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -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 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>, -} - -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 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 { -- cgit v1.2.3 From 9389e313d0f5f16e301acaf38fee1c00c76c5cce Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Aug 2020 13:20:17 +0200 Subject: Minor --- crates/stdx/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 3c5027fe5..265d19288 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -17,7 +17,7 @@ pub fn timeit(label: &'static str) -> impl Drop { impl Drop for Guard { fn drop(&mut self) { - eprintln!("{}: {:?}", self.label, self.start.elapsed()) + eprintln!("{}: {:.2?}", self.label, self.start.elapsed()) } } -- cgit v1.2.3 From aad911fb0cd772e809270be3e0a526d4738b9dd5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Aug 2020 17:20:10 +0200 Subject: Speedup ty tests Closes #5792 --- crates/stdx/src/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 265d19288..5d60f0219 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -1,5 +1,8 @@ //! Missing batteries for standard libraries. -use std::time::Instant; +use std::{ + sync::atomic::{AtomicUsize, Ordering}, + time::Instant, +}; mod macros; @@ -134,6 +137,31 @@ where left } +pub struct RacyFlag(AtomicUsize); + +impl RacyFlag { + pub const fn new() -> RacyFlag { + RacyFlag(AtomicUsize::new(0)) + } + + pub fn get(&self, init: impl FnMut() -> bool) -> bool { + let mut init = Some(init); + self.get_impl(&mut || init.take().map_or(false, |mut f| f())) + } + + fn get_impl(&self, init: &mut dyn FnMut() -> bool) -> bool { + match self.0.load(Ordering::Relaxed) { + 0 => false, + 1 => true, + _ => { + let res = init(); + self.0.store(if res { 1 } else { 0 }, Ordering::Relaxed); + res + } + } + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3 From 17d6efe6f20732a1508acd014ff9582c05ebaa05 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Aug 2020 23:51:01 +0200 Subject: Make RacyFlag actually work --- crates/stdx/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/stdx/src') diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 5d60f0219..273b0f55b 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs @@ -141,7 +141,7 @@ pub struct RacyFlag(AtomicUsize); impl RacyFlag { pub const fn new() -> RacyFlag { - RacyFlag(AtomicUsize::new(0)) + RacyFlag(AtomicUsize::new(!0)) } pub fn get(&self, init: impl FnMut() -> bool) -> bool { -- cgit v1.2.3