2013-09-22 05:51:57 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-09 19:42:22 -05:00
|
|
|
//! A wrapper around any Read to treat it as an RNG.
|
2013-10-01 12:17:57 -05:00
|
|
|
|
2015-04-09 19:42:22 -05:00
|
|
|
#![allow(dead_code)]
|
|
|
|
|
|
|
|
use prelude::v1::*;
|
|
|
|
use io::prelude::*;
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
use rand::Rng;
|
2013-09-22 05:51:57 -05:00
|
|
|
|
2015-04-09 19:42:22 -05:00
|
|
|
/// An RNG that reads random bytes straight from a `Read`. This will
|
2013-10-01 11:18:57 -05:00
|
|
|
/// work best with an infinite reader, but this is not required.
|
|
|
|
///
|
2014-10-09 14:17:22 -05:00
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// It will panic if it there is insufficient data to fulfill a request.
|
2013-09-22 05:51:57 -05:00
|
|
|
pub struct ReaderRng<R> {
|
2014-03-27 17:10:38 -05:00
|
|
|
reader: R
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 19:42:22 -05:00
|
|
|
impl<R: Read> ReaderRng<R> {
|
|
|
|
/// Create a new `ReaderRng` from a `Read`.
|
2013-09-22 05:51:57 -05:00
|
|
|
pub fn new(r: R) -> ReaderRng<R> {
|
|
|
|
ReaderRng {
|
|
|
|
reader: r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:42:22 -05:00
|
|
|
impl<R: Read> Rng for ReaderRng<R> {
|
2013-09-22 05:51:57 -05:00
|
|
|
fn next_u32(&mut self) -> u32 {
|
2013-10-08 10:13:34 -05:00
|
|
|
// This is designed for speed: reading a LE integer on a LE
|
|
|
|
// platform just involves blitting the bytes into the memory
|
|
|
|
// of the u32, similarly for BE on BE; avoiding byteswapping.
|
2015-04-09 19:42:22 -05:00
|
|
|
let mut bytes = [0; 4];
|
|
|
|
self.fill_bytes(&mut bytes);
|
|
|
|
unsafe { *(bytes.as_ptr() as *const u32) }
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
fn next_u64(&mut self) -> u64 {
|
2013-10-08 10:13:34 -05:00
|
|
|
// see above for explanation.
|
2015-04-09 19:42:22 -05:00
|
|
|
let mut bytes = [0; 8];
|
|
|
|
self.fill_bytes(&mut bytes);
|
|
|
|
unsafe { *(bytes.as_ptr() as *const u64) }
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
2015-04-09 19:42:22 -05:00
|
|
|
fn fill_bytes(&mut self, mut v: &mut [u8]) {
|
2015-03-24 18:54:09 -05:00
|
|
|
while !v.is_empty() {
|
2015-04-09 19:42:22 -05:00
|
|
|
let t = v;
|
|
|
|
match self.reader.read(t) {
|
|
|
|
Ok(0) => panic!("ReaderRng.fill_bytes: EOF reached"),
|
|
|
|
Ok(n) => v = t.split_at_mut(n).1,
|
|
|
|
Err(e) => panic!("ReaderRng.fill_bytes: {}", e),
|
|
|
|
}
|
2013-10-01 11:18:57 -05:00
|
|
|
}
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-12-22 11:04:23 -06:00
|
|
|
use prelude::v1::*;
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
|
2014-03-01 18:23:04 -06:00
|
|
|
use super::ReaderRng;
|
2014-11-09 23:26:10 -06:00
|
|
|
use num::Int;
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
use rand::Rng;
|
2013-09-22 05:51:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reader_rng_u64() {
|
|
|
|
// transmute from the target to avoid endianness concerns.
|
2015-04-10 13:39:53 -05:00
|
|
|
let v = &[0, 0, 0, 0, 0, 0, 0, 1,
|
|
|
|
0 , 0, 0, 0, 0, 0, 0, 2,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 3][..];
|
|
|
|
let mut rng = ReaderRng::new(v);
|
2013-09-22 05:51:57 -05:00
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
assert_eq!(rng.next_u64(), 1.to_be());
|
|
|
|
assert_eq!(rng.next_u64(), 2.to_be());
|
|
|
|
assert_eq!(rng.next_u64(), 3.to_be());
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_reader_rng_u32() {
|
2015-04-10 13:39:53 -05:00
|
|
|
let v = &[0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3][..];
|
|
|
|
let mut rng = ReaderRng::new(v);
|
2013-09-22 05:51:57 -05:00
|
|
|
|
2015-03-03 02:42:26 -06:00
|
|
|
assert_eq!(rng.next_u32(), 1.to_be());
|
|
|
|
assert_eq!(rng.next_u32(), 2.to_be());
|
|
|
|
assert_eq!(rng.next_u32(), 3.to_be());
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_reader_rng_fill_bytes() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let v = [1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
let mut w = [0; 8];
|
2013-09-22 05:51:57 -05:00
|
|
|
|
2015-04-10 13:39:53 -05:00
|
|
|
let mut rng = ReaderRng::new(&v[..]);
|
2014-11-17 02:39:01 -06:00
|
|
|
rng.fill_bytes(&mut w);
|
2013-09-22 05:51:57 -05:00
|
|
|
|
2014-02-28 03:23:06 -06:00
|
|
|
assert!(v == w);
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|
2013-10-01 11:18:57 -05:00
|
|
|
|
|
|
|
#[test]
|
2015-01-31 17:08:25 -06:00
|
|
|
#[should_panic]
|
2013-10-01 11:18:57 -05:00
|
|
|
fn test_reader_rng_insufficient_bytes() {
|
2015-04-10 13:39:53 -05:00
|
|
|
let mut rng = ReaderRng::new(&[][..]);
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut v = [0; 3];
|
2014-11-17 02:39:01 -06:00
|
|
|
rng.fill_bytes(&mut v);
|
2013-10-01 11:18:57 -05:00
|
|
|
}
|
2013-09-22 05:51:57 -05:00
|
|
|
}
|