]> git.proxmox.com Git - rustc.git/blob - src/libstd/rand/reader.rs
08bc809ed4d4632ac29dc8f5422487ce2dacfa1a
[rustc.git] / src / libstd / rand / reader.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! A wrapper around any Read to treat it as an RNG.
12
13 #![allow(dead_code)]
14
15 use io::prelude::*;
16 use rand::Rng;
17
18 /// An RNG that reads random bytes straight from a `Read`. This will
19 /// work best with an infinite reader, but this is not required.
20 ///
21 /// # Panics
22 ///
23 /// It will panic if it there is insufficient data to fulfill a request.
24 pub struct ReaderRng<R> {
25 reader: R
26 }
27
28 impl<R: Read> ReaderRng<R> {
29 /// Create a new `ReaderRng` from a `Read`.
30 pub fn new(r: R) -> ReaderRng<R> {
31 ReaderRng {
32 reader: r
33 }
34 }
35 }
36
37 impl<R: Read> Rng for ReaderRng<R> {
38 fn next_u32(&mut self) -> u32 {
39 // This is designed for speed: reading a LE integer on a LE
40 // platform just involves blitting the bytes into the memory
41 // of the u32, similarly for BE on BE; avoiding byteswapping.
42 let mut bytes = [0; 4];
43 self.fill_bytes(&mut bytes);
44 unsafe { *(bytes.as_ptr() as *const u32) }
45 }
46 fn next_u64(&mut self) -> u64 {
47 // see above for explanation.
48 let mut bytes = [0; 8];
49 self.fill_bytes(&mut bytes);
50 unsafe { *(bytes.as_ptr() as *const u64) }
51 }
52 fn fill_bytes(&mut self, mut v: &mut [u8]) {
53 while !v.is_empty() {
54 let t = v;
55 match self.reader.read(t) {
56 Ok(0) => panic!("ReaderRng.fill_bytes: EOF reached"),
57 Ok(n) => v = t.split_at_mut(n).1,
58 Err(e) => panic!("ReaderRng.fill_bytes: {}", e),
59 }
60 }
61 }
62 }
63
64 #[cfg(test)]
65 mod tests {
66 use super::ReaderRng;
67 use rand::Rng;
68
69 #[test]
70 fn test_reader_rng_u64() {
71 // transmute from the target to avoid endianness concerns.
72 let v = &[0, 0, 0, 0, 0, 0, 0, 1,
73 0, 0, 0, 0, 0, 0, 0, 2,
74 0, 0, 0, 0, 0, 0, 0, 3][..];
75 let mut rng = ReaderRng::new(v);
76
77 assert_eq!(rng.next_u64(), 1u64.to_be());
78 assert_eq!(rng.next_u64(), 2u64.to_be());
79 assert_eq!(rng.next_u64(), 3u64.to_be());
80 }
81 #[test]
82 fn test_reader_rng_u32() {
83 let v = &[0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3][..];
84 let mut rng = ReaderRng::new(v);
85
86 assert_eq!(rng.next_u32(), 1u32.to_be());
87 assert_eq!(rng.next_u32(), 2u32.to_be());
88 assert_eq!(rng.next_u32(), 3u32.to_be());
89 }
90 #[test]
91 fn test_reader_rng_fill_bytes() {
92 let v = [1, 2, 3, 4, 5, 6, 7, 8];
93 let mut w = [0; 8];
94
95 let mut rng = ReaderRng::new(&v[..]);
96 rng.fill_bytes(&mut w);
97
98 assert!(v == w);
99 }
100
101 #[test]
102 #[should_panic]
103 fn test_reader_rng_insufficient_bytes() {
104 let mut rng = ReaderRng::new(&[][..]);
105 let mut v = [0; 3];
106 rng.fill_bytes(&mut v);
107 }
108 }