From ba585309ec9ba6102038cd16c3fff107dfc1f56c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Jul 2020 19:49:10 +0200 Subject: Replace rand with oorandom --- crates/ra_ide/Cargo.toml | 2 +- crates/ra_ide/src/syntax_highlighting/html.rs | 10 ++++----- crates/ra_ide/test_data/rainbow_highlighting.html | 12 +++++------ crates/rust-analyzer/Cargo.toml | 2 +- crates/rust-analyzer/src/cli/analysis_stats.rs | 25 ++++++++++++++++++----- 5 files changed, 33 insertions(+), 18 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index 6f8107491..f4181c4eb 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -17,7 +17,7 @@ indexmap = "1.3.2" itertools = "0.9.0" log = "0.4.8" rustc-hash = "1.1.0" -rand = { version = "0.7.3", features = ["small_rng"] } +oorandom = "11.1.2" stdx = { path = "../stdx" } diff --git a/crates/ra_ide/src/syntax_highlighting/html.rs b/crates/ra_ide/src/syntax_highlighting/html.rs index 0be55bca9..a5e7d2867 100644 --- a/crates/ra_ide/src/syntax_highlighting/html.rs +++ b/crates/ra_ide/src/syntax_highlighting/html.rs @@ -1,5 +1,6 @@ //! Renders a bit of code as HTML. +use oorandom::Rand32; use ra_db::SourceDatabase; use ra_syntax::{AstNode, TextRange, TextSize}; @@ -9,13 +10,12 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo let parse = db.parse(file_id); fn rainbowify(seed: u64) -> String { - use rand::prelude::*; - let mut rng = SmallRng::seed_from_u64(seed); + let mut rng = Rand32::new(seed); format!( "hsl({h},{s}%,{l}%)", - h = rng.gen_range::(0, 361), - s = rng.gen_range::(42, 99), - l = rng.gen_range::(40, 91), + h = rng.rand_range(0..361), + s = rng.rand_range(42..99), + l = rng.rand_range(40..91), ) } diff --git a/crates/ra_ide/test_data/rainbow_highlighting.html b/crates/ra_ide/test_data/rainbow_highlighting.html index 08d83302c..401e87a73 100644 --- a/crates/ra_ide/test_data/rainbow_highlighting.html +++ b/crates/ra_ide/test_data/rainbow_highlighting.html @@ -36,14 +36,14 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
fn main() {
-    let hello = "hello";
-    let x = hello.to_string();
-    let y = hello.to_string();
+    let hello = "hello";
+    let x = hello.to_string();
+    let y = hello.to_string();
 
-    let x = "other color please!";
-    let y = x.to_string();
+    let x = "other color please!";
+    let y = x.to_string();
 }
 
 fn bar() {
-    let mut hello = "hello";
+    let mut hello = "hello";
 }
\ No newline at end of file diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 3f9c820c5..931fc61ed 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -23,7 +23,7 @@ log = "0.4.8" lsp-types = { version = "0.78.0", features = ["proposed"] } parking_lot = "0.11.0" pico-args = "0.3.1" -rand = { version = "0.7.3", features = ["small_rng"] } +oorandom = "11.1.2" rustc-hash = "1.1.0" serde = { version = "1.0.106", features = ["derive"] } serde_json = "1.0.48" diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index ccc058682..6b7c08971 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -1,7 +1,10 @@ //! Fully type-check project and print various stats, like the number of type //! errors. -use std::{path::Path, time::Instant}; +use std::{ + path::Path, + time::{Instant, SystemTime, UNIX_EPOCH}, +}; use hir::{ db::{AstDatabase, DefDatabase, HirDatabase}, @@ -10,12 +13,12 @@ use hir::{ use hir_def::FunctionId; use hir_ty::{Ty, TypeWalk}; use itertools::Itertools; +use oorandom::Rand32; use ra_db::{ salsa::{self, ParallelDatabase}, SourceDatabaseExt, }; use ra_syntax::AstNode; -use rand::{seq::SliceRandom, thread_rng}; use rayon::prelude::*; use rustc_hash::FxHashSet; use stdx::format_to; @@ -46,6 +49,11 @@ pub fn analysis_stats( load_output_dirs: bool, with_proc_macro: bool, ) -> Result<()> { + let mut rng = { + let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64; + Rand32::new(seed) + }; + let db_load_time = Instant::now(); let (host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?; let db = host.raw_database(); @@ -57,7 +65,7 @@ pub fn analysis_stats( let mut krates = Crate::all(db); if randomize { - krates.shuffle(&mut thread_rng()); + shuffle(&mut rng, &mut krates); } for krate in krates { let module = krate.root_module(db).expect("crate without root module"); @@ -72,7 +80,7 @@ pub fn analysis_stats( } if randomize { - visit_queue.shuffle(&mut thread_rng()); + shuffle(&mut rng, &mut visit_queue); } eprintln!("Crates in this dir: {}", num_crates); @@ -110,7 +118,7 @@ pub fn analysis_stats( ); if randomize { - funcs.shuffle(&mut thread_rng()); + shuffle(&mut rng, &mut funcs); } let mut bar = match verbosity { @@ -306,3 +314,10 @@ pub fn analysis_stats( Ok(()) } + +fn shuffle(rng: &mut Rand32, slice: &mut [T]) { + for i in (1..slice.len()).rev() { + let idx = rng.rand_range(0..i as u32) as usize; + slice.swap(idx, i - 1) + } +} -- cgit v1.2.3