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 1 commit
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
3 changes: 1 addition & 2 deletions benches/seq.rs
Expand Up @@ -60,8 +60,7 @@ 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]
Expand Down
18 changes: 9 additions & 9 deletions rand_core/src/block.rs
Expand Up @@ -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 @@ -295,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
4 changes: 3 additions & 1 deletion rand_distr/benches/distributions.rs
Expand Up @@ -94,7 +94,9 @@ macro_rules! distr_duration {
let mut accum = Duration::new(0, 0);
for _ in 0..RAND_BENCH_N {
let x: Duration = distr.sample(&mut rng);
accum = accum.checked_add(x).unwrap_or(Duration::new(u64::max_value(), 999_999_999));
accum = accum
.checked_add(x)
.unwrap_or(Duration::new(u64::max_value(), 999_999_999));
}
accum
});
Expand Down
6 changes: 2 additions & 4 deletions rand_distr/src/gamma.rs
Expand Up @@ -479,10 +479,8 @@ where
/// distribution.
pub fn new(alpha: N, beta: N) -> Result<Beta<N>, BetaError> {
Ok(Beta {
gamma_a: Gamma::new(alpha, N::from(1.))
.map_err(|_| BetaError::AlphaTooSmall)?,
gamma_b: Gamma::new(beta, N::from(1.))
.map_err(|_| BetaError::BetaTooSmall)?,
gamma_a: Gamma::new(alpha, N::from(1.)).map_err(|_| BetaError::AlphaTooSmall)?,
gamma_b: Gamma::new(beta, N::from(1.)).map_err(|_| BetaError::BetaTooSmall)?,
})
}
}
Expand Down
10 changes: 7 additions & 3 deletions rand_distr/tests/uniformity.rs
Expand Up @@ -25,9 +25,13 @@ fn unit_sphere() {
for _ in 0..N_SAMPLES {
let v: [f64; 3] = dist.sample(&mut rng);
for i in 0..N_DIM {
histograms[i].add(v[i]).map_err(
|e| { println!("v: {}", v[i]); e }
).unwrap();
histograms[i]
.add(v[i])
.map_err(|e| {
println!("v: {}", v[i]);
e
})
.unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this decreases readability, but I guess it's good enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess you're right. Feel free to post rustfmt config changes / a skip annotation if you prefer.

I'd really like to get the point where we can just run cargo fmt on the project so would prefer not to just revert stuff like this — but it may not be until Rustfmt 2.0 that we get there (and even then, we'll have to make further compromises).

}
}
for h in &histograms {
Expand Down
30 changes: 19 additions & 11 deletions rand_hc/src/hc128.rs
Expand Up @@ -184,9 +184,10 @@ impl Hc128Core {
let temp0 = p.get_unchecked(i511).rotate_right(23);
let temp1 = p.get_unchecked(i3).rotate_right(10);
let temp2 = p.get_unchecked(i10).rotate_right(8);
*p.get_unchecked_mut(i) = p.get_unchecked(i)
.wrapping_add(temp2)
.wrapping_add(temp0 ^ temp1);
*p.get_unchecked_mut(i) = p
.get_unchecked(i)
.wrapping_add(temp2)
.wrapping_add(temp0 ^ temp1);
let temp3 = {
// The h1 function in HC-128
let a = *p.get_unchecked(i12) as u8;
Expand All @@ -209,9 +210,10 @@ impl Hc128Core {
let temp0 = q.get_unchecked(i511).rotate_left(23);
let temp1 = q.get_unchecked(i3).rotate_left(10);
let temp2 = q.get_unchecked(i10).rotate_left(8);
*q.get_unchecked_mut(i) = q.get_unchecked(i)
.wrapping_add(temp2)
.wrapping_add(temp0 ^ temp1);
*q.get_unchecked_mut(i) = q
.get_unchecked(i)
.wrapping_add(temp2)
.wrapping_add(temp0 ^ temp1);
let temp3 = {
// The h2 function in HC-128
let a = *q.get_unchecked(i12) as u8;
Expand Down Expand Up @@ -295,9 +297,12 @@ impl Hc128Core {

// Generate the 256 intermediate values W[16] ... W[256+16-1], and
// copy the last 16 generated values to the start op P.
for i in 16..256+16 {
t[i] = f2(t[i-2]).wrapping_add(t[i-7]).wrapping_add(f1(t[i-15]))
.wrapping_add(t[i-16]).wrapping_add(i as u32);
for i in 16..256 + 16 {
t[i] = f2(t[i - 2])
.wrapping_add(t[i - 7])
.wrapping_add(f1(t[i - 15]))
.wrapping_add(t[i - 16])
.wrapping_add(i as u32);
}
{
let (p1, p2) = t.split_at_mut(256);
Expand All @@ -306,8 +311,11 @@ impl Hc128Core {

// Generate both the P and Q tables
for i in 16..1024 {
t[i] = f2(t[i-2]).wrapping_add(t[i-7]).wrapping_add(f1(t[i-15]))
.wrapping_add(t[i-16]).wrapping_add(256 + i as u32);
t[i] = f2(t[i - 2])
.wrapping_add(t[i - 7])
.wrapping_add(f1(t[i - 15]))
.wrapping_add(t[i - 16])
.wrapping_add(256 + i as u32);
}

let mut core = Self { t, counter1024: 0 };
Expand Down
3 changes: 2 additions & 1 deletion rand_pcg/src/pcg128.rs
Expand Up @@ -64,7 +64,8 @@ impl Lcg128Xsl64 {
#[inline]
fn step(&mut self) {
// prepare the LCG for the next round
self.state = self.state
self.state = self
.state
.wrapping_mul(MULTIPLIER)
.wrapping_add(self.increment);
}
Expand Down
3 changes: 2 additions & 1 deletion rand_pcg/src/pcg64.rs
Expand Up @@ -65,7 +65,8 @@ impl Lcg64Xsh32 {
#[inline]
fn step(&mut self) {
// prepare the LCG for the next round
self.state = self.state
self.state = self
.state
.wrapping_mul(MULTIPLIER)
.wrapping_add(self.increment);
}
Expand Down
4 changes: 2 additions & 2 deletions rand_pcg/tests/lcg128xsl64.rs
Expand Up @@ -48,8 +48,8 @@ fn test_lcg128xsl64_serde() {

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: Lcg128Xsl64 = bincode::deserialize_from(&mut read)
.expect("Could not deserialize");
let mut deserialized: Lcg128Xsl64 =
bincode::deserialize_from(&mut read).expect("Could not deserialize");

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
Expand Down
4 changes: 2 additions & 2 deletions rand_pcg/tests/lcg64xsh32.rs
Expand Up @@ -47,8 +47,8 @@ fn test_lcg64xsh32_serde() {

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: Lcg64Xsh32 = bincode::deserialize_from(&mut read)
.expect("Could not deserialize");
let mut deserialized: Lcg64Xsh32 =
bincode::deserialize_from(&mut read).expect("Could not deserialize");

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
Expand Down
4 changes: 2 additions & 2 deletions rand_pcg/tests/mcg128xsl64.rs
Expand Up @@ -47,8 +47,8 @@ fn test_mcg128xsl64_serde() {

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: Mcg128Xsl64 = bincode::deserialize_from(&mut read)
.expect("Could not deserialize");
let mut deserialized: Mcg128Xsl64 =
bincode::deserialize_from(&mut read).expect("Could not deserialize");

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
Expand Down
4 changes: 3 additions & 1 deletion src/distributions/other.rs
Expand Up @@ -202,7 +202,9 @@ mod tests {
// Test by generating a relatively large number of chars, so we also
// take the rejection sampling path.
let word: String = iter::repeat(())
.map(|()| rng.gen::<char>()).take(1000).collect();
.map(|()| rng.gen::<char>())
.take(1000)
.collect();
assert!(word.len() != 0);
}

Expand Down
17 changes: 9 additions & 8 deletions src/distributions/uniform.rs
Expand Up @@ -666,8 +666,9 @@ macro_rules! uniform_float_impl {
"Uniform::new called with `low >= high`");
assert!(low.all_finite() && high.all_finite(),
"Uniform::new called with non-finite boundaries");
let max_rand = <$ty>::splat((::core::$u_scalar::MAX >> $bits_to_discard)
.into_float_with_exponent(0) - 1.0);
let max_rand = <$ty>::splat(
(::core::$u_scalar::MAX >> $bits_to_discard).into_float_with_exponent(0) - 1.0,
);

let mut scale = high - low;

Expand Down Expand Up @@ -695,8 +696,9 @@ macro_rules! uniform_float_impl {
"Uniform::new_inclusive called with `low > high`");
assert!(low.all_finite() && high.all_finite(),
"Uniform::new_inclusive called with non-finite boundaries");
let max_rand = <$ty>::splat((::core::$u_scalar::MAX >> $bits_to_discard)
.into_float_with_exponent(0) - 1.0);
let max_rand = <$ty>::splat(
(::core::$u_scalar::MAX >> $bits_to_discard).into_float_with_exponent(0) - 1.0,
);

let mut scale = (high - low) / max_rand;

Expand All @@ -715,8 +717,7 @@ macro_rules! uniform_float_impl {

fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
// Generate a value in the range [1, 2)
let value1_2 = (rng.gen::<$uty>() >> $bits_to_discard)
.into_float_with_exponent(0);
let value1_2 = (rng.gen::<$uty>() >> $bits_to_discard).into_float_with_exponent(0);

// Get a value in the range [0, 1) in order to avoid
// overflowing into infinity when multiplying with scale
Expand Down Expand Up @@ -744,8 +745,8 @@ macro_rules! uniform_float_impl {

loop {
// Generate a value in the range [1, 2)
let value1_2 = (rng.gen::<$uty>() >> $bits_to_discard)
.into_float_with_exponent(0);
let value1_2 =
(rng.gen::<$uty>() >> $bits_to_discard).into_float_with_exponent(0);

// Get a value in the range [0, 1) in order to avoid
// overflowing into infinity when multiplying with scale
Expand Down
16 changes: 10 additions & 6 deletions src/distributions/weighted/mod.rs
Expand Up @@ -104,10 +104,7 @@ impl<X: SampleUniform + PartialOrd> WeightedIndex<X> {
X: for<'a> ::core::ops::AddAssign<&'a X> + Clone + Default,
{
let mut iter = weights.into_iter();
let mut total_weight: X = iter.next()
.ok_or(WeightedError::NoItem)?
.borrow()
.clone();
let mut total_weight: X = iter.next().ok_or(WeightedError::NoItem)?.borrow().clone();

let zero = <X as Default>::default();
if total_weight < zero {
Expand Down Expand Up @@ -232,8 +229,15 @@ where X: SampleUniform + PartialOrd
use ::core::cmp::Ordering;
let chosen_weight = self.weight_distribution.sample(rng);
// Find the first item which has a weight *higher* than the chosen weight.
self.cumulative_weights.binary_search_by(
|w| if *w <= chosen_weight { Ordering::Less } else { Ordering::Greater }).unwrap_err()
self.cumulative_weights
.binary_search_by(|w| {
if *w <= chosen_weight {
Ordering::Less
} else {
Ordering::Greater
}
})
.unwrap_err()
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/rngs/adapter/read.rs
Expand Up @@ -72,7 +72,9 @@ impl<R: Read> RngCore for ReadRng<R> {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
if dest.is_empty() { return Ok(()); }
// Use `std::io::read_exact`, which retries on `ErrorKind::Interrupted`.
self.reader.read_exact(dest).map_err(|e| Error::new(ReadError(e)))
self.reader
.read_exact(dest)
.map_err(|e| Error::new(ReadError(e)))
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/seq/index.rs
Expand Up @@ -378,12 +378,10 @@ mod test {
assert_eq!(sample_floyd(&mut r, 1, 1).into_vec(), vec![0]);

// These algorithms should be fast with big numbers. Test average.
let sum: usize = sample_rejection(&mut r, 1 << 25, 10u32)
.into_iter().sum();
let sum: usize = sample_rejection(&mut r, 1 << 25, 10u32).into_iter().sum();
assert!(1 << 25 < sum && sum < (1 << 25) * 25);

let sum: usize = sample_floyd(&mut r, 1 << 25, 10)
.into_iter().sum();
let sum: usize = sample_floyd(&mut r, 1 << 25, 10).into_iter().sum();
assert!(1 << 25 < sum && sum < (1 << 25) * 25);
}

Expand Down
6 changes: 3 additions & 3 deletions src/seq/mod.rs
Expand Up @@ -750,9 +750,9 @@ mod test {
// no randomization happens when amount >= len
assert_eq!(large_sample, vals.iter().collect::<Vec<_>>());

assert!(small_sample.iter().all(|e| {
**e >= min_val && **e <= max_val
}));
assert!(small_sample
.iter()
.all(|e| { **e >= min_val && **e <= max_val }));
}

#[test]
Expand Down