Skip to content

Commit e2dc2c3

Browse files
authored
Merge pull request #926 from dhardy/fmt
Apply rustfmt formatting (partial)
2 parents 350ba17 + fab964d commit e2dc2c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+2314
-1543
lines changed

benches/generators.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use test::{black_box, Bencher};
1919

2020
use rand::prelude::*;
2121
use rand::rngs::adapter::ReseedingRng;
22-
use rand::rngs::{OsRng, mock::StepRng};
23-
use rand_chacha::{ChaCha20Core, ChaCha8Rng, ChaCha12Rng, ChaCha20Rng};
24-
use rand_hc::{Hc128Rng};
22+
use rand::rngs::{mock::StepRng, OsRng};
23+
use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng};
24+
use rand_hc::Hc128Rng;
2525
use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg};
2626

2727
macro_rules! gen_bytes {
@@ -38,7 +38,7 @@ macro_rules! gen_bytes {
3838
});
3939
b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
4040
}
41-
}
41+
};
4242
}
4343

4444
gen_bytes!(gen_bytes_step, StepRng::new(0, 1));
@@ -50,7 +50,7 @@ gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_entropy());
5050
gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_entropy());
5151
gen_bytes!(gen_bytes_hc128, Hc128Rng::from_entropy());
5252
gen_bytes!(gen_bytes_std, StdRng::from_entropy());
53-
#[cfg(feature="small_rng")]
53+
#[cfg(feature = "small_rng")]
5454
gen_bytes!(gen_bytes_small, SmallRng::from_entropy());
5555
gen_bytes!(gen_bytes_os, OsRng);
5656

@@ -68,7 +68,7 @@ macro_rules! gen_uint {
6868
});
6969
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
7070
}
71-
}
71+
};
7272
}
7373

7474
gen_uint!(gen_u32_step, u32, StepRng::new(0, 1));
@@ -80,7 +80,7 @@ gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_entropy());
8080
gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_entropy());
8181
gen_uint!(gen_u32_hc128, u32, Hc128Rng::from_entropy());
8282
gen_uint!(gen_u32_std, u32, StdRng::from_entropy());
83-
#[cfg(feature="small_rng")]
83+
#[cfg(feature = "small_rng")]
8484
gen_uint!(gen_u32_small, u32, SmallRng::from_entropy());
8585
gen_uint!(gen_u32_os, u32, OsRng);
8686

@@ -93,7 +93,7 @@ gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_entropy());
9393
gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_entropy());
9494
gen_uint!(gen_u64_hc128, u64, Hc128Rng::from_entropy());
9595
gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
96-
#[cfg(feature="small_rng")]
96+
#[cfg(feature = "small_rng")]
9797
gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
9898
gen_uint!(gen_u64_os, u64, OsRng);
9999

@@ -107,7 +107,7 @@ macro_rules! init_gen {
107107
r2
108108
});
109109
}
110-
}
110+
};
111111
}
112112

113113
init_gen!(init_pcg32, Pcg32);
@@ -123,9 +123,7 @@ macro_rules! reseeding_bytes {
123123
($fnn:ident, $thresh:expr) => {
124124
#[bench]
125125
fn $fnn(b: &mut Bencher) {
126-
let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(),
127-
$thresh * 1024,
128-
OsRng);
126+
let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(), $thresh * 1024, OsRng);
129127
let mut buf = [0u8; RESEEDING_BYTES_LEN];
130128
b.iter(|| {
131129
for _ in 0..RESEEDING_BENCH_N {
@@ -135,7 +133,7 @@ macro_rules! reseeding_bytes {
135133
});
136134
b.bytes = RESEEDING_BYTES_LEN as u64 * RESEEDING_BENCH_N;
137135
}
138-
}
136+
};
139137
}
140138

141139
reseeding_bytes!(reseeding_chacha20_4k, 4);
@@ -160,7 +158,7 @@ macro_rules! threadrng_uint {
160158
});
161159
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
162160
}
163-
}
161+
};
164162
}
165163

166164
threadrng_uint!(thread_rng_u32, u32);

benches/misc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const RAND_BENCH_N: u64 = 1000;
1414

1515
use test::Bencher;
1616

17+
use rand::distributions::{Bernoulli, Distribution, Standard};
1718
use rand::prelude::*;
18-
use rand::distributions::{Distribution, Standard, Bernoulli};
1919
use rand_pcg::{Pcg32, Pcg64Mcg};
2020

2121
#[bench]

