Skip to content

Commit

Permalink
Apply rustfmt and fix Clippy warnings (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov authored May 9, 2024
1 parent e937769 commit 1b762b2
Show file tree
Hide file tree
Showing 64 changed files with 1,537 additions and 956 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/benches.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Benches

on:
pull_request:
paths:
- ".github/workflows/benches.yml"
- "benches/**"

jobs:
benches:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
components: clippy, rustfmt
- name: Rustfmt
run: cargo fmt --all -- --check
- name: Clippy
run: cargo clippy --all --all-targets -- -D warnings
- name: Build
run: RUSTFLAGS=-Dwarnings cargo build --all-targets
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ jobs:
if: ${{ matrix.variant == 'minimal_versions' }}
run: |
cargo generate-lockfile -Z minimal-versions
# Overrides for dependencies with incorrect requirements (may need periodic updating)
cargo update -p regex --precise 1.5.1
- name: Maybe nightly
if: ${{ matrix.toolchain == 'nightly' }}
run: |
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/workspace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Workspace

on:
pull_request:
paths-ignore:
- README.md
- "benches/**"
push:
branches: master
paths-ignore:
- README.md
- "benches/**"

jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: 1.78.0
components: clippy
- run: cargo clippy --all --all-targets -- -D warnings

rustfmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: rustfmt
- run: cargo fmt --all -- --check
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ unbiased = []

[workspace]
members = [
"benches",
"rand_core",
"rand_distr",
"rand_chacha",
"rand_pcg",
]
exclude = ["benches"]

[dependencies]
rand_core = { path = "rand_core", version = "=0.9.0-alpha.1", default-features = false }
Expand Down
3 changes: 0 additions & 3 deletions benches/benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ gen_bytes!(gen_bytes_chacha8, ChaCha8Rng::from_os_rng());
gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_os_rng());
gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_os_rng());
gen_bytes!(gen_bytes_std, StdRng::from_os_rng());
#[cfg(feature = "small_rng")]
gen_bytes!(gen_bytes_small, SmallRng::from_thread_rng());
gen_bytes!(gen_bytes_os, UnwrapErr(OsRng));
gen_bytes!(gen_bytes_thread, thread_rng());
Expand Down Expand Up @@ -81,7 +80,6 @@ gen_uint!(gen_u32_chacha8, u32, ChaCha8Rng::from_os_rng());
gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_os_rng());
gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_os_rng());
gen_uint!(gen_u32_std, u32, StdRng::from_os_rng());
#[cfg(feature = "small_rng")]
gen_uint!(gen_u32_small, u32, SmallRng::from_thread_rng());
gen_uint!(gen_u32_os, u32, UnwrapErr(OsRng));
gen_uint!(gen_u32_thread, u32, thread_rng());
Expand All @@ -95,7 +93,6 @@ gen_uint!(gen_u64_chacha8, u64, ChaCha8Rng::from_os_rng());
gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_os_rng());
gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_os_rng());
gen_uint!(gen_u64_std, u64, StdRng::from_os_rng());
#[cfg(feature = "small_rng")]
gen_uint!(gen_u64_small, u64, SmallRng::from_thread_rng());
gen_uint!(gen_u64_os, u64, UnwrapErr(OsRng));
gen_uint!(gen_u64_thread, u64, thread_rng());
Expand Down
26 changes: 18 additions & 8 deletions rand_chacha/src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

//! The ChaCha random number generator.

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

