From d28e09de04b73ef58019eda4c66f38319f33ddc6 Mon Sep 17 00:00:00 2001 From: Konstantin Pospelov Date: Sat, 16 Dec 2017 19:26:25 +0300 Subject: [PATCH] join: do not wrap stdin in BufReader --- src/join/join.rs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/join/join.rs b/src/join/join.rs index 01dc150c1..48fefb7a7 100644 --- a/src/join/join.rs +++ b/src/join/join.rs @@ -15,7 +15,7 @@ extern crate getopts; extern crate uucore; use std::fs::File; -use std::io::{BufRead, BufReader, Lines, Read, stdin}; +use std::io::{BufRead, BufReader, Lines, Stdin, stdin}; use std::cmp::Ordering; static NAME: &'static str = "join"; @@ -77,20 +77,20 @@ impl Line { } } -struct State { +struct State<'a> { key: usize, print_unpaired: bool, - lines: Lines>>, + lines: Lines>, seq: Vec, } -impl State { - fn new(name: &str, key: usize, print_unpaired: bool) -> State { - let f: Box = if name == "-" { - Box::new(stdin()) as Box +impl<'a> State<'a> { + fn new(name: &str, stdin: &'a Stdin, key: usize, print_unpaired: bool) -> State<'a> { + let f = if name == "-" { + Box::new(stdin.lock()) as Box } else { match File::open(name) { - Ok(file) => Box::new(file) as Box, + Ok(file) => Box::new(BufReader::new(file)) as Box, Err(err) => crash!(1, "{}: {}", name, err), } }; @@ -98,7 +98,7 @@ impl State { State { key: key, print_unpaired: print_unpaired, - lines: BufReader::new(f).lines(), + lines: f.lines(), seq: Vec::new(), } } @@ -124,7 +124,7 @@ impl State { fn next_line(&mut self) { match self.read_line() { Some(line) => self.seq[0] = line, - None => self.seq.clear() + None => self.seq.clear(), } } @@ -282,14 +282,18 @@ standard output. The default join field is the first, delimited by blanks.", } fn exec(files: Vec, settings: &Settings) -> i32 { + let stdin = stdin(); + let mut state1 = State::new( &files[0], + &stdin, settings.key1, settings.print_unpaired == FileNum::File1, ); let mut state2 = State::new( &files[1], + &stdin, settings.key2, settings.print_unpaired == FileNum::File2, );