benches/seq.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const RAND_BENCH_N: u64 = 1000;
2626
#[bench]
2727
fn seq_shuffle_100(b: &mut Bencher) {
2828
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
29-
let x : &mut [usize] = &mut [1; 100];
29+
let x: &mut [usize] = &mut [1; 100];
3030
b.iter(|| {
3131
x.shuffle(&mut rng);
3232
x[0]
@@ -36,7 +36,7 @@ fn seq_shuffle_100(b: &mut Bencher) {
3636
#[bench]
3737
fn seq_slice_choose_1_of_1000(b: &mut Bencher) {
3838
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
39-
let x : &mut [usize] = &mut [1; 1000];
39+
let x: &mut [usize] = &mut [1; 1000];
4040
for i in 0..1000 {
4141
x[i] = i;
4242
}
@@ -55,19 +55,18 @@ macro_rules! seq_slice_choose_multiple {
5555
#[bench]
5656
fn $name(b: &mut Bencher) {
5757
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
58-
let x : &[i32] = &[$amount; $length];
58+
let x: &[i32] = &[$amount; $length];
5959
let mut result = [0i32; $amount];
6060
b.iter(|| {
6161
// Collect full result to prevent unwanted shortcuts getting
6262
// first element (in case sample_indices returns an iterator).
63-
for (slot, sample) in result.iter_mut().zip(
64-
x.choose_multiple(&mut rng, $amount)) {
63+
for (slot, sample) in result.iter_mut().zip(x.choose_multiple(&mut rng, $amount)) {
6564
*slot = *sample;
6665
}
67-
result[$amount-1]
66+
result[$amount - 1]
6867
})
6968
}
70-
}
69+
};
7170
}
7271

7372
seq_slice_choose_multiple!(seq_slice_choose_multiple_1_of_1000, 1, 1000);
@@ -78,7 +77,7 @@ seq_slice_choose_multiple!(seq_slice_choose_multiple_90_of_100, 90, 100);
7877
#[bench]
7978
fn seq_iter_choose_from_1000(b: &mut Bencher) {
8079
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
81-
let x : &mut [usize] = &mut [1; 1000];
80+
let x: &mut [usize] = &mut [1; 1000];
8281
for i in 0..1000 {
8382
x[i] = i;
8483
}
@@ -98,6 +97,7 @@ struct UnhintedIterator<I: Iterator + Clone> {
9897
}
9998
impl<I: Iterator + Clone> Iterator for UnhintedIterator<I> {
10099
type Item = I::Item;
100+
101101
fn next(&mut self) -> Option<Self::Item> {
102102
self.iter.next()
103103
}
@@ -110,9 +110,11 @@ struct WindowHintedIterator<I: ExactSizeIterator + Iterator + Clone> {
110110
}
111111
impl<I: ExactSizeIterator + Iterator + Clone> Iterator for WindowHintedIterator<I> {
112112
type Item = I::Item;
113+
113114
fn next(&mut self) -> Option<Self::Item> {
114115
self.iter.next()
115116
}
117+
116118
fn size_hint(&self) -> (usize, Option<usize>) {
117119
(std::cmp::min(self.iter.len(), self.window_size), None)
118120
}
@@ -121,50 +123,50 @@ impl<I: ExactSizeIterator + Iterator + Clone> Iterator for WindowHintedIterator<
121123
#[bench]
122124
fn seq_iter_unhinted_choose_from_1000(b: &mut Bencher) {
123125
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
124-
let x : &[usize] = &[1; 1000];
126+
let x: &[usize] = &[1; 1000];
125127
b.iter(|| {
126-
UnhintedIterator { iter: x.iter() }.choose(&mut rng).unwrap()
128+
UnhintedIterator { iter: x.iter() }
129+
.choose(&mut rng)
130+
.unwrap()
127131
})
128132
}
129133

130134
#[bench]
131135
fn seq_iter_window_hinted_choose_from_1000(b: &mut Bencher) {
132136
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
133-
let x : &[usize] = &[1; 1000];
137+
let x: &[usize] = &[1; 1000];
134138
b.iter(|| {
135-
WindowHintedIterator { iter: x.iter(), window_size: 7 }.choose(&mut rng)
139+
WindowHintedIterator {
140+
iter: x.iter(),
141+
window_size: 7,
142+
}
143+
.choose(&mut rng)
136144
})
137145
}
138146

139147
#[bench]
140148
fn seq_iter_choose_multiple_10_of_100(b: &mut Bencher) {
141149
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
142-
let x : &[usize] = &[1; 100];
143-
b.iter(|| {
144-
x.iter().cloned().choose_multiple(&mut rng, 10)
145-
})
150+
let x: &[usize] = &[1; 100];
151+
b.iter(|| x.iter().cloned().choose_multiple(&mut rng, 10))
146152
}
147153

148154
#[bench]
149155
fn seq_iter_choose_multiple_fill_10_of_100(b: &mut Bencher) {
150156
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
151-
let x : &[usize] = &[1; 100];
157+
let x: &[usize] = &[1; 100];
152158
let mut buf = [0; 10];
153-
b.iter(|| {
154-
x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf)
155-
})
159+
b.iter(|| x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf))
156160
}
157161

158162
macro_rules! sample_indices {
159163
($name:ident, $fn:ident, $amount:expr, $length:expr) => {
160164
#[bench]
161165
fn $name(b: &mut Bencher) {
162166
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
163-
b.iter(|| {
164-
index::$fn(&mut rng, $length, $amount)
165-
})
167+
b.iter(|| index::$fn(&mut rng, $length, $amount))
166168
}
167-
}
169+
};
168170
}
169171

170172
sample_indices!(misc_sample_indices_1_of_1k, sample, 1, 1000);

benches/weighted.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
extern crate test;
1212

13-
use test::Bencher;
14-
use rand::Rng;
1513
use rand::distributions::WeightedIndex;
14+
use rand::Rng;
15+
use test::Bencher;
1616

1717
#[bench]
1818
fn weighted_index_creation(b: &mut Bencher) {

examples/monte-carlo.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ fn main() {
3838
for _ in 0..total {
3939
let a = range.sample(&mut rng);
4040
let b = range.sample(&mut rng);
41-
if a*a + b*b <= 1.0 {
41+
if a * a + b * b <= 1.0 {
4242
in_circle += 1;
4343
}
4444
}
4545

4646
// prints something close to 3.14159...
47-
println!("π is approximately {}", 4. * (in_circle as f64) / (total as f64));
47+
println!(
48+
"π is approximately {}",
49+
4. * (in_circle as f64) / (total as f64)
50+
);
4851
}

examples/monty-hall.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ fn simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R) -> SimulationResult
5252
choice = switch_door(choice, open);
5353
}
5454

55-
SimulationResult { win: choice == car, switch }
55+
SimulationResult {
56+
win: choice == car,
57+
switch,
58+
}
5659
}
5760

5861
// Returns the door the game host opens given our choice and knowledge of
@@ -97,16 +100,24 @@ fn main() {
97100
let total_switches = switch_wins + switch_losses;
98101
let total_keeps = keep_wins + keep_losses;
99102

100-
println!("Switched door {} times with {} wins and {} losses",
101-
total_switches, switch_wins, switch_losses);
103+
println!(
104+
"Switched door {} times with {} wins and {} losses",
105+
total_switches, switch_wins, switch_losses
106+
);
102107

103-
println!("Kept our choice {} times with {} wins and {} losses",
104-
total_keeps, keep_wins, keep_losses);
108+
println!(
109+
"Kept our choice {} times with {} wins and {} losses",
110+
total_keeps, keep_wins, keep_losses
111+
);
105112

106113
// With a large number of simulations, the values should converge to
107114
// 0.667 and 0.333 respectively.
108-
println!("Estimated chance to win if we switch: {}",
109-
switch_wins as f32 / total_switches as f32);
110-
println!("Estimated chance to win if we don't: {}",
111-
keep_wins as f32 / total_keeps as f32);
115+
println!(
116+
"Estimated chance to win if we switch: {}",
117+
switch_wins as f32 / total_switches as f32
118+
);
119+
println!(
120+
"Estimated chance to win if we don't: {}",
121+
keep_wins as f32 / total_keeps as f32
122+
);
112123
}

rand_chacha/src/chacha.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,33 @@
88

99
//! The ChaCha random number generator.
1010
11-
#[cfg(feature = "std")]
12-
use std as core;
13-
#[cfg(not(feature = "std"))]
14-
use core;
11+
#[cfg(not(feature = "std"))] use core;
12+
#[cfg(feature = "std")] use std as core;
1513

16-
use c2_chacha::guts::ChaCha;
1714
use self::core::fmt;
15+
use c2_chacha::guts::ChaCha;
1816
use rand_core::block::{BlockRng, BlockRngCore};
1917
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
2018

2119
const STREAM_PARAM_NONCE: u32 = 1;
2220
const STREAM_PARAM_BLOCK: u32 = 0;
2321

2422
pub struct Array64<T>([T; 64]);
25-
impl<T> Default for Array64<T> where T: Default {
23+
impl<T> Default for Array64<T>
24+
where T: Default
25+
{
26+
#[rustfmt::skip]
2627
fn default() -> Self {
27-
Self([T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
28-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
29-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
30-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
31-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
32-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
33-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
34-
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default()])
28+
Self([
29+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
30+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
31+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
32+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
33+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
34+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
35+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
36+
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
37+
])
3538
}
3639
}
3740
impl<T> AsRef<[T]> for Array64<T> {
@@ -44,7 +47,9 @@ impl<T> AsMut<[T]> for Array64<T> {
4447
&mut self.0
4548
}
4649
}
47-
impl<T> Clone for Array64<T> where T: Copy + Default {
50+
impl<T> Clone for Array64<T>
51+
where T: Copy + Default
52+
{
4853
fn clone(&self) -> Self {
4954
let mut new = Self::default();
5055
new.0.copy_from_slice(&self.0);

0 commit comments

Comments
 (0)