Skip to content

Commit 199b992

Browse files
committed
Fallout: add phantom data to librand
1 parent 2953710 commit 199b992

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/librand/distributions/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
use core::prelude::*;
2323
use core::num::{Float, Int};
24+
use core::marker::PhantomData;
2425

2526
use {Rng, Rand};
2627

@@ -56,7 +57,13 @@ pub trait IndependentSample<Support>: Sample<Support> {
5657

5758
/// A wrapper for generating types that implement `Rand` via the
5859
/// `Sample` & `IndependentSample` traits.
59-
pub struct RandSample<Sup>;
60+
pub struct RandSample<Sup> { _marker: PhantomData<Sup> }
61+
62+
impl<Sup> RandSample<Sup> {
63+
pub fn new() -> RandSample<Sup> {
64+
RandSample { _marker: PhantomData }
65+
}
66+
}
6067

6168
impl<Sup: Rand> Sample<Sup> for RandSample<Sup> {
6269
fn sample<R: Rng>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
@@ -285,7 +292,7 @@ mod tests {
285292

286293
#[test]
287294
fn test_rand_sample() {
288-
let mut rand_sample = RandSample::<ConstRand>;
295+
let mut rand_sample = RandSample::<ConstRand>::new();
289296

290297
assert_eq!(rand_sample.sample(&mut ::test::rng()), ConstRand(0));
291298
assert_eq!(rand_sample.ind_sample(&mut ::test::rng()), ConstRand(0));

src/librand/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern crate core;
4141
#[cfg(test)] #[macro_use] extern crate log;
4242

4343
use core::prelude::*;
44+
use core::marker::PhantomData;
4445

4546
pub use isaac::{IsaacRng, Isaac64Rng};
4647
pub use chacha::ChaChaRng;
@@ -206,7 +207,7 @@ pub trait Rng : Sized {
206207
/// .collect::<Vec<(f64, bool)>>());
207208
/// ```
208209
fn gen_iter<'a, T: Rand>(&'a mut self) -> Generator<'a, T, Self> {
209-
Generator { rng: self }
210+
Generator { rng: self, _marker: PhantomData }
210211
}
211212

212213
/// Generate a random value in the range [`low`, `high`).
@@ -317,6 +318,7 @@ pub trait Rng : Sized {
317318
/// This iterator is created via the `gen_iter` method on `Rng`.
318319
pub struct Generator<'a, T, R:'a> {
319320
rng: &'a mut R,
321+
_marker: PhantomData<T>
320322
}
321323

322324
impl<'a, T: Rand, R: Rng> Iterator for Generator<'a, T, R> {

0 commit comments

Comments
 (0)