Skip to content

Commit 9ed32bb

Browse files
committed
Auto merge of #29117 - mseri:patch-5a, r=nrc
r? @nrc Re-submission of the closed PR #29054 with the additional rustfmt-zation of the full librand.
2 parents d3f4978 + 8a0b9c0 commit 9ed32bb

File tree

10 files changed

+261
-145
lines changed

10 files changed

+261
-145
lines changed

src/librand/chacha.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ const CHACHA_ROUNDS: usize = 20; // Cryptographically secure from 8 upwards as o
2727
/// Salsa20*](http://cr.yp.to/chacha.html)
2828
#[derive(Copy, Clone)]
2929
pub struct ChaChaRng {
30-
buffer: [u32; STATE_WORDS], // Internal buffer of output
31-
state: [u32; STATE_WORDS], // Initial state
32-
index: usize, // Index into state
30+
buffer: [u32; STATE_WORDS], // Internal buffer of output
31+
state: [u32; STATE_WORDS], // Initial state
32+
index: usize, // Index into state
3333
}
3434

3535
static EMPTY: ChaChaRng = ChaChaRng {
36-
buffer: [0; STATE_WORDS],
37-
state: [0; STATE_WORDS],
38-
index: STATE_WORDS
36+
buffer: [0; STATE_WORDS],
37+
state: [0; STATE_WORDS],
38+
index: STATE_WORDS,
3939
};
4040

4141

