Skip to content

Commit 2e10f15

Browse files
bors[bot]matklad
andauthored
Merge #5574
5574: Replace rand with oorandom r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 17126b9 + 63de7bb commit 2e10f15

File tree

7 files changed

+44
-98
lines changed

7 files changed

+44
-98
lines changed

Cargo.lock

Lines changed: 11 additions & 79 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_ide/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ indexmap = "1.3.2"
1717
itertools = "0.9.0"
1818
log = "0.4.8"
1919
rustc-hash = "1.1.0"
20-
rand = { version = "0.7.3", features = ["small_rng"] }
20+
oorandom = "11.1.2"
2121

2222
stdx = { path = "../stdx" }
2323

crates/ra_ide/src/syntax_highlighting/html.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Renders a bit of code as HTML.
22
3+
use oorandom::Rand32;
34
use ra_db::SourceDatabase;
45
use ra_syntax::{AstNode, TextRange, TextSize};
56

@@ -9,13 +10,12 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
910
let parse = db.parse(file_id);
1011

1112
fn rainbowify(seed: u64) -> String {
12-
use rand::prelude::*;
13-
let mut rng = SmallRng::seed_from_u64(seed);
13+
let mut rng = Rand32::new(seed);
1414
format!(
1515
"hsl({h},{s}%,{l}%)",
16-
h = rng.gen_range::<u16, _, _>(0, 361),
17-
s = rng.gen_range::<u16, _, _>(42, 99),
18-
l = rng.gen_range::<u16, _, _>(40, 91),
16+
h = rng.rand_range(0..361),
17+
s = rng.rand_range(42..99),
18+
l = rng.rand_range(40..91),
1919
)
2020
}
2121

crates/ra_ide/test_data/rainbow_highlighting.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
.unresolved_reference { color: #FC5555; text-decoration: wavy underline; }
3737
</style>
3838
<pre><code><span class="keyword">fn</span> <span class="function declaration">main</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
39-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
40-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="2705725358298919760" style="color: hsl(17,51%,74%);">x</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
41-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="3365759661443752373" style="color: hsl(127,76%,66%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
39+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
40+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="2705725358298919760" style="color: hsl(76,47%,83%);">x</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
41+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="3365759661443752373" style="color: hsl(15,86%,51%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
4242

43-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span> <span class="operator">=</span> <span class="string_literal">"other color please!"</span><span class="punctuation">;</span>
44-
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="6717528807933952652" style="color: hsl(85,49%,84%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="794745962933817518" style="color: hsl(19,74%,76%);">x</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
43+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="794745962933817518" style="color: hsl(127,71%,87%);">x</span> <span class="operator">=</span> <span class="string_literal">"other color please!"</span><span class="punctuation">;</span>
44+
<span class="keyword">let</span> <span class="variable declaration" data-binding-hash="6717528807933952652" style="color: hsl(90,74%,79%);">y</span> <span class="operator">=</span> <span class="variable" data-binding-hash="794745962933817518" style="color: hsl(127,71%,87%);">x</span><span class="punctuation">.</span><span class="unresolved_reference">to_string</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span>
4545
<span class="punctuation">}</span>
4646

4747
<span class="keyword">fn</span> <span class="function declaration">bar</span><span class="punctuation">(</span><span class="punctuation">)</span> <span class="punctuation">{</span>
48-
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable" data-binding-hash="8121853618659664005" style="color: hsl(261,57%,61%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
48+
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable" data-binding-hash="8121853618659664005" style="color: hsl(273,88%,88%);">hello</span> <span class="operator">=</span> <span class="string_literal">"hello"</span><span class="punctuation">;</span>
4949
<span class="punctuation">}</span></code></pre>

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ log = "0.4.8"
2323
lsp-types = { version = "0.78.0", features = ["proposed"] }
2424
parking_lot = "0.11.0"
2525
pico-args = "0.3.1"
26-
rand = { version = "0.7.3", features = ["small_rng"] }
26+
oorandom = "11.1.2"
2727
rustc-hash = "1.1.0"
2828
serde = { version = "1.0.106", features = ["derive"] }
2929
serde_json = "1.0.48"

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Fully type-check project and print various stats, like the number of type
22
//! errors.
33
4-
use std::{path::Path, time::Instant};
4+
use std::{
5+
path::Path,
6+
time::{Instant, SystemTime, UNIX_EPOCH},
7+
};
58

69
use hir::{
710
db::{AstDatabase, DefDatabase, HirDatabase},
@@ -10,12 +13,12 @@ use hir::{
1013
use hir_def::FunctionId;
1114
use hir_ty::{Ty, TypeWalk};
1215
use itertools::Itertools;
16+
use oorandom::Rand32;
1317
use ra_db::{
1418
salsa::{self, ParallelDatabase},
1519
SourceDatabaseExt,
1620
};
1721
use ra_syntax::AstNode;
18-
use rand::{seq::SliceRandom, thread_rng};
1922
use rayon::prelude::*;
2023
use rustc_hash::FxHashSet;
2124
use stdx::format_to;
@@ -46,6 +49,11 @@ pub fn analysis_stats(
4649
load_output_dirs: bool,
4750
with_proc_macro: bool,
4851
) -> Result<()> {
52+
let mut rng = {
53+
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as u64;
54+
Rand32::new(seed)
55+
};
56+
4957
let db_load_time = Instant::now();
5058
let (host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?;
5159
let db = host.raw_database();
@@ -57,7 +65,7 @@ pub fn analysis_stats(
5765

5866
let mut krates = Crate::all(db);
5967
if randomize {
60-
krates.shuffle(&mut thread_rng());
68+
shuffle(&mut rng, &mut krates);
6169
}
6270
for krate in krates {
6371
let module = krate.root_module(db).expect("crate without root module");
@@ -72,7 +80,7 @@ pub fn analysis_stats(
7280
}
7381

7482
if randomize {
75-
visit_queue.shuffle(&mut thread_rng());
83+
shuffle(&mut rng, &mut visit_queue);
7684
}
7785

7886
eprintln!("Crates in this dir: {}", num_crates);
@@ -110,7 +118,7 @@ pub fn analysis_stats(
110118
);
111119

112120
if randomize {
113-
funcs.shuffle(&mut thread_rng());
121+
shuffle(&mut rng, &mut funcs);
114122
}
115123

116124
let mut bar = match verbosity {
@@ -306,3 +314,10 @@ pub fn analysis_stats(
306314

307315
Ok(())
308316
}
317+
318+
fn shuffle<T>(rng: &mut Rand32, slice: &mut [T]) {
319+
for i in (1..slice.len()).rev() {
320+
let idx = rng.rand_range(0..i as u32) as usize;
321+
slice.swap(idx, i)
322+
}
323+
}

xtask/tests/tidy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ fn check_licenses() {
5555
0BSD OR MIT OR Apache-2.0
5656
Apache-2.0 OR BSL-1.0
5757
Apache-2.0 OR MIT
58-
Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT
5958
Apache-2.0/MIT
6059
BSD-2-Clause
6160
BSD-3-Clause

0 commit comments

Comments
 (0)