1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use rand_core::impls::next_u64_via_u32;
use rand_core::{Error, RngCore, SeedableRng};

use super::Mt;
use crate::random::{seed_to_key, DEFAULT_SEED_BYTES};

impl SeedableRng for Mt {
    type Seed = [u8; DEFAULT_SEED_BYTES];

    /// Reseed from four `u32`s.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rand_core::{RngCore, SeedableRng};
    /// # use spinoso_random::Mt;
    /// // Default MT seed
    /// let seed = 5489_u128.to_le_bytes();
    /// let mut mt = Mt::from_seed(seed);
    /// assert_ne!(mt.next_u32(), mt.next_u32());
    /// ```
    #[inline]
    fn from_seed(seed: Self::Seed) -> Self {
        let seed = seed_to_key(seed);
        Self::new_with_key(seed.iter().copied())
    }
}

impl RngCore for Mt {
    /// Generate next `u64` output.
    ///
    /// This function is implemented by generating two `u32`s from the RNG and
    /// shifting + masking them into a `u64` output.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rand_core::RngCore;
    /// # use spinoso_random::Mt;
    /// let mut mt = Mt::with_seed(5489);
    /// assert_ne!(mt.next_u64(), mt.next_u64());
    /// ```
    #[inline]
    #[must_use]
    fn next_u64(&mut self) -> u64 {
        next_u64_via_u32(self)
    }

    /// Generate next `u32` output.
    ///
    /// `u32` is the native output of the generator. This function advances the
    /// RNG step counter by one.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rand_core::RngCore;
    /// # use spinoso_random::Mt;
    /// let mut mt = Mt::with_seed(5489);
    /// assert_ne!(mt.next_u32(), mt.next_u32());
    /// ```
    #[inline]
    #[must_use]
    fn next_u32(&mut self) -> u32 {
        self.next_int32()
    }

    /// Fill a buffer with bytes generated from the RNG.
    ///
    /// This method generates random `u32`s (the native output unit of the RNG)
    /// until `dest` is filled.
    ///
    /// This method may discard some output bits if `dest.len()` is not a
    /// multiple of 4.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rand_core::RngCore;
    /// # use spinoso_random::Mt;
    /// let mut mt = Mt::with_seed(5489);
    /// let mut buf = [0; 32];
    /// mt.fill_bytes(&mut buf);
    /// assert_ne!([0; 32], buf);
    /// let mut buf = [0; 31];
    /// mt.fill_bytes(&mut buf);
    /// assert_ne!([0; 31], buf);
    /// ```
    #[inline]
    fn fill_bytes(&mut self, dest: &mut [u8]) {
        Self::fill_bytes(self, dest);
    }

    /// Fill a buffer with bytes generated from the RNG.
    ///
    /// This method generates random `u32`s (the native output unit of the RNG)
    /// until `dest` is filled.
    ///
    /// This method may discard some output bits if `dest.len()` is not a
    /// multiple of 4.
    ///
    /// `try_fill_bytes` is implemented with [`fill_bytes`] and is infallible.
    ///
    /// # Examples
    ///
    /// ```
    /// # use rand_core::{Error, RngCore};
    /// # use spinoso_random::Mt;
    /// # fn example() -> Result<(), Error> {
    /// let mut mt = Mt::with_seed(5489);
    /// let mut buf = [0; 32];
    /// mt.try_fill_bytes(&mut buf)?;
    /// assert_ne!([0; 32], buf);
    /// let mut buf = [0; 31];
    /// mt.try_fill_bytes(&mut buf)?;
    /// assert_ne!([0; 31], buf);
    /// # Ok(())
    /// # }
    /// # example().unwrap()
    /// ```
    ///
    /// # Errors
    ///
    /// This method never returns an error. It is equivalent to calling the
    /// infallible [`fill_bytes`] method.
    ///
    /// [`fill_bytes`]: Mt::fill_bytes
    #[inline]
    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
        Self::fill_bytes(self, dest);
        Ok(())
    }
}