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 9 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
22 changes: 11 additions & 11 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 Down Expand Up @@ -135,7 +135,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 +160,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
20 changes: 14 additions & 6 deletions benches/seq.rs
Expand Up @@ -60,14 +60,13 @@ macro_rules! seq_slice_choose_multiple {
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]
})
}
}
};
}

seq_slice_choose_multiple!(seq_slice_choose_multiple_1_of_1000, 1, 1000);
Expand Down Expand Up @@ -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 @@ -123,7 +125,9 @@ fn seq_iter_unhinted_choose_from_1000(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 1000];
b.iter(|| {
UnhintedIterator { iter: x.iter() }.choose(&mut rng).unwrap()
UnhintedIterator { iter: x.iter() }
.choose(&mut rng)
.unwrap()
})
}

Expand All @@ -132,7 +136,11 @@ 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];
b.iter(|| {
WindowHintedIterator { iter: x.iter(), window_size: 7 }.choose(&mut rng)
WindowHintedIterator {
iter: x.iter(),
window_size: 7,
}
.choose(&mut rng)
})
}

Expand Down Expand Up @@ -164,7 +172,7 @@ macro_rules! sample_indices {
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
5 changes: 4 additions & 1 deletion 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
16 changes: 9 additions & 7 deletions rand_chacha/src/chacha.rs
Expand Up @@ -8,21 +8,21 @@

//! 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
{
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(),
Expand All @@ -44,7 +44,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
14 changes: 8 additions & 6 deletions rand_chacha/src/lib.rs
Expand Up @@ -8,21 +8,23 @@

//! The ChaCha random number generator.

#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://rust-random.github.io/rand/")]

#![doc(
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://rust-random.github.io/rand/"
)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]

#![cfg_attr(not(feature = "std"), no_std)]

pub use rand_core;

mod chacha;

pub use crate::chacha::{ChaCha12Core, ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Core, ChaCha8Rng};
pub use crate::chacha::{
ChaCha12Core, ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Core, ChaCha8Rng,
};

/// ChaCha with 20 rounds
pub type ChaChaRng = ChaCha20Rng;
Expand Down
31 changes: 15 additions & 16 deletions rand_core/src/block.rs
Expand Up @@ -50,11 +50,11 @@
//! [`BlockRngCore`]: crate::block::BlockRngCore
//! [`fill_bytes`]: RngCore::fill_bytes

use crate::impls::{fill_via_u32_chunks, fill_via_u64_chunks};
use crate::{CryptoRng, Error, RngCore, SeedableRng};
use core::convert::AsRef;
use core::{fmt, ptr};
#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
use crate::{RngCore, CryptoRng, SeedableRng, Error};
use crate::impls::{fill_via_u32_chunks, fill_via_u64_chunks};
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};

/// A trait for RNGs which do not generate random numbers individually, but in
/// blocks (typically `[u32; N]`). This technique is commonly used by
Expand Down Expand Up @@ -110,7 +110,7 @@ pub trait BlockRngCore {
/// [`fill_bytes`]: RngCore::fill_bytes
/// [`try_fill_bytes`]: RngCore::try_fill_bytes
#[derive(Clone)]
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct BlockRng<R: BlockRngCore + ?Sized> {
results: R::Results,
index: usize,
Expand All @@ -122,10 +122,10 @@ pub struct BlockRng<R: BlockRngCore + ?Sized> {
impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng<R> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BlockRng")
.field("core", &self.core)
.field("result_len", &self.results.as_ref().len())
.field("index", &self.index)
.finish()
.field("core", &self.core)
.field("result_len", &self.results.as_ref().len())
.field("index", &self.index)
.finish()
}
}

Expand Down Expand Up @@ -188,7 +188,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
let read_u64 = |results: &[u32], index| {
if cfg!(any(target_endian = "little")) {
// requires little-endian CPU
#[allow(clippy::cast_ptr_alignment)] // false positive
#[allow(clippy::cast_ptr_alignment)] // false positive
let ptr: *const u64 = results[index..=index+1].as_ptr() as *const u64;
unsafe { ptr::read_unaligned(ptr) }
} else {
Expand Down Expand Up @@ -259,7 +259,6 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
}



/// A wrapper type implementing [`RngCore`] for some type implementing
/// [`BlockRngCore`] with `u64` array buffer; i.e. this can be used to implement
/// a full RNG from just a `generate` function.
Expand All @@ -283,7 +282,7 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
/// [`fill_bytes`]: RngCore::fill_bytes
/// [`try_fill_bytes`]: RngCore::try_fill_bytes
#[derive(Clone)]
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct BlockRng64<R: BlockRngCore + ?Sized> {
results: R::Results,
index: usize,
Expand All @@ -296,11 +295,11 @@ pub struct BlockRng64<R: BlockRngCore + ?Sized> {
impl<R: BlockRngCore + fmt::Debug> fmt::Debug for BlockRng64<R> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BlockRng64")
.field("core", &self.core)
.field("result_len", &self.results.as_ref().len())
.field("index", &self.index)
.field("half_used", &self.half_used)
.finish()
.field("core", &self.core)
.field("result_len", &self.results.as_ref().len())
.field("index", &self.index)
.field("half_used", &self.half_used)
.finish()
}
}

Expand Down