Skip to content

Commit 1b762b2

Browse files
authored
Apply rustfmt and fix Clippy warnings (#1448)
1 parent e937769 commit 1b762b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1537
-956
lines changed

.github/workflows/benches.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Benches
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- ".github/workflows/benches.yml"
7+
- "benches/**"
8+
9+
jobs:
10+
benches:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: dtolnay/rust-toolchain@master
15+
with:
16+
toolchain: nightly
17+
components: clippy, rustfmt
18+
- name: Rustfmt
19+
run: cargo fmt --all -- --check
20+
- name: Clippy
21+
run: cargo clippy --all --all-targets -- -D warnings
22+
- name: Build
23+
run: RUSTFLAGS=-Dwarnings cargo build --all-targets

.github/workflows/test.yml

-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ jobs:
7272
if: ${{ matrix.variant == 'minimal_versions' }}
7373
run: |
7474
cargo generate-lockfile -Z minimal-versions
75-
# Overrides for dependencies with incorrect requirements (may need periodic updating)
76-
cargo update -p regex --precise 1.5.1
7775
- name: Maybe nightly
7876
if: ${{ matrix.toolchain == 'nightly' }}
7977
run: |

.github/workflows/workspace.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Workspace
2+
3+
on:
4+
pull_request:
5+
paths-ignore:
6+
- README.md
7+
- "benches/**"
8+
push:
9+
branches: master
10+
paths-ignore:
11+
- README.md
12+
- "benches/**"
13+
14+
jobs:
15+
clippy:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
- uses: dtolnay/rust-toolchain@master
20+
with:
21+
toolchain: 1.78.0
22+
components: clippy
23+
- run: cargo clippy --all --all-targets -- -D warnings
24+
25+
rustfmt:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- uses: actions/checkout@v4
29+
- uses: dtolnay/rust-toolchain@master
30+
with:
31+
toolchain: stable
32+
components: rustfmt
33+
- run: cargo fmt --all -- --check

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ unbiased = []
5858

5959
[workspace]
6060
members = [
61-
"benches",
6261
"rand_core",
6362
"rand_distr",
6463
"rand_chacha",
6564
"rand_pcg",
6665
]
66+
exclude = ["benches"]
6767

6868
[dependencies]
6969
rand_core = { path = "rand_core", version = "=0.9.0-alpha.1", default-features = false }

benches/benches/generators.rs

-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ gen_bytes!(gen_bytes_chacha8, ChaCha8Rng::from_os_rng());
5050
gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_os_rng());
5151
gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_os_rng());
5252
gen_bytes!(gen_bytes_std, StdRng::from_os_rng());
53-
#[cfg(feature = "small_rng")]
5453
gen_bytes!(gen_bytes_small, SmallRng::from_thread_rng());
5554
gen_bytes!(gen_bytes_os, UnwrapErr(OsRng));
5655
gen_bytes!(gen_bytes_thread, thread_rng());
@@ -81,7 +80,6 @@ gen_uint!(gen_u32_chacha8, u32, ChaCha8Rng::from_os_rng());
8180
gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_os_rng());
8281
gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_os_rng());
8382
gen_uint!(gen_u32_std, u32, StdRng::from_os_rng());
84-
#[cfg(feature = "small_rng")]
8583
gen_uint!(gen_u32_small, u32, SmallRng::from_thread_rng());
8684
gen_uint!(gen_u32_os, u32, UnwrapErr(OsRng));
8785
gen_uint!(gen_u32_thread, u32, thread_rng());
@@ -95,7 +93,6 @@ gen_uint!(gen_u64_chacha8, u64, ChaCha8Rng::from_os_rng());
9593
gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_os_rng());
9694
gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_os_rng());
9795
gen_uint!(gen_u64_std, u64, StdRng::from_os_rng());
98-
#[cfg(feature = "small_rng")]
9996
gen_uint!(gen_u64_small, u64, SmallRng::from_thread_rng());
10097
gen_uint!(gen_u64_os, u64, UnwrapErr(OsRng));
10198
gen_uint!(gen_u64_thread, u64, thread_rng());

rand_chacha/src/chacha.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
//! The ChaCha random number generator.
1010
11-
#[cfg(not(feature = "std"))] use core;
12-
#[cfg(feature = "std")] use std as core;
11+
#[cfg(not(feature = "std"))]
12+
use core;
13+
#[cfg(feature = "std")]
14+
use std as core;
1315

