Skip to content

Commit 0e1226a

Browse files
committed
bstr: just use StdinLock
There's no need to wrap a StdinLock in a BufReader because StdinLock already implements BufRead. I had originally written a lot of this code using Stdin instead of StdinLock, where Stdin does not implement BufRead. But StdinLock is the right thing to use here. Also, it is so much easier to use StdinLock now than it used to be. You used to have to do something like this: let stdin = io::stdin(); let stdin = stdin.lock(); Otherwise the lifetimes wouldn't work out. Turns out that we recently made a change to Stdin::lock such that it returns a StdinLock<'static> instead of a lifetime bound to Stdin. Which is so much nicer. Ref: rust-lang/rust#93965
1 parent 20e5cdd commit 0e1226a

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

content/post/bstr.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ use bstr::{io::BufReadExt, ByteSlice};
289289

290290
fn main() -> Result<(), Box<dyn std::error::Error>> {
291291
let needle = "Affiliate";
292-
for result in std::io::BufReader::new(std::io::stdin()).byte_lines() {
292+
for result in std::io::stdin().lock().byte_lines() {
293293
let line = result?;
294294
// '[T]::contains' is already defined in std and is not
295295
// substring search, so bstr defines it under a new name and
@@ -427,7 +427,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
427427

428428
fn main() -> Result<()> {
429429
let shiftor = ShiftOr::new("Sushi")?;
430-
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
430+
let mut rdr = std::io::stdin().lock();
431431
let mut line = String::new();
432432
loop {
433433
line.clear();
@@ -523,7 +523,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
523523

524524
fn main() -> Result<()> {
525525
let shiftor = ShiftOr::new("Sushi")?;
526-
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
526+
let mut rdr = std::io::stdin().lock();
527527
let mut line = Vec::new();
528528
loop {
529529
line.clear();
@@ -587,7 +587,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
587587

588588
fn main() -> Result<()> {
589589
let needle = "Sushi";
590-
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
590+
let mut rdr = std::io::stdin().lock();
591591
let mut line = String::new();
592592
loop {
593593
line.clear();
@@ -614,7 +614,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
614614

615615
fn main() -> Result<()> {
616616
let needle = "Sushi";
617-
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
617+
let mut rdr = std::io::stdin().lock();
618618
let mut line = Vec::new();
619619
loop {
620620
line.clear();
@@ -677,7 +677,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
677677

678678
fn main() -> Result<()> {
679679
let searcher = bstr::Finder::new("Sushi");
680-
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
680+
let mut rdr = std::io::stdin().lock();
681681
let mut line = Vec::new();
682682
loop {
683683
line.clear();
@@ -729,7 +729,7 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
729729

730730
fn main() -> Result<()> {
731731
let searcher = bstr::Finder::new("Sushi");
732-
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
732+
let mut rdr = std::io::stdin().lock();
733733
rdr.for_byte_line_with_terminator(|line| {
734734
if searcher.find(line).is_some() {
735735
std::io::stdout().write_all(line.as_bytes())?;
@@ -917,7 +917,7 @@ use bstr::{io::BufReadExt, ByteSlice};
917917
fn main() -> anyhow::Result<()> {
918918
let config = Config::parse(std::env::args_os())?;
919919
let (mut chars, mut words, mut lines) = (0, 0, 0);
920-
let mut bufrdr = io::BufReader::new(io::stdin().lock());
920+
let mut bufrdr = io::stdin().lock();
921921
bufrdr.for_byte_line_with_terminator(|line| {
922922
lines += 1;
923923
if config.chars {
@@ -1138,7 +1138,7 @@ use bstr::io::BufReadExt;
11381138
fn main() -> anyhow::Result<()> {
11391139
let config = Config::parse(std::env::args_os())?;
11401140
let searcher = bstr::Finder::new(&config.needle);
1141-
let mut bufrdr = io::BufReader::new(io::stdin().lock());
1141+
let mut bufrdr = io::stdin().lock();
11421142
let mut wtr = termcolor::StandardStream::stdout(ColorChoice::Auto);
11431143
let mut lineno = 0;
11441144
bufrdr.for_byte_line(|line| {
@@ -1239,7 +1239,7 @@ use termcolor::{ColorChoice, WriteColor};
12391239
/// badutf8 < stdin
12401240
/// foo ... | badutf8
12411241
fn main() -> anyhow::Result<()> {
1242-
let mut bufrdr = io::BufReader::new(io::stdin().lock());
1242+
let mut bufrdr = io::stdin().lock();
12431243
let mut wtr = termcolor::StandardStream::stdout(ColorChoice::Auto);
12441244
let mut lineno = 0;
12451245
bufrdr.for_byte_line(|mut line| {

0 commit comments

Comments
 (0)