summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/stdx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:50 +0000
commit2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35 (patch)
treed325add32978dbdc1db975a438b3a77d571b1ab8 /src/tools/rust-analyzer/crates/stdx
parentReleasing progress-linux version 1.68.2+dfsg1-1~progress7.99u1. (diff)
downloadrustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.tar.xz
rustc-2e00214b3efbdfeefaa0fe9e8b8fd519de7adc35.zip
Merging upstream version 1.69.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/rust-analyzer/crates/stdx')
-rw-r--r--src/tools/rust-analyzer/crates/stdx/Cargo.toml8
-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
4 files changed, 45 insertions, 8 deletions
diff --git a/src/tools/rust-analyzer/crates/stdx/Cargo.toml b/src/tools/rust-analyzer/crates/stdx/Cargo.toml
index f7b7d0964..c881f2fd3 100644
--- a/src/tools/rust-analyzer/crates/stdx/Cargo.toml
+++ b/src/tools/rust-analyzer/crates/stdx/Cargo.toml
@@ -2,9 +2,11 @@
name = "stdx"
version = "0.0.0"
description = "TBD"
-license = "MIT OR Apache-2.0"
-edition = "2021"
-rust-version = "1.65"
+
+authors.workspace = true
+edition.workspace = true
+license.workspace = true
+rust-version.workspace = true
[lib]
doctest = false
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()
+}