about summary refs log tree commit diff
path: root/crates/stdx/src
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2023-02-14 00:56:28 +0000
committerAleksey Kladov <aleksey.kladov@gmail.com>2023-02-14 01:09:50 +0000
commit0da27376cf3768d92bcd0c094b52da50146dc70f (patch)
tree0249938c5a632d60c548a1ca8767c30892804b7f /crates/stdx/src
parentc97aae38f20f64daede9877212aff83c259a4faa (diff)
downloadrust-0da27376cf3768d92bcd0c094b52da50146dc70f.tar.gz
rust-0da27376cf3768d92bcd0c094b52da50146dc70f.zip
Support UTF-32 position encoding
Looks like this is a native encoding for Emacs at least!
Diffstat (limited to 'crates/stdx/src')
-rw-r--r--crates/stdx/src/lib.rs1
-rw-r--r--crates/stdx/src/rand.rs21
2 files changed, 22 insertions, 0 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index bd24d7d28ba..5639aaf57cd 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -11,6 +11,7 @@ pub mod hash;
 pub mod process;
 pub mod panic_context;
 pub mod non_empty_vec;
+pub mod rand;
 
 pub use always_assert::{always, never};
 
diff --git a/crates/stdx/src/rand.rs b/crates/stdx/src/rand.rs
new file mode 100644
index 00000000000..b38506caef5
--- /dev/null
+++ b/crates/stdx/src/rand.rs
@@ -0,0 +1,21 @@
+//! We don't use `rand`, as that's too many things for us.
+//!
+//! Currently, we use oorandom instead, but it misses these two utilities.
+//! Perhaps we should switch to `fastrand`, or our own small prng, it's not like
+//! we need anything move complicatied that 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()
+}