10
10
//! Random number generation traits
11
11
//!
12
12
//! This crate is mainly of interest to crates publishing implementations of
13
- //! [`RngCore`]. Other users are encouraged to use the [rand] crate instead
13
+ //! [`RngCore`]. Other users are encouraged to use the [` rand` ] crate instead
14
14
//! which re-exports the main traits and error types.
15
15
//!
16
16
//! [`RngCore`] is the core trait implemented by algorithmic pseudo-random number
25
25
//! The [`impls`] and [`le`] sub-modules include a few small functions to assist
26
26
//! implementation of [`RngCore`].
27
27
//!
28
- //! [rand]: https://crates.io/crates/rand
29
- //! [`RngCore`]: trait.RngCore.html
30
- //! [`SeedableRng`]: trait.SeedableRng.html
31
- //! [`Error`]: struct.Error.html
32
- //! [`impls`]: impls/index.html
33
- //! [`le`]: le/index.html
28
+ //! [`rand`]: https://docs.rs/rand
34
29
35
30
#![ doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" ,
36
31
html_favicon_url = "https://www.rust-lang.org/favicon.ico" ,
@@ -68,8 +63,8 @@ pub mod le;
68
63
///
69
64
/// This trait encapsulates the low-level functionality common to all
70
65
/// generators, and is the "back end", to be implemented by generators.
71
- /// End users should normally use [ `Rng`] from the [rand] crate, which is
72
- /// automatically implemented for every type implementing `RngCore`.
66
+ /// End users should normally use `Rng` trait from the [` rand` ] crate,
67
+ /// which is automatically implemented for every type implementing `RngCore`.
73
68
///
74
69
/// Three different methods for generating random data are provided since the
75
70
/// optimal implementation of each is dependent on the type of generator. There
@@ -89,7 +84,7 @@ pub mod le;
89
84
///
90
85
/// Typically implementators will implement only one of the methods available
91
86
/// in this trait directly, then use the helper functions from the
92
- /// [`rand_core:: impls`] module to implement the other methods.
87
+ /// [`impls`] module to implement the other methods.
93
88
///
94
89
/// It is recommended that implementations also implement:
95
90
///
@@ -135,40 +130,37 @@ pub mod le;
135
130
/// }
136
131
/// ```
137
132
///
138
- /// [rand]: https://crates.io/crates/rand
139
- /// [`Rng`]: ../rand/trait.Rng.html
140
- /// [`SeedableRng`]: trait.SeedableRng.html
141
- /// [`rand_core::impls`]: ../rand_core/impls/index.html
142
- /// [`try_fill_bytes`]: trait.RngCore.html#tymethod.try_fill_bytes
143
- /// [`fill_bytes`]: trait.RngCore.html#tymethod.fill_bytes
144
- /// [`next_u32`]: trait.RngCore.html#tymethod.next_u32
145
- /// [`next_u64`]: trait.RngCore.html#tymethod.next_u64
146
- /// [`CryptoRng`]: trait.CryptoRng.html
133
+ /// [`rand`]: https://docs.rs/rand
134
+ /// [`try_fill_bytes`]: RngCore::try_fill_bytes
135
+ /// [`fill_bytes`]: RngCore::fill_bytes
136
+ /// [`next_u32`]: RngCore::next_u32
137
+ /// [`next_u64`]: RngCore::next_u64
147
138
pub trait RngCore {
148
139
/// Return the next random `u32`.
149
140
///
150
141
/// RNGs must implement at least one method from this trait directly. In
151
142
/// the case this method is not implemented directly, it can be implemented
152
- /// using `self.next_u64() as u32` or
153
- /// [via `fill_bytes`](../rand_core/ impls/fn. next_u32_via_fill.html) .
143
+ /// using `self.next_u64() as u32` or via
144
+ /// [`fill_bytes`][ impls:: next_u32_via_fill] .
154
145
fn next_u32 ( & mut self ) -> u32 ;
155
146
156
147
/// Return the next random `u64`.
157
148
///
158
149
/// RNGs must implement at least one method from this trait directly. In
159
150
/// the case this method is not implemented directly, it can be implemented
160
- /// [ via `next_u32`](../rand_core/ impls/fn. next_u64_via_u32.html) or
161
- /// [via `fill_bytes`](../rand_core/ impls/fn. next_u64_via_fill.html) .
151
+ /// via [ `next_u32`][ impls:: next_u64_via_u32] or via
152
+ /// [`fill_bytes`][ impls:: next_u64_via_fill] .
162
153
fn next_u64 ( & mut self ) -> u64 ;
163
154
164
155
/// Fill `dest` with random data.
165
156
///
166
157
/// RNGs must implement at least one method from this trait directly. In
167
158
/// the case this method is not implemented directly, it can be implemented
168
- /// [via `next_u*`](../rand_core/impls/fn.fill_bytes_via_next.html) or
169
- /// via `try_fill_bytes`; if this generator can fail the implementation
170
- /// must choose how best to handle errors here (e.g. panic with a
171
- /// descriptive message or log a warning and retry a few times).
159
+ /// via [`next_u*`][impls::fill_bytes_via_next] or
160
+ /// via [`try_fill_bytes`][RngCore::try_fill_bytes]; if this generator can
161
+ /// fail the implementation must choose how best to handle errors here
162
+ /// (e.g. panic with a descriptive message or log a warning and retry a few
163
+ /// times).
172
164
///
173
165
/// This method should guarantee that `dest` is entirely filled
174
166
/// with new data, and may panic if this is impossible
@@ -188,7 +180,7 @@ pub trait RngCore {
188
180
/// `fill_bytes` may be implemented with
189
181
/// `self.try_fill_bytes(dest).unwrap()` or more specific error handling.
190
182
///
191
- /// [`fill_bytes`]: trait. RngCore.html#method. fill_bytes
183
+ /// [`fill_bytes`]: RngCore:: fill_bytes
192
184
fn try_fill_bytes ( & mut self , dest : & mut [ u8 ] ) -> Result < ( ) , Error > ;
193
185
}
194
186
@@ -213,20 +205,19 @@ pub trait RngCore {
213
205
/// Note also that use of a `CryptoRng` does not protect against other
214
206
/// weaknesses such as seeding from a weak entropy source or leaking state.
215
207
///
216
- /// [`RngCore`]: trait.RngCore.html
217
- /// [`BlockRngCore`]: ../rand_core/block/trait.BlockRngCore.html
208
+ /// [`BlockRngCore`]: block::BlockRngCore
218
209
pub trait CryptoRng { }
219
210
220
211
/// A random number generator that can be explicitly seeded.
221
212
///
222
213
/// This trait encapsulates the low-level functionality common to all
223
214
/// pseudo-random number generators (PRNGs, or algorithmic generators).
224
215
///
225
- /// The [`rand:: FromEntropy`] trait is automatically implemented for every type
226
- /// implementing `SeedableRng`, providing a convenient `from_entropy()`
227
- /// constructor.
216
+ /// The ` FromEntropy` trait from [`rand`] crate is automatically
217
+ /// implemented for every type implementing `SeedableRng`, providing
218
+ /// a convenient `from_entropy()` constructor.
228
219
///
229
- /// [`rand::FromEntropy `]: ../rand/trait.FromEntropy.html
220
+ /// [`rand`]: https://docs.rs/rand
230
221
pub trait SeedableRng : Sized {
231
222
/// Seed type, which is restricted to types mutably-dereferencable as `u8`
232
223
/// arrays (we recommend `[u8; N]` for some `N`).
@@ -340,8 +331,8 @@ pub trait SeedableRng: Sized {
340
331
/// Create a new PRNG seeded from another `Rng`.
341
332
///
342
333
/// This is the recommended way to initialize PRNGs with fresh entropy. The
343
- /// [ `FromEntropy`] trait provides a convenient `from_entropy` method
344
- /// based on `from_rng`.
334
+ /// `FromEntropy` trait from [`rand`] crate provides a convenient
335
+ /// `from_entropy` method based on `from_rng`.
345
336
///
346
337
/// Usage of this method is not recommended when reproducibility is required
347
338
/// since implementing PRNGs are not required to fix Endianness and are
@@ -354,9 +345,9 @@ pub trait SeedableRng: Sized {
354
345
/// results if their seed numbers are small or if there is a simple pattern
355
346
/// between them.
356
347
///
357
- /// Prefer to seed from a strong external entropy source like [ `OsRng`] or
358
- /// from a cryptographic PRNG; if creating a new generator for cryptographic
359
- /// uses you *must* seed from a strong source.
348
+ /// 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
350
+ /// generator for cryptographic uses you *must* seed from a strong source.
360
351
///
361
352
/// Seeding a small PRNG from another small PRNG is possible, but
362
353
/// something to be careful with. An extreme example of how this can go
@@ -367,8 +358,8 @@ pub trait SeedableRng: Sized {
367
358
/// PRNG implementations are allowed to assume that a good RNG is provided
368
359
/// for seeding, and that it is cryptographically secure when appropriate.
369
360
///
370
- /// [`FromEntropy `]: ../rand/trait.FromEntropy.html
371
- /// [`OsRng `]: ../rand/rngs/struct.OsRng.html
361
+ /// [`rand `]: https://docs.rs/rand
362
+ /// [`rand_os `]: https://docs.rs/rand_os
372
363
fn from_rng < R : RngCore > ( mut rng : R ) -> Result < Self , Error > {
373
364
let mut seed = Self :: Seed :: default ( ) ;
374
365
rng. try_fill_bytes ( seed. as_mut ( ) ) ?;
0 commit comments