Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply rustfmt formatting (partial) #926

Merged
merged 24 commits into from Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d3d7de4
Remove outdated rustfmt options and adjust settings
dhardy Jan 1, 2020
88aa915
rustfmt: use, mod statements and initial attributes
dhardy Jan 1, 2020
81a038d
incease inline_attribute_width
dhardy Jan 1, 2020
7a181de
rustfmt: initial attributes, mod and use statements
dhardy Jan 1, 2020
ccb64dc
rustfmt: attributes
dhardy Jan 1, 2020
477b470
rustfmt: spacing, new-lines, trailing commas/semicolon
dhardy Jan 1, 2020
88c7f5e
rustfmt: where clauses and super traits
dhardy Jan 1, 2020
4f1f0fc
rustfmt: struct literals, if/for blocks
dhardy Jan 2, 2020
3635715
rustfmt: method chaining
dhardy Jan 2, 2020
35080d4
rustfmt: struct literals, if/for blocks
dhardy Jan 2, 2020
1b3bcce
rustfmt: closures
dhardy Jan 2, 2020
fd872b6
rustfmt: arrays
dhardy Jan 2, 2020
079ff0d
rustfmt: function specifications
dhardy Jan 2, 2020
61317ff
rustfmt: macro parameters and definition
dhardy Jan 2, 2020
0f9500b
rustfmt: function parameters
dhardy Jan 2, 2020
92e0701
rustfmt: expression spacing (on the line)
dhardy Jan 2, 2020
e927d31
rustfmt: expression spacing across lines (excluding if conditions)
dhardy Jan 2, 2020
0b1694c
rustfmt: moving assoc. type and const definitions
dhardy Jan 2, 2020
e9ec7fc
rustfmt: if conditions (some exceptions)
dhardy Jan 2, 2020
b5af1f9
manual reformatting of if conditions
dhardy Jan 2, 2020
27ddcbb
rustfmt: extern "C"
dhardy Jan 2, 2020
3a51c3e
Manual array reformatting
dhardy Jan 2, 2020
3a5d5a8
Use rustfmt::skip for some cases
dhardy Jan 2, 2020
fab964d
Manual function specification reformatting
dhardy Jan 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions benches/generators.rs
Expand Up @@ -19,9 +19,9 @@ use test::{black_box, Bencher};

use rand::prelude::*;
use rand::rngs::adapter::ReseedingRng;
use rand::rngs::{OsRng, mock::StepRng};
use rand_chacha::{ChaCha20Core, ChaCha8Rng, ChaCha12Rng, ChaCha20Rng};
use rand_hc::{Hc128Rng};
use rand::rngs::{mock::StepRng, OsRng};
use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng};
use rand_hc::Hc128Rng;
use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg};

macro_rules! gen_bytes {
Expand All @@ -38,7 +38,7 @@ macro_rules! gen_bytes {
});
b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
}
}
};
}

gen_bytes!(gen_bytes_step, StepRng::new(0, 1));
Expand All @@ -50,7 +50,7 @@ gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_entropy());
gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_entropy());
gen_bytes!(gen_bytes_hc128, Hc128Rng::from_entropy());
gen_bytes!(gen_bytes_std, StdRng::from_entropy());
#[cfg(feature="small_rng")]
#[cfg(feature = "small_rng")]
gen_bytes!(gen_bytes_small, SmallRng::from_entropy());
gen_bytes!(gen_bytes_os, OsRng);

Expand All @@ -68,7 +68,7 @@ macro_rules! gen_uint {
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
}
};
}

gen_uint!(gen_u32_step, u32, StepRng::new(0, 1));
Expand All @@ -80,7 +80,7 @@ gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_entropy());
gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_entropy());
gen_uint!(gen_u32_hc128, u32, Hc128Rng::from_entropy());
gen_uint!(gen_u32_std, u32, StdRng::from_entropy());
#[cfg(feature="small_rng")]
#[cfg(feature = "small_rng")]
gen_uint!(gen_u32_small, u32, SmallRng::from_entropy());
gen_uint!(gen_u32_os, u32, OsRng);

Expand All @@ -93,7 +93,7 @@ gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_entropy());
gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_entropy());
gen_uint!(gen_u64_hc128, u64, Hc128Rng::from_entropy());
gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
#[cfg(feature="small_rng")]
#[cfg(feature = "small_rng")]
gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
gen_uint!(gen_u64_os, u64, OsRng);

Expand All @@ -107,7 +107,7 @@ macro_rules! init_gen {
r2
});
}
}
};
}

