| 1 | // Copyright 2018 Developers of the Rand project. |
| 2 | // |
| 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. |
| 8 | |
| 9 | //! Mock random number generator |
| 10 | |
| 11 | #![allow(deprecated)] |
| 12 | |
| 13 | use rand_corerust-analyzer cargo rand_core 0.9.5 crate/::{implsrust-analyzer cargo rand_core 0.9.5 impls/, RngCorerust-analyzer cargo rand_core 0.9.5 RngCore#}; |
| 14 | |
| 15 | #[cfg(feature = "serde")] |
| 16 | use serde::{Deserialize, Serialize}; |
| 17 | |
| 18 | /// A mock generator yielding very predictable output |
| 19 | /// |
| 20 | /// This generates an arithmetic sequence (i.e. adds a constant each step) |
| 21 | /// over a `u64` number, using wrapping arithmetic. If the increment is 0 |
| 22 | /// the generator yields a constant. |
| 23 | /// |
| 24 | /// Other integer types (64-bit and smaller) are produced via cast from `u64`. |
| 25 | /// |
| 26 | /// Other types are produced via their implementation of [`Rng`](crate::Rng) or |
| 27 | /// [`Distribution`](crate::distr::Distribution). |
| 28 | /// Output values may not be intuitive and may change in future releases but |
| 29 | /// are considered |
| 30 | /// [portable](https://rust-random.github.io/book/portability.html). |
| 31 | /// (`bool` output is true when bit `1u64 << 31` is set.) |
| 32 | /// |
| 33 | /// # Example |
| 34 | /// |
| 35 | /// ``` |
| 36 | /// # #![allow(deprecated)] |
| 37 | /// use rand::Rng; |
| 38 | /// use rand::rngs::mock::StepRng; |
| 39 | /// |
| 40 | /// let mut my_rng = StepRng::new(2, 1); |
| 41 | /// let sample: [u64; 3] = my_rng.random(); |
| 42 | /// assert_eq!(sample, [2, 3, 4]); |
| 43 | /// ``` |
| 44 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 45 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 46 | #[deprecated(since = "0.9.2", note = "Deprecated without replacement")] |
| 47 | pub struct StepRngrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng# { |
| 48 | vrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#v.: u64, |
| 49 | arust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#a.: u64, |
| 50 | } |
| 51 | |
| 52 | impl StepRngrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng# { |
| 53 | /// Create a `StepRng`, yielding an arithmetic sequence starting with |
| 54 | /// `initial` and incremented by `increment` each time. |
| 55 | pub fn newrust-analyzer cargo rand 0.9.2 rngs/mock/impl#[StepRng]new().(initial: u64, increment: u64) -> Selfrust-analyzer cargo rand 0.9.2 rngs/mock/impl#[StepRng] { |
| 56 | StepRngrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng# { |
| 57 | vrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#v.: initial, |
| 58 | arust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#a.: increment, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl RngCorerust-analyzer cargo rand_core 0.9.5 RngCore# for StepRngrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng# { |
| 64 | #[inline] |
| 65 | fn next_u32rust-analyzer cargo rand 0.9.2 rngs/mock/impl#[StepRng][RngCore]next_u32().(&mut self) -> u32 { |
| 66 | self.next_u64rust-analyzer cargo rand 0.9.2 rngs/mock/impl#[StepRng][RngCore]next_u64().() as u32 |
| 67 | } |
| 68 | |
| 69 | #[inline] |
| 70 | fn next_u64rust-analyzer cargo rand 0.9.2 rngs/mock/impl#[StepRng][RngCore]next_u64().(&mut self) -> u64 { |
| 71 | let res = self.vrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#v.; |
| 72 | self.vrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#v. = self.vrust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#v..wrapping_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]wrapping_add().(self.arust-analyzer cargo rand 0.9.2 rngs/mock/StepRng#a.); |
| 73 | res |
| 74 | } |
| 75 | |
| 76 | #[inline] |
| 77 | fn fill_bytesrust-analyzer cargo rand 0.9.2 rngs/mock/impl#[StepRng][RngCore]fill_bytes().(&mut self, dst: &mut [u8]) { |
| 78 | implsrust-analyzer cargo rand_core 0.9.5 impls/::fill_bytes_via_nextrust-analyzer cargo rand_core 0.9.5 impls/fill_bytes_via_next().(self, dst) |
| 79 | } |
| 80 | } |