1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

factor: Don’t wrap stdin in BufReader & use lock()

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.
This commit is contained in:
Paul Capron 2018-10-24 15:21:05 +02:00
parent bb9cc77858
commit 324cbad7a3

View file

@ -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<String>) -> 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);
}