From d061ab6c8e7d40b5b83c324282b0d8a7da6ada0a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 15:40:15 +0200 Subject: [PATCH] Code shuffle resiliently --- crates/rust-analyzer/src/cli/analysis_stats.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 187a0ebe632..a270eb481c0 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -309,8 +309,13 @@ pub fn analysis_stats( } 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) + for i in 0..slice.len() { + randomize_first(rng, &mut slice[i..]); + } + + fn randomize_first(rng: &mut Rand32, slice: &mut [T]) { + assert!(!slice.is_empty()); + let idx = rng.rand_range(0..slice.len() as u32) as usize; + slice.swap(0, idx); } }