use self::core::fmt;
use crate::guts::ChaCha;
Expand All @@ -27,7 +29,8 @@ const BLOCK_WORDS: u8 = 16;
#[repr(transparent)]
pub struct Array64<T>([T; 64]);
impl<T> Default for Array64<T>
where T: Default
where
T: Default,
{
#[rustfmt::skip]
fn default() -> Self {
Expand All @@ -54,7 +57,8 @@ impl<T> AsMut<[T]> for Array64<T> {
}
}
impl<T> Clone for Array64<T>
where T: Copy + Default
where
T: Copy + Default,
{
fn clone(&self) -> Self {
let mut new = Self::default();
Expand Down Expand Up @@ -275,20 +279,25 @@ macro_rules! chacha_impl {
#[cfg(feature = "serde1")]
impl Serialize for $ChaChaXRng {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: Serializer {
where
S: Serializer,
{
$abst::$ChaChaXRng::from(self).serialize(s)
}
}
#[cfg(feature = "serde1")]
impl<'de> Deserialize<'de> for $ChaChaXRng {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
where
D: Deserializer<'de>,
{
$abst::$ChaChaXRng::deserialize(d).map(|x| Self::from(&x))
}
}

mod $abst {
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};

// The abstract state of a ChaCha stream, independent of implementation choices. The
// comparison and serialization of this object is considered a semver-covered part of
Expand Down Expand Up @@ -353,7 +362,8 @@ chacha_impl!(
mod test {
use rand_core::{RngCore, SeedableRng};

#[cfg(feature = "serde1")] use super::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};
#[cfg(feature = "serde1")]
use super::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};

type ChaChaRng = super::ChaCha20Rng;

Expand Down
12 changes: 9 additions & 3 deletions rand_chacha/src/guts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use ppv_lite86::{dispatch, dispatch_light128};

pub use ppv_lite86::Machine;
use ppv_lite86::{vec128_storage, ArithOps, BitOps32, LaneWords4, MultiLane, StoreBytes, Vec4, Vec4Ext, Vector};
use ppv_lite86::{
vec128_storage, ArithOps, BitOps32, LaneWords4, MultiLane, StoreBytes, Vec4, Vec4Ext, Vector,
};

pub(crate) const BLOCK: usize = 16;
pub(crate) const BLOCK64: u64 = BLOCK as u64;
Expand Down Expand Up @@ -140,14 +142,18 @@ fn add_pos<Mach: Machine>(m: Mach, d: Mach::u32x4, i: u64) -> Mach::u32x4 {
#[cfg(target_endian = "little")]
fn d0123<Mach: Machine>(m: Mach, d: vec128_storage) -> Mach::u32x4x4 {
let d0: Mach::u64x2 = m.unpack(d);
let incr = Mach::u64x2x4::from_lanes([m.vec([0, 0]), m.vec([1, 0]), m.vec([2, 0]), m.vec([3, 0])]);
let incr =
Mach::u64x2x4::from_lanes([m.vec([0, 0]), m.vec([1, 0]), m.vec([2, 0]), m.vec([3, 0])]);
m.unpack((Mach::u64x2x4::from_lanes([d0, d0, d0, d0]) + incr).into())
}

#[allow(clippy::many_single_char_names)]
#[inline(always)]
fn refill_wide_impl<Mach: Machine>(
m: Mach, state: &mut ChaCha, drounds: u32, out: &mut [u32; BUFSZ],
m: Mach,
state: &mut ChaCha,
drounds: u32,
out: &mut [u32; BUFSZ],
) {
let k = m.vec([0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574]);
let b = m.unpack(state.b);
Expand Down
3 changes: 2 additions & 1 deletion rand_core/src/blanket_impls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[cfg(feature = "alloc")] use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;

use crate::{CryptoRng, RngCore, TryCryptoRng, TryRngCore};

Expand Down
3 changes: 2 additions & 1 deletion rand_core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
use crate::impls::{fill_via_u32_chunks, fill_via_u64_chunks};
use crate::{CryptoRng, RngCore, SeedableRng, TryRngCore};
use core::fmt;
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
#[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
2 changes: 1 addition & 1 deletion rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ macro_rules! impl_try_rng_from_rng_core {
macro_rules! impl_try_crypto_rng_from_crypto_rng {
($t:ty) => {
$crate::impl_try_rng_from_rng_core!($t);

impl $crate::TryCryptoRng for $t {}

/// Check at compile time that `$t` implements `CryptoRng`
Expand Down
25 changes: 17 additions & 8 deletions rand_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,28 @@
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
#![allow(unexpected_cfgs)]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![no_std]

#[cfg(feature = "alloc")] extern crate alloc;
#[cfg(feature = "std")] extern crate std;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

use core::fmt;

mod blanket_impls;
pub mod block;
pub mod impls;
pub mod le;
#[cfg(feature = "getrandom")] mod os;

#[cfg(feature = "getrandom")] pub use getrandom;
#[cfg(feature = "getrandom")] pub use os::OsRng;
#[cfg(feature = "getrandom")]
mod os;

#[cfg(feature = "getrandom")]
pub use getrandom;
#[cfg(feature = "getrandom")]
pub use os::OsRng;

/// The core of a random number generator.
///
Expand Down Expand Up @@ -213,14 +218,18 @@ pub trait TryRngCore {

/// Wrap RNG with the [`UnwrapErr`] wrapper.
fn unwrap_err(self) -> UnwrapErr<Self>
where Self: Sized {
where
Self: Sized,
{
UnwrapErr(self)
}

/// Convert an [`RngCore`] to a [`RngReadAdapter`].
#[cfg(feature = "std")]
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
where Self: Sized {
where
Self: Sized,
{
RngReadAdapter { inner: self }
}
}
Expand Down
21 changes: 10 additions & 11 deletions rand_distr/src/binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
//! The binomial distribution.

use crate::{Distribution, Uniform};
use rand::Rng;
use core::fmt;
use core::cmp::Ordering;
use core::fmt;
#[allow(unused_imports)]
use num_traits::Float;
use rand::Rng;

/// The binomial distribution `Binomial(n, p)`.
///
Expand Down Expand Up @@ -110,21 +110,21 @@ impl Distribution<u64> for Binomial {
// Threshold for preferring the BINV algorithm. The paper suggests 10,
// Ranlib uses 30, and GSL uses 14.
const BINV_THRESHOLD: f64 = 10.;

// Same value as in GSL.
// It is possible for BINV to get stuck, so we break if x > BINV_MAX_X and try again.
// It would be safer to set BINV_MAX_X to self.n, but it is extremely unlikely to be relevant.
// When n*p < 10, so is n*p*q which is the variance, so a result > 110 would be 100 / sqrt(10) = 31 standard deviations away.
const BINV_MAX_X : u64 = 110;
const BINV_MAX_X: u64 = 110;

if (self.n as f64) * p < BINV_THRESHOLD && self.n <= (i32::MAX as u64) {
// Use the BINV algorithm.
let s = p / q;
let a = ((self.n + 1) as f64) * s;

result = 'outer: loop {
let mut r = q.powi(self.n as i32);
let mut u: f64 = rng.gen();
let mut u: f64 = rng.random();
let mut x = 0;

while u > r {
Expand All @@ -136,7 +136,6 @@ impl Distribution<u64> for Binomial {
r *= a / (x as f64) - s;
}
break x;

}
} else {
// Use the BTPE algorithm.
Expand Down Expand Up @@ -238,7 +237,7 @@ impl Distribution<u64> for Binomial {
break;
}
}
},
}
Ordering::Greater => {
let mut i = y;
loop {
Expand All @@ -248,8 +247,8 @@ impl Distribution<u64> for Binomial {
break;
}
}
},
Ordering::Equal => {},
}
Ordering::Equal => {}
}
if v > f {
continue;
Expand Down Expand Up @@ -366,7 +365,7 @@ mod test {
fn binomial_distributions_can_be_compared() {
assert_eq!(Binomial::new(1, 1.0), Binomial::new(1, 1.0));
}

#[test]
fn binomial_avoid_infinite_loop() {
let dist = Binomial::new(16000000, 3.1444753148558566e-10).unwrap();
Expand Down
Loading

0 comments on commit 1b762b2

Please sign in to comment.