about summary refs log tree commit diff
path: root/src/libstd/rand/reader.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-04-09 17:42:22 -0700
committerAlex Crichton <alex@alexcrichton.com>2015-04-14 10:14:11 -0700
commitbf4e77d4b543632ca4df8fdd7092850dffc3954b (patch)
treec4b56d2a5974e1b3bf4bfc8b7ca1a62d64c2c341 /src/libstd/rand/reader.rs
parentdabf0c6371d3b193664f58746fa27c1835a010f3 (diff)
downloadrust-bf4e77d4b543632ca4df8fdd7092850dffc3954b.tar.gz
rust-bf4e77d4b543632ca4df8fdd7092850dffc3954b.zip
std: Remove old_io/old_path/rand modules
This commit entirely removes the old I/O, path, and rand modules. All
functionality has been deprecated and unstable for quite some time now!
Diffstat (limited to 'src/libstd/rand/reader.rs')
-rw-r--r--src/libstd/rand/reader.rs56
1 files changed, 23 insertions, 33 deletions
diff --git a/src/libstd/rand/reader.rs b/src/libstd/rand/reader.rs
index ece6867ddca..3d0055b43c7 100644
--- a/src/libstd/rand/reader.rs
+++ b/src/libstd/rand/reader.rs
@@ -8,35 +8,26 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-//! A wrapper around any Reader to treat it as an RNG.
+//! A wrapper around any Read to treat it as an RNG.
 
-use old_io::Reader;
+#![allow(dead_code)]
+
+use prelude::v1::*;
+use io::prelude::*;
 use rand::Rng;
-use result::Result::{Ok, Err};
 
-/// An RNG that reads random bytes straight from a `Reader`. This will
+/// An RNG that reads random bytes straight from a `Read`. This will
 /// work best with an infinite reader, but this is not required.
 ///
 /// # Panics
 ///
 /// It will panic if it there is insufficient data to fulfill a request.
-///
-/// # Examples
-///
-/// ```
-/// # #![feature(rand, old_io)]
-/// use std::rand::{reader, Rng};
-/// use std::old_io::MemReader;
-///
-/// let mut rng = reader::ReaderRng::new(MemReader::new(vec!(1,2,3,4,5,6,7,8)));
-/// println!("{:x}", rng.gen::<usize>());
-/// ```
 pub struct ReaderRng<R> {
     reader: R
 }
 
-impl<R: Reader> ReaderRng<R> {
-    /// Create a new `ReaderRng` from a `Reader`.
+impl<R: Read> ReaderRng<R> {
+    /// Create a new `ReaderRng` from a `Read`.
     pub fn new(r: R) -> ReaderRng<R> {
         ReaderRng {
             reader: r
@@ -44,30 +35,29 @@ impl<R: Reader> ReaderRng<R> {
     }
 }
 
-impl<R: Reader> Rng for ReaderRng<R> {
+impl<R: Read> Rng for ReaderRng<R> {
     fn next_u32(&mut self) -> u32 {
         // 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.
-        if cfg!(target_endian="little") {
-            self.reader.read_le_u32().unwrap()
-        } else {
-            self.reader.read_be_u32().unwrap()
-        }
+        let mut bytes = [0; 4];
+        self.fill_bytes(&mut bytes);
+        unsafe { *(bytes.as_ptr() as *const u32) }
     }
     fn next_u64(&mut self) -> u64 {
         // see above for explanation.
-        if cfg!(target_endian="little") {
-            self.reader.read_le_u64().unwrap()
-        } else {
-            self.reader.read_be_u64().unwrap()
-        }
+        let mut bytes = [0; 8];
+        self.fill_bytes(&mut bytes);
+        unsafe { *(bytes.as_ptr() as *const u64) }
     }
-    fn fill_bytes(&mut self, v: &mut [u8]) {
-        if v.len() == 0 { return }
-        match self.reader.read_at_least(v.len(), v) {
-            Ok(_) => {}
-            Err(e) => panic!("ReaderRng.fill_bytes error: {:?}", e)
+    fn fill_bytes(&mut self, mut v: &mut [u8]) {
+        while v.len() > 0 {
+            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),
+            }
         }
     }
 }