Fallout: add phantom data to librand

This commit is contained in:
Niko Matsakis 2015-02-12 12:41:03 -05:00
parent 2953710d26
commit 199b992e63
2 changed files with 12 additions and 3 deletions

View file

@ -21,6 +21,7 @@
use core::prelude::*;
use core::num::{Float, Int};
use core::marker::PhantomData;
use {Rng, Rand};
@ -56,7 +57,13 @@ pub trait IndependentSample<Support>: Sample<Support> {
/// A wrapper for generating types that implement `Rand` via the
/// `Sample` & `IndependentSample` traits.
pub struct RandSample<Sup>;
pub struct RandSample<Sup> { _marker: PhantomData<Sup> }
impl<Sup> RandSample<Sup> {
pub fn new() -> RandSample<Sup> {
RandSample { _marker: PhantomData }
}
}
impl<Sup: Rand> Sample<Sup> for RandSample<Sup> {
fn sample<R: Rng>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
@ -285,7 +292,7 @@ mod tests {
#[test]
fn test_rand_sample() {
let mut rand_sample = RandSample::<ConstRand>;
let mut rand_sample = RandSample::<ConstRand>::new();
assert_eq!(rand_sample.sample(&mut ::test::rng()), ConstRand(0));
assert_eq!(rand_sample.ind_sample(&mut ::test::rng()), ConstRand(0));

View file

@ -41,6 +41,7 @@ extern crate core;
#[cfg(test)] #[macro_use] extern crate log;
use core::prelude::*;
use core::marker::PhantomData;
pub use isaac::{IsaacRng, Isaac64Rng};
pub use chacha::ChaChaRng;
@ -206,7 +207,7 @@ pub trait Rng : Sized {
/// .collect::<Vec<(f64, bool)>>());
/// ```
fn gen_iter<'a, T: Rand>(&'a mut self) -> Generator<'a, T, Self> {
Generator { rng: self }
Generator { rng: self, _marker: PhantomData }
}
/// Generate a random value in the range [`low`, `high`).
@ -317,6 +318,7 @@ pub trait Rng : Sized {
/// This iterator is created via the `gen_iter` method on `Rng`.
pub struct Generator<'a, T, R:'a> {
rng: &'a mut R,
_marker: PhantomData<T>
}
impl<'a, T: Rand, R: Rng> Iterator for Generator<'a, T, R> {