nushell/crates/nu-protocol/src/util.rs
Michael Angerman 95cd9dd2b2
move BufferedReader out of nu-command (#7697)
src/main.rs has a dependency on BufferedReader
which is currently located in nu_command.

I am moving BufferedReader to a more relevant
location (crate) which will allow / eliminate main's dependency
on nu_command in a benchmark / testing environment...

now that @rgwood  has landed benches I want
to start experimenting with benchmarks related
to the parser.

For benchmark purposes when dealing with parsing
you need a very simple set of commands that show
how well the parser is doing, in other words
just the core commands... Not all of nu_command...

Having a smaller nu binary when running the benchmark CI
would enable building nushell quickly, yet still show us
how well the parser is performing...

Once this PR lands the only dependency main will have
on nu_command is create_default_context ---
meaning for benchmark purposes we can swap in a tiny
crate of commands instead of the gigantic nu_command
which has its "own" create_default_context...

It will also enable other crates going forward to
use BufferedReader.  Right now it is not accessible
to other lower level crates because it is located in a
"top of the stack crate".
2023-01-06 15:22:17 -08:00

37 lines
857 B
Rust

use crate::ShellError;
use std::io::{BufRead, BufReader, Read};
pub struct BufferedReader<R: Read> {
pub input: BufReader<R>,
}
impl<R: Read> BufferedReader<R> {
pub fn new(input: BufReader<R>) -> Self {
Self { input }
}
}
impl<R: Read> Iterator for BufferedReader<R> {
type Item = Result<Vec<u8>, ShellError>;
fn next(&mut self) -> Option<Self::Item> {
let buffer = self.input.fill_buf();
match buffer {
Ok(s) => {
let result = s.to_vec();
let buffer_len = s.len();
if buffer_len == 0 {
None
} else {
self.input.consume(buffer_len);
Some(Ok(result))
}
}
Err(e) => Some(Err(ShellError::IOError(e.to_string()))),
}
}
}