init_gen!(init_pcg32, Pcg32);
Expand All @@ -123,9 +123,7 @@ macro_rules! reseeding_bytes {
($fnn:ident, $thresh:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(),
$thresh * 1024,
OsRng);
let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(), $thresh * 1024, OsRng);
let mut buf = [0u8; RESEEDING_BYTES_LEN];
b.iter(|| {
for _ in 0..RESEEDING_BENCH_N {
Expand All @@ -135,7 +133,7 @@ macro_rules! reseeding_bytes {
});
b.bytes = RESEEDING_BYTES_LEN as u64 * RESEEDING_BENCH_N;
}
}
};
}

reseeding_bytes!(reseeding_chacha20_4k, 4);
Expand All @@ -160,7 +158,7 @@ macro_rules! threadrng_uint {
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
}
};
}

threadrng_uint!(thread_rng_u32, u32);
Expand Down
2 changes: 1 addition & 1 deletion benches/misc.rs
Expand Up @@ -14,8 +14,8 @@ const RAND_BENCH_N: u64 = 1000;

use test::Bencher;

use rand::distributions::{Bernoulli, Distribution, Standard};
use rand::prelude::*;
use rand::distributions::{Distribution, Standard, Bernoulli};
use rand_pcg::{Pcg32, Pcg64Mcg};

#[bench]
Expand Down
50 changes: 26 additions & 24 deletions benches/seq.rs
Expand Up @@ -26,7 +26,7 @@ const RAND_BENCH_N: u64 = 1000;
#[bench]
fn seq_shuffle_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &mut [usize] = &mut [1; 100];
let x: &mut [usize] = &mut [1; 100];
b.iter(|| {
x.shuffle(&mut rng);
x[0]
Expand All @@ -36,7 +36,7 @@ fn seq_shuffle_100(b: &mut Bencher) {
#[bench]
fn seq_slice_choose_1_of_1000(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &mut [usize] = &mut [1; 1000];
let x: &mut [usize] = &mut [1; 1000];
for i in 0..1000 {
x[i] = i;
}
Expand All @@ -55,19 +55,18 @@ macro_rules! seq_slice_choose_multiple {
#[bench]
fn $name(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[i32] = &[$amount; $length];
let x: &[i32] = &[$amount; $length];
let mut result = [0i32; $amount];
b.iter(|| {
// Collect full result to prevent unwanted shortcuts getting
// first element (in case sample_indices returns an iterator).
for (slot, sample) in result.iter_mut().zip(
x.choose_multiple(&mut rng, $amount)) {
for (slot, sample) in result.iter_mut().zip(x.choose_multiple(&mut rng, $amount)) {
*slot = *sample;
}
result[$amount-1]
result[$amount - 1]
})
}
}
};
}

seq_slice_choose_multiple!(seq_slice_choose_multiple_1_of_1000, 1, 1000);
Expand All @@ -78,7 +77,7 @@ seq_slice_choose_multiple!(seq_slice_choose_multiple_90_of_100, 90, 100);
#[bench]
fn seq_iter_choose_from_1000(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &mut [usize] = &mut [1; 1000];
let x: &mut [usize] = &mut [1; 1000];
for i in 0..1000 {
x[i] = i;
}
Expand All @@ -98,6 +97,7 @@ struct UnhintedIterator<I: Iterator + Clone> {
}
impl<I: Iterator + Clone> Iterator for UnhintedIterator<I> {
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
Expand All @@ -110,9 +110,11 @@ struct WindowHintedIterator<I: ExactSizeIterator + Iterator + Clone> {
}
impl<I: ExactSizeIterator + Iterator + Clone> Iterator for WindowHintedIterator<I> {
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}

fn size_hint(&self) -> (usize, Option<usize>) {
(std::cmp::min(self.iter.len(), self.window_size), None)
}
Expand All @@ -121,50 +123,50 @@ impl<I: ExactSizeIterator + Iterator + Clone> Iterator for WindowHintedIterator<
#[bench]
fn seq_iter_unhinted_choose_from_1000(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 1000];
let x: &[usize] = &[1; 1000];
b.iter(|| {
UnhintedIterator { iter: x.iter() }.choose(&mut rng).unwrap()
UnhintedIterator { iter: x.iter() }
.choose(&mut rng)
.unwrap()
})
}

#[bench]
fn seq_iter_window_hinted_choose_from_1000(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 1000];
let x: &[usize] = &[1; 1000];
b.iter(|| {
WindowHintedIterator { iter: x.iter(), window_size: 7 }.choose(&mut rng)
WindowHintedIterator {
iter: x.iter(),
window_size: 7,
}
.choose(&mut rng)
})
}

#[bench]
fn seq_iter_choose_multiple_10_of_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
x.iter().cloned().choose_multiple(&mut rng, 10)
})
let x: &[usize] = &[1; 100];
b.iter(|| x.iter().cloned().choose_multiple(&mut rng, 10))
}