1416
use self::core::fmt;
1517
use crate::guts::ChaCha;
@@ -27,7 +29,8 @@ const BLOCK_WORDS: u8 = 16;
2729
#[repr(transparent)]
2830
pub struct Array64<T>([T; 64]);
2931
impl<T> Default for Array64<T>
30-
where T: Default
32+
where
33+
T: Default,
3134
{
3235
#[rustfmt::skip]
3336
fn default() -> Self {
@@ -54,7 +57,8 @@ impl<T> AsMut<[T]> for Array64<T> {
5457
}
5558
}
5659
impl<T> Clone for Array64<T>
57-
where T: Copy + Default
60+
where
61+
T: Copy + Default,
5862
{
5963
fn clone(&self) -> Self {
6064
let mut new = Self::default();
@@ -275,20 +279,25 @@ macro_rules! chacha_impl {
275279
#[cfg(feature = "serde1")]
276280
impl Serialize for $ChaChaXRng {
277281
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
278-
where S: Serializer {
282+
where
283+
S: Serializer,
284+
{
279285
$abst::$ChaChaXRng::from(self).serialize(s)
280286
}
281287
}
282288
#[cfg(feature = "serde1")]
283289
impl<'de> Deserialize<'de> for $ChaChaXRng {
284290
fn deserialize<D>(d: D) -> Result<Self, D::Error>
285-
where D: Deserializer<'de> {
291+
where
292+
D: Deserializer<'de>,
293+
{
286294
$abst::$ChaChaXRng::deserialize(d).map(|x| Self::from(&x))
287295
}
288296
}
289297

290298
mod $abst {
291-
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
299+
#[cfg(feature = "serde1")]
300+
use serde::{Deserialize, Serialize};
292301

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

356-
#[cfg(feature = "serde1")] use super::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};
365+
#[cfg(feature = "serde1")]
366+
use super::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};
357367

358368
type ChaChaRng = super::ChaCha20Rng;
359369

rand_chacha/src/guts.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use ppv_lite86::{dispatch, dispatch_light128};
1313

1414
pub use ppv_lite86::Machine;
15-
use ppv_lite86::{vec128_storage, ArithOps, BitOps32, LaneWords4, MultiLane, StoreBytes, Vec4, Vec4Ext, Vector};
15+
use ppv_lite86::{
16+
vec128_storage, ArithOps, BitOps32, LaneWords4, MultiLane, StoreBytes, Vec4, Vec4Ext, Vector,
17+
};
1618

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

147150
#[allow(clippy::many_single_char_names)]
148151
#[inline(always)]
149152
fn refill_wide_impl<Mach: Machine>(
150-
m: Mach, state: &mut ChaCha, drounds: u32, out: &mut [u32; BUFSZ],
153+
m: Mach,
154+
state: &mut ChaCha,
155+
drounds: u32,
156+
out: &mut [u32; BUFSZ],
151157
) {
152158
let k = m.vec([0x6170_7865, 0x3320_646e, 0x7962_2d32, 0x6b20_6574]);
153159
let b = m.unpack(state.b);

rand_core/src/blanket_impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#[cfg(feature = "alloc")] use alloc::boxed::Box;
1+
#[cfg(feature = "alloc")]
2+
use alloc::boxed::Box;
23

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

rand_core/src/block.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
use crate::impls::{fill_via_u32_chunks, fill_via_u64_chunks};
5757
use crate::{CryptoRng, RngCore, SeedableRng, TryRngCore};
5858
use core::fmt;
59-
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
59+
#[cfg(feature = "serde1")]
60+
use serde::{Deserialize, Serialize};
6061

6162
/// A trait for RNGs which do not generate random numbers individually, but in
6263
/// blocks (typically `[u32; N]`). This technique is commonly used by

rand_core/src/impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ macro_rules! impl_try_rng_from_rng_core {
199199
macro_rules! impl_try_crypto_rng_from_crypto_rng {
200200
($t:ty) => {
201201
$crate::impl_try_rng_from_rng_core!($t);
202-
202+
203203
impl $crate::TryCryptoRng for $t {}
204204

205205
/// Check at compile time that `$t` implements `CryptoRng`

rand_core/src/lib.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,28 @@
3232
#![deny(missing_docs)]
3333
#![deny(missing_debug_implementations)]
3434
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
35+
#![allow(unexpected_cfgs)]
3536
#![cfg_attr(doc_cfg, feature(doc_cfg))]
3637
#![no_std]
3738

38-
#[cfg(feature = "alloc")] extern crate alloc;
39-
#[cfg(feature = "std")] extern crate std;
39+
#[cfg(feature = "alloc")]
40+
extern crate alloc;
41+
#[cfg(feature = "std")]
42+
extern crate std;
4043

4144
use core::fmt;
4245

4346
mod blanket_impls;
4447
pub mod block;
4548
pub mod impls;
4649
pub mod le;
47-
#[cfg(feature = "getrandom")] mod os;
48-
49-
#[cfg(feature = "getrandom")] pub use getrandom;
50-
#[cfg(feature = "getrandom")] pub use os::OsRng;
50+
#[cfg(feature = "getrandom")]
51+
mod os;
5152

53+
#[cfg(feature = "getrandom")]
54+
pub use getrandom;
55+
#[cfg(feature = "getrandom")]
56+
pub use os::OsRng;
5257

5358
/// The core of a random number generator.
5459
///
@@ -213,14 +218,18 @@ pub trait TryRngCore {
213218

214219
/// Wrap RNG with the [`UnwrapErr`] wrapper.
215220
fn unwrap_err(self) -> UnwrapErr<Self>
216-
where Self: Sized {
221+
where
222+
Self: Sized,
223+
{
217224
UnwrapErr(self)
218225
}
219226

220227
/// Convert an [`RngCore`] to a [`RngReadAdapter`].
221228
#[cfg(feature = "std")]
222229
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
223-
where Self: Sized {
230+
where
231+
Self: Sized,
232+
{
224233
RngReadAdapter { inner: self }
225234
}
226235
}

rand_distr/src/binomial.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
//! The binomial distribution.
1111
1212
use crate::{Distribution, Uniform};
13-
use rand::Rng;
14-
use core::fmt;
1513
use core::cmp::Ordering;
14+
use core::fmt;
1615
#[allow(unused_imports)]
1716
use num_traits::Float;
17+
use rand::Rng;
1818

1919
/// The binomial distribution `Binomial(n, p)`.
2020
///
@@ -110,21 +110,21 @@ impl Distribution<u64> for Binomial {
110110
// Threshold for preferring the BINV algorithm. The paper suggests 10,
111111
// Ranlib uses 30, and GSL uses 14.
112112
const BINV_THRESHOLD: f64 = 10.;
113-
113+
114114
// Same value as in GSL.
115115
// It is possible for BINV to get stuck, so we break if x > BINV_MAX_X and try again.
116116
// It would be safer to set BINV_MAX_X to self.n, but it is extremely unlikely to be relevant.
117117
// 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.
118-
const BINV_MAX_X : u64 = 110;
118+
const BINV_MAX_X: u64 = 110;
119119

120120
if (self.n as f64) * p < BINV_THRESHOLD && self.n <= (i32::MAX as u64) {
121121
// Use the BINV algorithm.
122122
let s = p / q;
123123
let a = ((self.n + 1) as f64) * s;
124-
124+
125125
result = 'outer: loop {
126126
let mut r = q.powi(self.n as i32);
127-
let mut u: f64 = rng.gen();
127+
let mut u: f64 = rng.random();
128128
let mut x = 0;
129129

130130
while u > r {
@@ -136,7 +136,6 @@ impl Distribution<u64> for Binomial {
136136
r *= a / (x as f64) - s;
137137
}
138138
break x;
139-
140139
}
141140
} else {
142141
// Use the BTPE algorithm.
@@ -238,7 +237,7 @@ impl Distribution<u64> for Binomial {
238237
break;
239238
}
240239
}
241-
},
240+
}
242241
Ordering::Greater => {
243242
let mut i = y;
244243
loop {
@@ -248,8 +247,8 @@ impl Distribution<u64> for Binomial {
248247
break;
249248
}
250249
}
251-
},
252-
Ordering::Equal => {},
250+
}
251+
Ordering::Equal => {}
253252
}
254253
if v > f {
255254
continue;
@@ -366,7 +365,7 @@ mod test {
366365
fn binomial_distributions_can_be_compared() {
367366
assert_eq!(Binomial::new(1, 1.0), Binomial::new(1, 1.0));
368367
}
369-
368+
370369
#[test]
371370
fn binomial_avoid_infinite_loop() {
372371
let dist = Binomial::new(16000000, 3.1444753148558566e-10).unwrap();

0 commit comments

Comments
 (0)