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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//! Compatibility shims for the `rand` crate ecosystem.

use crate::{
    traits::{GenCore, TurboCore},
    RngCore,
};

#[cfg(feature = "wyrand")]
use crate::rng::Rng;

#[cfg(feature = "chacha")]
use crate::chacha_rng::ChaChaRng;

#[cfg(feature = "atomic")]
use crate::rng::AtomicRng;

/// A wrapper struct around [`TurboCore`] to allow implementing
/// [`RngCore`] trait in a compatible manner.
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct RandCompat<T: TurboCore + GenCore>(T);

#[cfg(feature = "std")]
impl<T: TurboCore + GenCore + Default> RandCompat<T> {
    /// Creates a new [`RandCompat`] with a randomised seed.
    ///
    /// # Example
    /// ```
    /// use turborand::prelude::*;
    /// use rand_core::RngCore;
    ///
    /// let mut rng = RandCompat::<Rng>::new();
    /// let mut buffer = [0u8; 32];
    ///
    /// rng.fill_bytes(&mut buffer);
    ///
    /// assert_ne!(&buffer, &[0u8; 32]);
    /// ```
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self(T::default())
    }
}

#[cfg(feature = "std")]
impl<T: TurboCore + GenCore + Default> Default for RandCompat<T> {
    /// Initialises a default instance of [`RandCompat`]. Warning, the default is
    /// seeded with a randomly generated state, so this is **not** deterministic.
    ///
    /// # Example
    /// ```
    /// use turborand::prelude::*;
    /// use rand_core::RngCore;
    ///
    /// let mut rng1 = RandCompat::<Rng>::default();
    /// let mut rng2 = RandCompat::<Rng>::default();
    ///
    /// assert_ne!(rng1.next_u64(), rng2.next_u64());
    /// ```
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<T: TurboCore + GenCore> RngCore for RandCompat<T> {
    #[inline]
    fn next_u32(&mut self) -> u32 {
        self.0.gen_u32()
    }

    #[inline]
    fn next_u64(&mut self) -> u64 {
        self.0.gen_u64()
    }

    #[inline]
    fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.0.fill_bytes(dest);
    }

    #[inline]
    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
        self.0.fill_bytes(dest);
        Ok(())
    }
}

impl<T: TurboCore + GenCore> From<T> for RandCompat<T> {
    #[inline]
    fn from(rng: T) -> Self {
        Self(rng)
    }
}

#[cfg(feature = "wyrand")]
impl From<RandCompat<Rng>> for Rng {
    #[inline]
    fn from(rand: RandCompat<Rng>) -> Self {
        rand.0
    }
}

#[cfg(feature = "atomic")]
impl From<RandCompat<AtomicRng>> for AtomicRng {
    #[inline]
    fn from(rand: RandCompat<AtomicRng>) -> Self {
        rand.0
    }
}

#[cfg(feature = "chacha")]
impl From<RandCompat<ChaChaRng>> for ChaChaRng {
    #[inline]
    fn from(rand: RandCompat<ChaChaRng>) -> Self {
        rand.0
    }
}

/// A wrapper struct around a borrowed [`TurboCore`] instance to allow
/// implementing [`RngCore`] trait in a compatible manner. Uses a mutable
/// reference to gain exclusive control over the [`TurboCore`] instance.
/// [`RngCore`] uses `&mut self` for its methods, so [`RandBorrowed`] should
/// impose the same requirements onto [`TurboCore`] in needing a `&mut`
/// reference.
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct RandBorrowed<'a, T: TurboCore + GenCore>(&'a mut T);

impl<'a, T: TurboCore + GenCore> From<&'a mut T> for RandBorrowed<'a, T> {
    /// Convert a [`TurboCore`] reference into a [`RandBorrowed`] struct,
    /// allowing a borrowed reference to be used with the `rand` crate
    /// ecosystem.
    ///
    /// # Example
    /// ```
    /// use turborand::prelude::*;
    /// use rand_core::RngCore;
    ///
    /// let mut turbo = Rng::with_seed(Default::default());
    ///
    /// let mut rng = RandBorrowed::from(&mut turbo);
    ///
    /// assert_eq!(rng.next_u32(), 3791187244);
    /// ```
    #[inline]
    fn from(rng: &'a mut T) -> Self {
        Self(rng)
    }
}

impl<'a, T: TurboCore + GenCore + Default> RngCore for RandBorrowed<'a, T> {
    #[inline]
    fn next_u32(&mut self) -> u32 {
        self.0.gen_u32()
    }

    #[inline]
    fn next_u64(&mut self) -> u64 {
        self.0.gen_u64()
    }

    #[inline]
    fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.0.fill_bytes(dest);
    }

    #[inline]
    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
        self.0.fill_bytes(dest);
        Ok(())
    }
}