#[bench]
fn seq_iter_choose_multiple_fill_10_of_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
let x: &[usize] = &[1; 100];
let mut buf = [0; 10];
b.iter(|| {
x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf)
})
b.iter(|| x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf))
}

macro_rules! sample_indices {
($name:ident, $fn:ident, $amount:expr, $length:expr) => {
#[bench]
fn $name(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
b.iter(|| {
index::$fn(&mut rng, $length, $amount)
})
b.iter(|| index::$fn(&mut rng, $length, $amount))
}
}
};
}

sample_indices!(misc_sample_indices_1_of_1k, sample, 1, 1000);
Expand Down
4 changes: 2 additions & 2 deletions benches/weighted.rs
Expand Up @@ -10,9 +10,9 @@

extern crate test;

use test::Bencher;
use rand::Rng;
use rand::distributions::WeightedIndex;
use rand::Rng;
use test::Bencher;

#[bench]
fn weighted_index_creation(b: &mut Bencher) {
Expand Down
7 changes: 5 additions & 2 deletions examples/monte-carlo.rs
Expand Up @@ -38,11 +38,14 @@ fn main() {
for _ in 0..total {
let a = range.sample(&mut rng);
let b = range.sample(&mut rng);
if a*a + b*b <= 1.0 {
if a * a + b * b <= 1.0 {
in_circle += 1;
}
}

// prints something close to 3.14159...
println!("π is approximately {}", 4. * (in_circle as f64) / (total as f64));
println!(
"π is approximately {}",
4. * (in_circle as f64) / (total as f64)
);
}
29 changes: 20 additions & 9 deletions examples/monty-hall.rs
Expand Up @@ -52,7 +52,10 @@ fn simulate<R: Rng>(random_door: &Uniform<u32>, rng: &mut R) -> SimulationResult
choice = switch_door(choice, open);
}

SimulationResult { win: choice == car, switch }
SimulationResult {
win: choice == car,
switch,
}
}

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

println!("Switched door {} times with {} wins and {} losses",
total_switches, switch_wins, switch_losses);
println!(
"Switched door {} times with {} wins and {} losses",
total_switches, switch_wins, switch_losses
);

println!("Kept our choice {} times with {} wins and {} losses",
total_keeps, keep_wins, keep_losses);
println!(
"Kept our choice {} times with {} wins and {} losses",
total_keeps, keep_wins, keep_losses
);

// With a large number of simulations, the values should converge to
// 0.667 and 0.333 respectively.
println!("Estimated chance to win if we switch: {}",
switch_wins as f32 / total_switches as f32);
println!("Estimated chance to win if we don't: {}",
keep_wins as f32 / total_keeps as f32);
println!(
"Estimated chance to win if we switch: {}",
switch_wins as f32 / total_switches as f32
);
println!(
"Estimated chance to win if we don't: {}",
keep_wins as f32 / total_keeps as f32
);
}
35 changes: 20 additions & 15 deletions rand_chacha/src/chacha.rs
Expand Up @@ -8,30 +8,33 @@

//! The ChaCha random number generator.

#[cfg(feature = "std")]
use std as core;
#[cfg(not(feature = "std"))]
use core;
#[cfg(not(feature = "std"))] use core;
#[cfg(feature = "std")] use std as core;

use c2_chacha::guts::ChaCha;
use self::core::fmt;
use c2_chacha::guts::ChaCha;
use rand_core::block::{BlockRng, BlockRngCore};
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};

const STREAM_PARAM_NONCE: u32 = 1;
const STREAM_PARAM_BLOCK: u32 = 0;

pub struct Array64<T>([T; 64]);
impl<T> Default for Array64<T> where T: Default {
impl<T> Default for Array64<T>
where T: Default
{
#[rustfmt::skip]
fn default() -> Self {
Self([T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default()])
Self([
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
])
}
}
impl<T> AsRef<[T]> for Array64<T> {
Expand All @@ -44,7 +47,9 @@ impl<T> AsMut<[T]> for Array64<T> {
&mut self.0
}
}
impl<T> Clone for Array64<T> where T: Copy + Default {
impl<T> Clone for Array64<T>
where T: Copy + Default
{
fn clone(&self) -> Self {
let mut new = Self::default();
new.0.copy_from_slice(&self.0);
Expand Down