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
//! # Wrappers for compatibility with rand 0.4

#![no_std]

use core::marker::PhantomData;

use rand::Rng;
pub use rand04;

#[derive(Debug)]
pub struct Generator<'a, T, R>(&'a mut R, PhantomData<T>);

impl<'a, R: Rng, T: rand04::Rand> Iterator for Generator<'a, T, R> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        Some(self.0.gen04())
    }
}

/// An extension trait for `Rng` to generate values that implement `rand04::Rand`.
pub trait RngExt {
    /// Returns a random value.
    fn gen04<T: rand04::Rand>(&mut self) -> T;

    /// Returns an iterator that will yield an infinite number of randomly generated items.
    fn gen_iter04<T: rand04::Rand>(&mut self) -> Generator<T, Self>
    where
        Self: Sized;
}

impl<R: Rng + ?Sized> RngExt for R {
    fn gen04<T: rand04::Rand>(&mut self) -> T {
        rand04::Rng::gen(&mut RngWrapper(self))
    }

    fn gen_iter04<T: rand04::Rand>(&mut self) -> Generator<T, Self>
    where
        Self: Sized,
    {
        Generator(self, PhantomData)
    }
}

/// A wrapper for a random number generator that implements the 0.4 `Rng` trait.
pub struct RngWrapper<R>(pub R);

impl<R: Rng> rand04::Rng for RngWrapper<R> {
    #[inline]
    fn next_u32(&mut self) -> u32 {
        self.0.next_u32()
    }

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

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