From bb9cc77858e7d487d48a2fad80de138e2011f570 Mon Sep 17 00:00:00 2001 From: Paul Capron Date: Wed, 24 Oct 2018 15:08:51 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Don=E2=80=99t=20wrap=20stdin=20in=20a=20Buf?= =?UTF-8?q?Reader=20when=20prompting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit stdin() is already buffered. stdin().read_line() calls stdin().lock() behind the hood (see https://doc.rust-lang.org/src/std/io/stdio.rs.html#274) Here we are reading user input, prompting them to confirm their action: it seems useless to handle mutex locking/unlocking explicitely and beforehand to avoid its overhead. This commit is related to issue #1103. --- src/cp/cp.rs | 4 ++-- src/ln/ln.rs | 4 ++-- src/mv/mv.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cp/cp.rs b/src/cp/cp.rs index de05619c4..ae04475b1 100644 --- a/src/cp/cp.rs +++ b/src/cp/cp.rs @@ -42,7 +42,7 @@ use clap::{App, Arg, ArgMatches}; use quick_error::ResultExt; use std::collections::HashSet; use std::fs; -use std::io::{stdin, stdout, BufRead, BufReader, Write}; +use std::io::{stdin, stdout, Write}; use std::io; use std::path::{Path, PathBuf, StripPrefixError}; use std::str::FromStr; @@ -123,7 +123,7 @@ macro_rules! prompt_yes( print!(" [y/N]: "); crash_if_err!(1, stdout().flush()); let mut s = String::new(); - match BufReader::new(stdin()).read_line(&mut s) { + match stdin().read_line(&mut s) { Ok(_) => match s.char_indices().nth(0) { Some((_, x)) => x == 'y' || x == 'Y', _ => false diff --git a/src/ln/ln.rs b/src/ln/ln.rs index d182bd688..d6c89f50d 100644 --- a/src/ln/ln.rs +++ b/src/ln/ln.rs @@ -13,7 +13,7 @@ extern crate uucore; use std::fs; -use std::io::{stdin, BufRead, BufReader, Result}; +use std::io::{stdin, Result}; #[cfg(any(unix, target_os = "redox"))] use std::os::unix::fs::symlink; #[cfg(windows)] @@ -303,7 +303,7 @@ fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> { fn read_yes() -> bool { let mut s = String::new(); - match BufReader::new(stdin()).read_line(&mut s) { + match stdin().read_line(&mut s) { Ok(_) => match s.char_indices().nth(0) { Some((_, x)) => x == 'y' || x == 'Y', _ => false, diff --git a/src/mv/mv.rs b/src/mv/mv.rs index 1b1ee68db..19746c956 100644 --- a/src/mv/mv.rs +++ b/src/mv/mv.rs @@ -16,7 +16,7 @@ extern crate uucore; use std::fs; use std::env; -use std::io::{stdin, BufRead, BufReader, Result}; +use std::io::{stdin, Result}; use std::path::{Path, PathBuf}; static NAME: &str = "mv"; @@ -374,7 +374,7 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behaviour) -> Result<()> { fn read_yes() -> bool { let mut s = String::new(); - match BufReader::new(stdin()).read_line(&mut s) { + match stdin().read_line(&mut s) { Ok(_) => match s.chars().nth(0) { Some(x) => x == 'y' || x == 'Y', _ => false, From 324cbad7a38f6b6206dff3c7c35e0d9398137a11 Mon Sep 17 00:00:00 2001 From: Paul Capron Date: Wed, 24 Oct 2018 15:21:05 +0200 Subject: [PATCH 2/2] =?UTF-8?q?factor:=20Don=E2=80=99t=20wrap=20stdin=20in?= =?UTF-8?q?=20BufReader=20&=20use=20lock()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit stdin() is already buffered. Locking upfront explicitely avoid the overhead of mutex locking/unlocking everytime a new line is read. See https://stackoverflow.com/a/17546731 and https://www.reddit.com/r/rust/comments/3rj54u/how_can_i_read_char_by_char_from_stdin/cwpojn1/ The code cannot be simplified to “for line in stdin().lock().lines()” until non-lexical lifetimes are implemented. The compiler complains at the moment about a temporary value not living long enough/being dropped while still borrowed. See https://github.com/rust-lang/rust/issues/33520 This commit is related to issue #1103. --- src/factor/factor.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/factor/factor.rs b/src/factor/factor.rs index 97b92a4d3..2f5d5bb2f 100644 --- a/src/factor/factor.rs +++ b/src/factor/factor.rs @@ -23,7 +23,7 @@ use rand::distributions::{Distribution, Uniform}; use rand::{SeedableRng, thread_rng}; use rand::rngs::SmallRng; use std::cmp::{max, min}; -use std::io::{stdin, BufRead, BufReader}; +use std::io::{stdin, BufRead}; use std::num::Wrapping; use std::mem::swap; @@ -163,7 +163,8 @@ pub fn uumain(args: Vec) -> i32 { let matches = new_coreopts!(SYNTAX, SUMMARY, LONG_HELP).parse(args); if matches.free.is_empty() { - for line in BufReader::new(stdin()).lines() { + let stdin = stdin(); + for line in stdin.lock().lines() { for number in line.unwrap().split_whitespace() { print_factors_str(number); }