Skip to content

Commit 300b84b

Browse files
committed
fix errors
1 parent b451306 commit 300b84b

File tree

5 files changed

+70
-70
lines changed

5 files changed

+70
-70
lines changed

rand_core/src/block.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@
2020
//! reseeding with very low overhead.
2121
//!
2222
//! # Example
23-
//!
23+
//!
2424
//! ```norun
2525
//! use rand_core::block::{BlockRngCore, BlockRng};
26-
//!
26+
//!
2727
//! struct MyRngCore;
28-
//!
28+
//!
2929
//! impl BlockRngCore for MyRngCore {
3030
//! type Results = [u32; 16];
31-
//!
31+
//!
3232
//! fn generate(&mut self, results: &mut Self::Results) {
3333
//! unimplemented!()
3434
//! }
3535
//! }
36-
//!
36+
//!
3737
//! impl SeedableRng for MyRngCore {
3838
//! type Seed = unimplemented!();
3939
//! fn from_seed(seed: Self::Seed) -> Self {
4040
//! unimplemented!()
4141
//! }
4242
//! }
43-
//!
43+
//!
4444
//! // optionally, also implement CryptoRng for MyRngCore
45-
//!
45+
//!
4646
//! // Final RNG.
4747
//! type MyRng = BlockRng<u32, MyRngCore>;
4848
//! ```
49-
//!
49+
//!
5050
//! [`BlockRngCore`]: crate::block::BlockRngCore
5151
//! [`fill_bytes`]: RngCore::fill_bytes
5252
@@ -58,12 +58,12 @@ use impls::{fill_via_u32_chunks, fill_via_u64_chunks};
5858
/// A trait for RNGs which do not generate random numbers individually, but in
5959
/// blocks (typically `[u32; N]`). This technique is commonly used by
6060
/// cryptographic RNGs to improve performance.
61-
///
61+
///
6262
/// See the [module][crate::block] documentation for details.
6363
pub trait BlockRngCore {
6464
/// Results element type, e.g. `u32`.
6565
type Item;
66-
66+
6767
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
6868
/// generates, which will usually be an array like `[u32; 16]`.
6969
type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default;
@@ -141,7 +141,7 @@ impl<R: BlockRngCore> BlockRng<R> {
141141
}
142142

143143
/// Get the index into the result buffer.
144-
///
144+
///
145145
/// If this is equal to or larger than the size of the result buffer then
146146
/// the buffer is "empty" and `generate()` must be called to produce new
147147
/// results.

rand_core/src/lib.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
// except according to those terms.
99

1010
//! Random number generation traits
11-
//!
11+
//!
1212
//! This crate is mainly of interest to crates publishing implementations of
1313
//! [`RngCore`]. Other users are encouraged to use the [`rand`] crate instead
1414
//! which re-exports the main traits and error types.
1515
//!
1616
//! [`RngCore`] is the core trait implemented by algorithmic pseudo-random number
1717
//! generators and external random-number sources.
18-
//!
18+
//!
1919
//! [`SeedableRng`] is an extension trait for construction from fixed seeds and
2020
//! other random number generators.
21-
//!
21+
//!
2222
//! [`Error`] is provided for error-handling. It is safe to use in `no_std`
2323
//! environments.
24-
//!
24+
//!
2525
//! The [`impls`] and [`le`] sub-modules include a few small functions to assist
2626
//! implementation of [`RngCore`].
27-
//!
27+
//!
2828
//! [`rand`]: https://docs.rs/rand
2929
3030
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",
@@ -60,34 +60,34 @@ pub mod le;
6060

6161

6262
/// The core of a random number generator.
63-
///
63+
///
6464
/// This trait encapsulates the low-level functionality common to all
6565
/// generators, and is the "back end", to be implemented by generators.
66-
/// End users should normally use `Rng` trait from the [`rand`] crate,
66+
/// End users should normally use the `Rng` trait from the [`rand`] crate,
6767
/// which is automatically implemented for every type implementing `RngCore`.
68-
///
68+
///
6969
/// Three different methods for generating random data are provided since the
7070
/// optimal implementation of each is dependent on the type of generator. There
7171
/// is no required relationship between the output of each; e.g. many
7272
/// implementations of [`fill_bytes`] consume a whole number of `u32` or `u64`
7373
/// values and drop any remaining unused bytes.
74-
///
74+
///
7575
/// The [`try_fill_bytes`] method is a variant of [`fill_bytes`] allowing error
7676
/// handling; it is not deemed sufficiently useful to add equivalents for
7777
/// [`next_u32`] or [`next_u64`] since the latter methods are almost always used
7878
/// with algorithmic generators (PRNGs), which are normally infallible.
79-
///
79+
///
8080
/// Algorithmic generators implementing [`SeedableRng`] should normally have
8181
/// *portable, reproducible* output, i.e. fix Endianness when converting values
8282
/// to avoid platform differences, and avoid making any changes which affect
8383
/// output (except by communicating that the release has breaking changes).
84-
///
84+
///
8585
/// Typically implementators will implement only one of the methods available
8686
/// in this trait directly, then use the helper functions from the
8787
/// [`impls`] module to implement the other methods.
88-
///
88+
///
8989
/// It is recommended that implementations also implement:
90-
///
90+
///
9191
/// - `Debug` with a custom implementation which *does not* print any internal
9292
/// state (at least, [`CryptoRng`]s should not risk leaking state through
9393
/// `Debug`).
@@ -99,37 +99,37 @@ pub mod le;
9999
/// implement [`SeedableRng`], to guide users towards proper seeding.
100100
/// External / hardware RNGs can choose to implement `Default`.
101101
/// - `Eq` and `PartialEq` could be implemented, but are probably not useful.
102-
///
102+
///
103103
/// # Example
104-
///
104+
///
105105
/// A simple example, obviously not generating very *random* output:
106-
///
106+
///
107107
/// ```
108108
/// #![allow(dead_code)]
109109
/// use rand_core::{RngCore, Error, impls};
110-
///
110+
///
111111
/// struct CountingRng(u64);
112-
///
112+
///
113113
/// impl RngCore for CountingRng {
114114
/// fn next_u32(&mut self) -> u32 {
115115
/// self.next_u64() as u32
116116
/// }
117-
///
117+
///
118118
/// fn next_u64(&mut self) -> u64 {
119119
/// self.0 += 1;
120120
/// self.0
121121
/// }
122-
///
122+
///
123123
/// fn fill_bytes(&mut self, dest: &mut [u8]) {
124124
/// impls::fill_bytes_via_next(self, dest)
125125
/// }
126-
///
126+
///
127127
/// fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
128128
/// Ok(self.fill_bytes(dest))
129129
/// }
130130
/// }
131131
/// ```
132-
///
132+
///
133133
/// [`rand`]: https://docs.rs/rand
134134
/// [`try_fill_bytes`]: RngCore::try_fill_bytes
135135
/// [`fill_bytes`]: RngCore::fill_bytes
@@ -148,7 +148,7 @@ pub trait RngCore {
148148
///
149149
/// RNGs must implement at least one method from this trait directly. In
150150
/// the case this method is not implemented directly, it can be implemented
151-
/// via [`next_u32`][impls::next_u64_via_u32] or via
151+
/// via [`next_u32`][impls::next_u64_via_u32] or via
152152
/// [`fill_bytes`][impls::next_u64_via_fill].
153153
fn next_u64(&mut self) -> u64;
154154

@@ -161,7 +161,7 @@ pub trait RngCore {
161161
/// fail the implementation must choose how best to handle errors here
162162
/// (e.g. panic with a descriptive message or log a warning and retry a few
163163
/// times).
164-
///
164+
///
165165
/// This method should guarantee that `dest` is entirely filled
166166
/// with new data, and may panic if this is impossible
167167
/// (e.g. reading past the end of a file that is being used as the
@@ -174,49 +174,49 @@ pub trait RngCore {
174174
/// generating random data thus making this the primary method implemented
175175
/// by external (true) RNGs (e.g. `OsRng`) which can fail. It may be used
176176
/// directly to generate keys and to seed (infallible) PRNGs.
177-
///
177+
///
178178
/// Other than error handling, this method is identical to [`fill_bytes`];
179179
/// thus this may be implemented using `Ok(self.fill_bytes(dest))` or
180180
/// `fill_bytes` may be implemented with
181181
/// `self.try_fill_bytes(dest).unwrap()` or more specific error handling.
182-
///
182+
///
183183
/// [`fill_bytes`]: RngCore::fill_bytes
184184
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error>;
185185
}
186186

187187
/// A marker trait used to indicate that an [`RngCore`] or [`BlockRngCore`]
188188
/// implementation is supposed to be cryptographically secure.
189-
///
189+
///
190190
/// *Cryptographically secure generators*, also known as *CSPRNGs*, should
191191
/// satisfy an additional properties over other generators: given the first
192192
/// *k* bits of an algorithm's output
193193
/// sequence, it should not be possible using polynomial-time algorithms to
194194
/// predict the next bit with probability significantly greater than 50%.
195-
///
195+
///
196196
/// Some generators may satisfy an additional property, however this is not
197197
/// required by this trait: if the CSPRNG's state is revealed, it should not be
198198
/// computationally-feasible to reconstruct output prior to this. Some other
199199
/// generators allow backwards-computation and are consided *reversible*.
200-
///
200+
///
201201
/// Note that this trait is provided for guidance only and cannot guarantee
202202
/// suitability for cryptographic applications. In general it should only be
203203
/// implemented for well-reviewed code implementing well-regarded algorithms.
204-
///
204+
///
205205
/// Note also that use of a `CryptoRng` does not protect against other
206206
/// weaknesses such as seeding from a weak entropy source or leaking state.
207-
///
207+
///
208208
/// [`BlockRngCore`]: block::BlockRngCore
209209
pub trait CryptoRng {}
210210

211211
/// A random number generator that can be explicitly seeded.
212212
///
213213
/// This trait encapsulates the low-level functionality common to all
214214
/// pseudo-random number generators (PRNGs, or algorithmic generators).
215-
///
216-
/// The `FromEntropy` trait from [`rand`] crate is automatically
215+
///
216+
/// The `FromEntropy` trait from the [`rand`] crate is automatically
217217
/// implemented for every type implementing `SeedableRng`, providing
218218
/// a convenient `from_entropy()` constructor.
219-
///
219+
///
220220
/// [`rand`]: https://docs.rs/rand
221221
pub trait SeedableRng: Sized {
222222
/// Seed type, which is restricted to types mutably-dereferencable as `u8`
@@ -288,17 +288,17 @@ pub trait SeedableRng: Sized {
288288
/// for example `0xBAD5EEDu32` or `0x0DDB1A5E5BAD5EEDu64` ("odd biases? bad
289289
/// seed"). This is assuming only a small number of values must be rejected.
290290
fn from_seed(seed: Self::Seed) -> Self;
291-
291+
292292
/// Create a new PRNG using a `u64` seed.
293-
///
293+
///
294294
/// This is a convenience-wrapper around `from_seed` to allow construction
295295
/// of any `SeedableRng` from a simple `u64` value. It is designed such that
296296
/// low Hamming Weight numbers like 0 and 1 can be used and should still
297297
/// result in good, independent seeds to the PRNG which is returned.
298-
///
298+
///
299299
/// This **is not suitable for cryptography**, as should be clear given that
300300
/// the input size is only 64 bits.
301-
///
301+
///
302302
/// Implementations for PRNGs *may* provide their own implementations of
303303
/// this function, but the default implementation should be good enough for
304304
/// all purposes. *Changing* the implementation of this function should be
@@ -307,33 +307,33 @@ pub trait SeedableRng: Sized {
307307
// We use PCG32 to generate a u32 sequence, and copy to the seed
308308
const MUL: u64 = 6364136223846793005;
309309
const INC: u64 = 11634580027462260723;
310-
310+
311311
let mut seed = Self::Seed::default();
312312
for chunk in seed.as_mut().chunks_mut(4) {
313313
// We advance the state first (to get away from the input value,
314314
// in case it has low Hamming Weight).
315315
state = state.wrapping_mul(MUL).wrapping_add(INC);
316-
316+
317317
// Use PCG output function with to_le to generate x:
318318
let xorshifted = (((state >> 18) ^ state) >> 27) as u32;
319319
let rot = (state >> 59) as u32;
320320
let x = xorshifted.rotate_right(rot).to_le();
321-
321+
322322
unsafe {
323323
let p = &x as *const u32 as *const u8;
324324
copy_nonoverlapping(p, chunk.as_mut_ptr(), chunk.len());
325325
}
326326
}
327-
327+
328328
Self::from_seed(seed)
329329
}
330-
330+
331331
/// Create a new PRNG seeded from another `Rng`.
332332
///
333333
/// This is the recommended way to initialize PRNGs with fresh entropy. The
334-
/// `FromEntropy` trait from [`rand`] crate provides a convenient
334+
/// `FromEntropy` trait from the [`rand`] crate provides a convenient
335335
/// `from_entropy` method based on `from_rng`.
336-
///
336+
///
337337
/// Usage of this method is not recommended when reproducibility is required
338338
/// since implementing PRNGs are not required to fix Endianness and are
339339
/// allowed to modify implementations in new releases.
@@ -346,7 +346,7 @@ pub trait SeedableRng: Sized {
346346
/// between them.
347347
///
348348
/// Prefer to seed from a strong external entropy source like `OsRng` from
349-
/// [`rand_os`] crate or from a cryptographic PRNG; if creating a new
349+
/// the [`rand_os`] crate or from a cryptographic PRNG; if creating a new
350350
/// generator for cryptographic uses you *must* seed from a strong source.
351351
///
352352
/// Seeding a small PRNG from another small PRNG is possible, but
@@ -357,7 +357,7 @@ pub trait SeedableRng: Sized {
357357
///
358358
/// PRNG implementations are allowed to assume that a good RNG is provided
359359
/// for seeding, and that it is cryptographically secure when appropriate.
360-
///
360+
///
361361
/// [`rand`]: https://docs.rs/rand
362362
/// [`rand_os`]: https://docs.rs/rand_os
363363
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
@@ -436,7 +436,7 @@ impl<R: CryptoRng + ?Sized> CryptoRng for Box<R> {}
436436
#[cfg(test)]
437437
mod test {
438438
use super::*;
439-
439+
440440
#[test]
441441
fn test_seed_from_u64() {
442442
struct SeedableNum(u64);
@@ -448,29 +448,29 @@ mod test {
448448
SeedableNum(x[0])
449449
}
450450
}
451-
451+
452452
const N: usize = 8;
453453
const SEEDS: [u64; N] = [0u64, 1, 2, 3, 4, 8, 16, -1i64 as u64];
454454
let mut results = [0u64; N];
455455
for (i, seed) in SEEDS.iter().enumerate() {
456456
let SeedableNum(x) = SeedableNum::seed_from_u64(*seed);
457457
results[i] = x;
458458
}
459-
459+
460460
for (i1, r1) in results.iter().enumerate() {
461461
let weight = r1.count_ones();
462462
// This is the binomial distribution B(64, 0.5), so chance of
463463
// weight < 20 is binocdf(19, 64, 0.5) = 7.8e-4, and same for
464464
// weight > 44.
465465
assert!(weight >= 20 && weight <= 44);
466-
466+
467467
for (i2, r2) in results.iter().enumerate() {
468468
if i1 == i2 { continue; }
469469
let diff_weight = (r1 ^ r2).count_ones();
470470
assert!(diff_weight >= 20);
471471
}
472472
}
473-
473+
474474
// value-breakage test:
475475
assert_eq!(results[0], 5029875928683246316);
476476
}

0 commit comments

Comments
 (0)