@@ -95,9 +95,9 @@ impl ChaChaRng {
9595
/// associated with a particular nonce can call this function with
9696
/// arguments `0, desired_nonce`.
9797
pub fn set_counter(&mut self, counter_low: u64, counter_high: u64) {
98-
self.state[12] = (counter_low >> 0) as u32;
98+
self.state[12] = (counter_low >> 0) as u32;
9999
self.state[13] = (counter_low >> 32) as u32;
100-
self.state[14] = (counter_high >> 0) as u32;
100+
self.state[14] = (counter_high >> 0) as u32;
101101
self.state[15] = (counter_high >> 32) as u32;
102102
self.index = STATE_WORDS; // force recomputation
103103
}
@@ -127,7 +127,7 @@ impl ChaChaRng {
127127
self.state[3] = 0x6B206574;
128128

129129
for i in 0..KEY_WORDS {
130-
self.state[4+i] = key[i];
130+
self.state[4 + i] = key[i];
131131
}
132132

133133
self.state[12] = 0;
@@ -144,11 +144,17 @@ impl ChaChaRng {
144144
self.index = 0;
145145
// update 128-bit counter
146146
self.state[12] += 1;
147-
if self.state[12] != 0 { return };
147+
if self.state[12] != 0 {
148+
return;
149+
}
148150
self.state[13] += 1;
149-
if self.state[13] != 0 { return };
151+
if self.state[13] != 0 {
152+
return;
153+
}
150154
self.state[14] += 1;
151-
if self.state[14] != 0 { return };
155+
if self.state[14] != 0 {
156+
return;
157+
}
152158
self.state[15] += 1;
153159
}
154160
}
@@ -172,7 +178,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
172178
// reset state
173179
self.init(&[0; KEY_WORDS]);
174180
// set key in place
175-
let key = &mut self.state[4 .. 4+KEY_WORDS];
181+
let key = &mut self.state[4..4 + KEY_WORDS];
176182
for (k, s) in key.iter_mut().zip(seed) {
177183
*k = *s;
178184
}
@@ -191,7 +197,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
191197

192198
impl Rand for ChaChaRng {
193199
fn rand<R: Rng>(other: &mut R) -> ChaChaRng {
194-
let mut key : [u32; KEY_WORDS] = [0; KEY_WORDS];
200+
let mut key: [u32; KEY_WORDS] = [0; KEY_WORDS];
195201
for word in &mut key {
196202
*word = other.gen();
197203
}
@@ -219,7 +225,7 @@ mod tests {
219225

220226
#[test]
221227
fn test_rng_seeded() {
222-
let seed : &[_] = &[0,1,2,3,4,5,6,7];
228+
let seed: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
223229
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);
224230
let mut rb: ChaChaRng = SeedableRng::from_seed(seed);
225231
assert!(order::equals(ra.gen_ascii_chars().take(100),
@@ -239,10 +245,11 @@ mod tests {
239245
}
240246

241247
#[test]
248+
#[rustfmt_skip]
242249
fn test_rng_true_values() {
243250
// Test vectors 1 and 2 from
244251
// http://tools.ietf.org/html/draft-nir-cfrg-chacha20-poly1305-04
245-
let seed : &[_] = &[0; 8];
252+
let seed: &[_] = &[0; 8];
246253
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);
247254

248255
let v = (0..16).map(|_| ra.next_u32()).collect::<Vec<_>>();
@@ -260,12 +267,12 @@ mod tests {
260267
0x281fed31, 0x45fb0a51, 0x1f0ae1ac, 0x6f4d794b));
261268

262269

263-
let seed : &[_] = &[0,1,2,3,4,5,6,7];
270+
let seed: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
264271
let mut ra: ChaChaRng = SeedableRng::from_seed(seed);
265272

266273
// Store the 17*i-th 32-bit word,
267274
// i.e., the i-th word of the i-th 16-word block
268-
let mut v : Vec<u32> = Vec::new();
275+
let mut v: Vec<u32> = Vec::new();
269276
for _ in 0..16 {
270277
v.push(ra.next_u32());
271278
for _ in 0..16 {
@@ -282,7 +289,7 @@ mod tests {
282289

283290
#[test]
284291
fn test_rng_clone() {
285-
let seed : &[_] = &[0; 8];
292+
let seed: &[_] = &[0; 8];
286293
let mut rng: ChaChaRng = SeedableRng::from_seed(seed);
287294
let mut clone = rng.clone();
288295
for _ in 0..16 {

src/librand/distributions/exponential.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,22 @@ pub struct Exp1(pub f64);
3535
// This could be done via `-rng.gen::<f64>().ln()` but that is slower.
3636
impl Rand for Exp1 {
3737
#[inline]
38-
fn rand<R:Rng>(rng: &mut R) -> Exp1 {
38+
fn rand<R: Rng>(rng: &mut R) -> Exp1 {
3939
#[inline]
4040
fn pdf(x: f64) -> f64 {
4141
(-x).exp()
4242
}
4343
#[inline]
44-
fn zero_case<R:Rng>(rng: &mut R, _u: f64) -> f64 {
44+
fn zero_case<R: Rng>(rng: &mut R, _u: f64) -> f64 {
4545
ziggurat_tables::ZIG_EXP_R - rng.gen::<f64>().ln()
4646
}
4747

48-
Exp1(ziggurat(rng, false,
48+
Exp1(ziggurat(rng,
49+
false,
4950
&ziggurat_tables::ZIG_EXP_X,
5051
&ziggurat_tables::ZIG_EXP_F,
51-
pdf, zero_case))
52+
pdf,
53+
zero_case))
5254
}
5355
}
5456

@@ -59,7 +61,7 @@ impl Rand for Exp1 {
5961
#[derive(Copy, Clone)]
6062
pub struct Exp {
6163
/// `lambda` stored as `1/lambda`, since this is what we scale by.
62-
lambda_inverse: f64
64+
lambda_inverse: f64,
6365
}
6466

6567
impl Exp {
@@ -72,7 +74,9 @@ impl Exp {
7274
}
7375

7476
impl Sample<f64> for Exp {
75-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
77+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
78+
self.ind_sample(rng)
79+
}
7680
}
7781
impl IndependentSample<f64> for Exp {
7882
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {

src/librand/distributions/gamma.rs

+32-20
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct Gamma {
4646
enum GammaRepr {
4747
Large(GammaLargeShape),
4848
One(Exp),
49-
Small(GammaSmallShape)
49+
Small(GammaSmallShape),
5050
}
5151

5252
// These two helpers could be made public, but saving the
@@ -65,7 +65,7 @@ enum GammaRepr {
6565
/// shape parameters.
6666
struct GammaSmallShape {
6767
inv_shape: f64,
68-
large_shape: GammaLargeShape
68+
large_shape: GammaLargeShape,
6969
}
7070

7171
/// Gamma distribution where the shape parameter is larger than 1.
@@ -75,7 +75,7 @@ struct GammaSmallShape {
7575
struct GammaLargeShape {
7676
scale: f64,
7777
c: f64,
78-
d: f64
78+
d: f64,
7979
}
8080

8181
impl Gamma {
@@ -88,9 +88,9 @@ impl Gamma {
8888
assert!(scale > 0.0, "Gamma::new called with scale <= 0");
8989

9090
let repr = match shape {
91-
1.0 => One(Exp::new(1.0 / scale)),
91+
1.0 => One(Exp::new(1.0 / scale)),
9292
0.0 ... 1.0 => Small(GammaSmallShape::new_raw(shape, scale)),
93-
_ => Large(GammaLargeShape::new_raw(shape, scale))
93+
_ => Large(GammaLargeShape::new_raw(shape, scale)),
9494
};
9595
Gamma { repr: repr }
9696
}
@@ -100,7 +100,7 @@ impl GammaSmallShape {
100100
fn new_raw(shape: f64, scale: f64) -> GammaSmallShape {
101101
GammaSmallShape {
102102
inv_shape: 1. / shape,
103-
large_shape: GammaLargeShape::new_raw(shape + 1.0, scale)
103+
large_shape: GammaLargeShape::new_raw(shape + 1.0, scale),
104104
}
105105
}
106106
}
@@ -111,19 +111,25 @@ impl GammaLargeShape {
111111
GammaLargeShape {
112112
scale: scale,
113113
c: 1. / (9. * d).sqrt(),
114-
d: d
114+
d: d,
115115
}
116116
}
117117
}
118118

119119
impl Sample<f64> for Gamma {
120-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
120+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
121+
self.ind_sample(rng)
122+
}
121123
}
122124
impl Sample<f64> for GammaSmallShape {
123-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
125+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
126+
self.ind_sample(rng)
127+
}
124128
}
125129
impl Sample<f64> for GammaLargeShape {
126-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
130+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
131+
self.ind_sample(rng)
132+
}
127133
}
128134

129135
impl IndependentSample<f64> for Gamma {
@@ -148,16 +154,16 @@ impl IndependentSample<f64> for GammaLargeShape {
148154
let StandardNormal(x) = rng.gen::<StandardNormal>();
149155
let v_cbrt = 1.0 + self.c * x;
150156
if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0
151-
continue
157+
continue;
152158
}
153159

154160
let v = v_cbrt * v_cbrt * v_cbrt;
155161
let Open01(u) = rng.gen::<Open01<f64>>();
156162

157163
let x_sqr = x * x;
158164
if u < 1.0 - 0.0331 * x_sqr * x_sqr ||
159-
u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) {
160-
return self.d * v * self.scale
165+
u.ln() < 0.5 * x_sqr + self.d * (1.0 - v + v.ln()) {
166+
return self.d * v * self.scale;
161167
}
162168
}
163169
}
@@ -196,7 +202,9 @@ impl ChiSquared {
196202
}
197203
}
198204
impl Sample<f64> for ChiSquared {
199-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
205+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
206+
self.ind_sample(rng)
207+
}
200208
}
201209
impl IndependentSample<f64> for ChiSquared {
202210
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
@@ -206,7 +214,7 @@ impl IndependentSample<f64> for ChiSquared {
206214
let StandardNormal(norm) = rng.gen::<StandardNormal>();
207215
norm * norm
208216
}
209-
DoFAnythingElse(ref g) => g.ind_sample(rng)
217+
DoFAnythingElse(ref g) => g.ind_sample(rng),
210218
}
211219
}
212220
}
@@ -234,12 +242,14 @@ impl FisherF {
234242
FisherF {
235243
numer: ChiSquared::new(m),
236244
denom: ChiSquared::new(n),
237-
dof_ratio: n / m
245+
dof_ratio: n / m,
238246
}
239247
}
240248
}
241249
impl Sample<f64> for FisherF {
242-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
250+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
251+
self.ind_sample(rng)
252+
}
243253
}
244254
impl IndependentSample<f64> for FisherF {
245255
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
@@ -251,7 +261,7 @@ impl IndependentSample<f64> for FisherF {
251261
/// freedom.
252262
pub struct StudentT {
253263
chi: ChiSquared,
254-
dof: f64
264+
dof: f64,
255265
}
256266

257267
impl StudentT {
@@ -261,12 +271,14 @@ impl StudentT {
261271
assert!(n > 0.0, "StudentT::new called with `n <= 0`");
262272
StudentT {
263273
chi: ChiSquared::new(n),
264-
dof: n
274+
dof: n,
265275
}
266276
}
267277
}
268278
impl Sample<f64> for StudentT {
269-
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
279+
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 {
280+
self.ind_sample(rng)
281+
}
270282
}
271283
impl IndependentSample<f64> for StudentT {
272284
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {

0 commit comments

Comments
 (0)