1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 04:27:45 +00:00

Don’t wrap stdin in a BufReader when prompting

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.
This commit is contained in:
Paul Capron 2018-10-24 15:08:51 +02:00
parent a673c12694
commit bb9cc77858
3 changed files with 6 additions and 6 deletions

View file

@ -42,7 +42,7 @@ use clap::{App, Arg, ArgMatches};
use quick_error::ResultExt; use quick_error::ResultExt;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs; use std::fs;
use std::io::{stdin, stdout, BufRead, BufReader, Write}; use std::io::{stdin, stdout, Write};
use std::io; use std::io;
use std::path::{Path, PathBuf, StripPrefixError}; use std::path::{Path, PathBuf, StripPrefixError};
use std::str::FromStr; use std::str::FromStr;
@ -123,7 +123,7 @@ macro_rules! prompt_yes(
print!(" [y/N]: "); print!(" [y/N]: ");
crash_if_err!(1, stdout().flush()); crash_if_err!(1, stdout().flush());
let mut s = String::new(); 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) { Ok(_) => match s.char_indices().nth(0) {
Some((_, x)) => x == 'y' || x == 'Y', Some((_, x)) => x == 'y' || x == 'Y',
_ => false _ => false

View file

@ -13,7 +13,7 @@
extern crate uucore; extern crate uucore;
use std::fs; use std::fs;
use std::io::{stdin, BufRead, BufReader, Result}; use std::io::{stdin, Result};
#[cfg(any(unix, target_os = "redox"))] #[cfg(any(unix, target_os = "redox"))]
use std::os::unix::fs::symlink; use std::os::unix::fs::symlink;
#[cfg(windows)] #[cfg(windows)]
@ -303,7 +303,7 @@ fn link(src: &PathBuf, dst: &PathBuf, settings: &Settings) -> Result<()> {
fn read_yes() -> bool { fn read_yes() -> bool {
let mut s = String::new(); 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) { Ok(_) => match s.char_indices().nth(0) {
Some((_, x)) => x == 'y' || x == 'Y', Some((_, x)) => x == 'y' || x == 'Y',
_ => false, _ => false,

View file

@ -16,7 +16,7 @@ extern crate uucore;
use std::fs; use std::fs;
use std::env; use std::env;
use std::io::{stdin, BufRead, BufReader, Result}; use std::io::{stdin, Result};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
static NAME: &str = "mv"; static NAME: &str = "mv";
@ -374,7 +374,7 @@ fn rename(from: &PathBuf, to: &PathBuf, b: &Behaviour) -> Result<()> {
fn read_yes() -> bool { fn read_yes() -> bool {
let mut s = String::new(); 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) { Ok(_) => match s.chars().nth(0) {
Some(x) => x == 'y' || x == 'Y', Some(x) => x == 'y' || x == 'Y',
_ => false, _ => false,