summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/stdx/src
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rust-analyzer/crates/stdx/src')
-rw-r--r--src/tools/rust-analyzer/crates/stdx/src/lib.rs15
-rw-r--r--src/tools/rust-analyzer/crates/stdx/src/macros.rs9
-rw-r--r--src/tools/rust-analyzer/crates/stdx/src/rand.rs21
3 files changed, 40 insertions, 5 deletions
diff --git a/src/tools/rust-analyzer/crates/stdx/src/lib.rs b/src/tools/rust-analyzer/crates/stdx/src/lib.rs
index 51e109798..5639aaf57 100644
--- a/src/tools/rust-analyzer/crates/stdx/src/lib.rs
+++ b/src/tools/rust-analyzer/crates/stdx/src/lib.rs
@@ -2,15 +2,16 @@
#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+use std::io as sio;
use std::process::Command;
use std::{cmp::Ordering, ops, time::Instant};
-use std::{io as sio, iter};
mod macros;
pub mod hash;
pub mod process;
pub mod panic_context;
pub mod non_empty_vec;
+pub mod rand;
pub use always_assert::{always, never};
@@ -39,15 +40,19 @@ Uncomment `default = [ "backtrace" ]` in `crates/stdx/Cargo.toml`.
}
pub fn to_lower_snake_case(s: &str) -> String {
- to_snake_case(s, char::to_ascii_lowercase)
+ to_snake_case(s, char::to_lowercase)
}
pub fn to_upper_snake_case(s: &str) -> String {
- to_snake_case(s, char::to_ascii_uppercase)
+ to_snake_case(s, char::to_uppercase)
}
// Code partially taken from rust/compiler/rustc_lint/src/nonstandard_style.rs
// commit: 9626f2b
-fn to_snake_case<F: Fn(&char) -> char>(mut s: &str, change_case: F) -> String {
+fn to_snake_case<F, I>(mut s: &str, change_case: F) -> String
+where
+ F: Fn(char) -> I,
+ I: Iterator<Item = char>,
+{
let mut words = vec![];
// Preserve leading underscores
@@ -75,7 +80,7 @@ fn to_snake_case<F: Fn(&char) -> char>(mut s: &str, change_case: F) -> String {
}
last_upper = ch.is_uppercase();
- buf.extend(iter::once(change_case(&ch)));
+ buf.extend(change_case(ch));
}
words.push(buf);
diff --git a/src/tools/rust-analyzer/crates/stdx/src/macros.rs b/src/tools/rust-analyzer/crates/stdx/src/macros.rs
index d91fc690c..1a9982fa8 100644
--- a/src/tools/rust-analyzer/crates/stdx/src/macros.rs
+++ b/src/tools/rust-analyzer/crates/stdx/src/macros.rs
@@ -43,5 +43,14 @@ macro_rules! impl_from {
}
)*)?
)*
+ };
+ ($($variant:ident$(<$V:ident>)?),* for $enum:ident) => {
+ $(
+ impl$(<$V>)? From<$variant$(<$V>)?> for $enum$(<$V>)? {
+ fn from(it: $variant$(<$V>)?) -> $enum$(<$V>)? {
+ $enum::$variant(it)
+ }
+ }
+ )*
}
}
diff --git a/src/tools/rust-analyzer/crates/stdx/src/rand.rs b/src/tools/rust-analyzer/crates/stdx/src/rand.rs
new file mode 100644
index 000000000..64aa57eae
--- /dev/null
+++ b/src/tools/rust-analyzer/crates/stdx/src/rand.rs
@@ -0,0 +1,21 @@
+//! We don't use `rand`, as that's too many things for us.
+//!
+//! We currently use oorandom instead, but it's missing these two utilities.
+//! Perhaps we should switch to `fastrand`, or our own small PRNG, it's not like
+//! we need anything more complicated than xor-shift.
+
+pub fn shuffle<T>(slice: &mut [T], mut rand_index: impl FnMut(usize) -> usize) {
+ let mut remaining = slice.len() - 1;
+ while remaining > 0 {
+ let index = rand_index(remaining);
+ slice.swap(remaining, index);
+ remaining -= 1;
+ }
+}
+
+pub fn seed() -> u64 {
+ use std::collections::hash_map::RandomState;
+ use std::hash::{BuildHasher, Hasher};
+
+ RandomState::new().build_hasher().finish()
+}