]>
git.proxmox.com Git - rustc.git/blob - vendor/rand-0.7.3/src/rngs/mock.rs
1 // Copyright 2018 Developers of the Rand project.
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
9 //! Mock random number generator
11 use rand_core
::{impls, Error, RngCore}
;
13 /// A simple implementation of `RngCore` for testing purposes.
15 /// This generates an arithmetic sequence (i.e. adds a constant each step)
16 /// over a `u64` number, using wrapping arithmetic. If the increment is 0
17 /// the generator yields a constant.
21 /// use rand::rngs::mock::StepRng;
23 /// let mut my_rng = StepRng::new(2, 1);
24 /// let sample: [u64; 3] = my_rng.gen();
25 /// assert_eq!(sample, [2, 3, 4]);
27 #[derive(Debug, Clone)]
34 /// Create a `StepRng`, yielding an arithmetic sequence starting with
35 /// `initial` and incremented by `increment` each time.
36 pub fn new(initial
: u64, increment
: u64) -> Self {
44 impl RngCore
for StepRng
{
46 fn next_u32(&mut self) -> u32 {
47 self.next_u64() as u32
51 fn next_u64(&mut self) -> u64 {
53 self.v
= self.v
.wrapping_add(self.a
);
58 fn fill_bytes(&mut self, dest
: &mut [u8]) {
59 impls
::fill_bytes_via_next(self, dest
);
63 fn try_fill_bytes(&mut self, dest
: &mut [u8]) -> Result
<(), Error
> {
64 self.fill_bytes(dest
);