Skip to content

Commit

Permalink
bstr: just use StdinLock
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BurntSushi committed Sep 8, 2022
1 parent 20e5cdd commit 0e1226a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions content/post/bstr.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ use bstr::{io::BufReadExt, ByteSlice};

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

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

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

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

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

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

fn main() -> Result<()> {
let searcher = bstr::Finder::new("Sushi");
let mut rdr = std::io::BufReader::new(std::io::stdin().lock());
let mut rdr = std::io::stdin().lock();
rdr.for_byte_line_with_terminator(|line| {
if searcher.find(line).is_some() {
std::io::stdout().write_all(line.as_bytes())?;
Expand Down Expand Up @@ -917,7 +917,7 @@ use bstr::{io::BufReadExt, ByteSlice};
fn main() -> anyhow::Result<()> {
let config = Config::parse(std::env::args_os())?;
let (mut chars, mut words, mut lines) = (0, 0, 0);
let mut bufrdr = io::BufReader::new(io::stdin().lock());
let mut bufrdr = io::stdin().lock();
bufrdr.for_byte_line_with_terminator(|line| {
lines += 1;
if config.chars {
Expand Down Expand Up @@ -1138,7 +1138,7 @@ use bstr::io::BufReadExt;
fn main() -> anyhow::Result<()> {
let config = Config::parse(std::env::args_os())?;
let searcher = bstr::Finder::new(&config.needle);
let mut bufrdr = io::BufReader::new(io::stdin().lock());
let mut bufrdr = io::stdin().lock();
let mut wtr = termcolor::StandardStream::stdout(ColorChoice::Auto);
let mut lineno = 0;
bufrdr.for_byte_line(|line| {
Expand Down Expand Up @@ -1239,7 +1239,7 @@ use termcolor::{ColorChoice, WriteColor};
/// badutf8 < stdin
/// foo ... | badutf8
fn main() -> anyhow::Result<()> {
let mut bufrdr = io::BufReader::new(io::stdin().lock());
let mut bufrdr = io::stdin().lock();
let mut wtr = termcolor::StandardStream::stdout(ColorChoice::Auto);
let mut lineno = 0;
bufrdr.for_byte_line(|mut line| {
Expand Down

0 comments on commit 0e1226a

Please sign in to comment.