diff --git a/src/comm/comm.rs b/src/comm/comm.rs index 60a2b6beb..505a70a30 100644 --- a/src/comm/comm.rs +++ b/src/comm/comm.rs @@ -14,7 +14,7 @@ extern crate getopts; use std::cmp::Ord; use std::io::{BufferedReader, IoResult, print}; use std::io::fs::File; -use std::io::stdio::stdin; +use std::io::stdio::{stdin, StdReader}; use std::path::Path; static NAME : &'static str = "comm"; @@ -44,7 +44,21 @@ fn ensure_nl(line: String) -> String { } } -fn comm(a: &mut Box, b: &mut Box, opts: &getopts::Matches) { +enum LineReader { + Stdin(BufferedReader), + FileIn(BufferedReader) +} + +impl LineReader { + fn read_line(&mut self) -> IoResult { + match self { + &Stdin(ref mut r) => r.read_line(), + &FileIn(ref mut r) => r.read_line(), + } + } +} + +fn comm(a: &mut LineReader, b: &mut LineReader, opts: &getopts::Matches) { let delim = Vec::from_fn(4, |col| mkdelim(col, opts)); @@ -83,12 +97,12 @@ fn comm(a: &mut Box, b: &mut Box, opts: &getopts::Matches) { } } -fn open_file(name: &str) -> IoResult> { +fn open_file(name: &str) -> IoResult { match name { - "-" => Ok(box stdin() as Box), + "-" => Ok(LineReader::Stdin(stdin())), _ => { - let f = try!(File::open(&Path::new(name))); - Ok(box BufferedReader::new(f) as Box) + let f = try!(std::io::File::open(&Path::new(name))); + Ok(LineReader::FileIn(BufferedReader::new(f))) } } } diff --git a/src/shuf/shuf.rs b/src/shuf/shuf.rs index 4d5394be0..7441e0b32 100644 --- a/src/shuf/shuf.rs +++ b/src/shuf/shuf.rs @@ -19,7 +19,7 @@ use std::from_str::from_str; use std::io; use std::io::IoResult; use std::iter::{range_inclusive, RangeInclusive}; -use std::rand; +use std::rand::{mod, Rng}; use std::uint; #[path = "../common/util.rs"] @@ -151,14 +151,28 @@ fn shuf(input: Vec, mode: Mode, repeat: bool, zero: bool, count: uint, o } } +enum WrappedRng { + RngFile(rand::reader::ReaderRng), + RngDefault(rand::TaskRng), +} + +impl WrappedRng { + fn next_u32(&mut self) -> u32 { + match self { + &RngFile(ref mut r) => r.next_u32(), + &RngDefault(ref mut r) => r.next_u32(), + } + } +} + fn shuf_lines(mut lines: Vec, repeat: bool, zero: bool, count: uint, outname: Option, random: Option) -> IoResult<()> { let mut output = match outname { Some(name) => box io::BufferedWriter::new(try!(io::File::create(&Path::new(name)))) as Box, None => box io::stdout() as Box }; let mut rng = match random { - Some(name) => box rand::reader::ReaderRng::new(try!(io::File::open(&Path::new(name)))) as Box, - None => box rand::task_rng() as Box + Some(name) => RngFile(rand::reader::ReaderRng::new(try!(io::File::open(&Path::new(name))))), + None => RngDefault(rand::task_rng()), }; let mut len = lines.len(); let max = if repeat { count } else { cmp::min(count, len) };