use std::io;
use tokio::io::{AsyncBufReadExt as _, AsyncRead, BufReader};
const RETAINED_LINE: usize = 16 * 1024;
pub(super) struct LineReader<R> {
reader: BufReader<R>,
buf: Vec<u8>,
handed_out: bool,
}
impl<R: AsyncRead + Unpin> LineReader<R> {
pub(super) fn new(reader: BufReader<R>) -> Self {
Self {
reader,
buf: Vec::new(),
handed_out: false,
}
}
#[cfg(unix)]
pub(super) fn get_ref(&self) -> &BufReader<R> {
&self.reader
}
pub(super) async fn read_line(&mut self) -> io::Result<Option<&str>> {
if self.handed_out {
self.release();
}
let read = self.reader.read_until(b'\n', &mut self.buf).await?;
if read == 0 && self.buf.is_empty() {
return Ok(None);
}
self.handed_out = true;
if self.buf.ends_with(b"\n") {
self.buf.pop();
if self.buf.ends_with(b"\r") {
self.buf.pop();
}
}
match core::str::from_utf8(&self.buf) {
Ok(line) => Ok(Some(line)),
Err(_) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
)),
}
}
pub(super) async fn read_line_owned(&mut self) -> io::Result<Option<String>> {
let line = self.read_line().await?.map(str::to_owned);
self.release();
Ok(line)
}
fn release(&mut self) {
self.buf.clear();
self.buf.shrink_to(RETAINED_LINE);
self.handed_out = false;
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use tokio::io::{AsyncWriteExt as _, DuplexStream, duplex};
use tokio::time::timeout;
use super::*;
fn over(stream: DuplexStream) -> LineReader<DuplexStream> {
LineReader::new(BufReader::new(stream))
}
#[tokio::test(start_paused = true)]
async fn a_run_of_lines_comes_back_one_at_a_time() {
let (mut writer, reader) = duplex(64);
writer.write_all(b"one\ntwo\n\nfour\n").await.unwrap();
drop(writer);
let mut lines = over(reader);
assert_eq!(lines.read_line().await.unwrap(), Some("one"));
assert_eq!(lines.read_line().await.unwrap(), Some("two"));
assert_eq!(lines.read_line().await.unwrap(), Some(""));
assert_eq!(lines.read_line().await.unwrap(), Some("four"));
assert_eq!(lines.read_line().await.unwrap(), None);
}
#[tokio::test(start_paused = true)]
async fn a_carriage_return_before_the_newline_is_not_part_of_the_line() {
let (mut writer, reader) = duplex(64);
writer.write_all(b"crlf\r\nbare\rcr\r\n").await.unwrap();
drop(writer);
let mut lines = over(reader);
assert_eq!(lines.read_line().await.unwrap(), Some("crlf"));
assert_eq!(lines.read_line().await.unwrap(), Some("bare\rcr"));
}
#[tokio::test(start_paused = true)]
async fn a_last_line_with_no_terminator_is_still_a_line() {
let (mut writer, reader) = duplex(64);
writer.write_all(b"first\nlast").await.unwrap();
drop(writer);
let mut lines = over(reader);
assert_eq!(lines.read_line().await.unwrap(), Some("first"));
assert_eq!(lines.read_line().await.unwrap(), Some("last"));
assert_eq!(lines.read_line().await.unwrap(), None);
}
#[tokio::test(start_paused = true)]
async fn a_line_that_is_not_utf8_is_an_error_not_a_lossy_line() {
let (mut writer, reader) = duplex(64);
writer.write_all(b"\xff\xfe\nafter\n").await.unwrap();
drop(writer);
let mut lines = over(reader);
let error = lines.read_line().await.unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidData);
assert_eq!(lines.read_line().await.unwrap(), Some("after"));
}
#[tokio::test(start_paused = true)]
async fn a_line_cancelled_part_way_is_whole_on_the_next_call() {
let (mut writer, reader) = duplex(64);
let mut lines = over(reader);
writer.write_all(b"par").await.unwrap();
let cancelled = timeout(Duration::from_secs(1), lines.read_line()).await;
assert!(cancelled.is_err(), "no line is complete yet");
writer.write_all(b"tial\nnext\n").await.unwrap();
assert_eq!(lines.read_line().await.unwrap(), Some("partial"));
assert_eq!(lines.read_line().await.unwrap(), Some("next"));
}
#[tokio::test(start_paused = true)]
async fn a_cancellation_between_lines_does_not_replay_the_last_one() {
let (mut writer, reader) = duplex(64);
let mut lines = over(reader);
writer.write_all(b"done\n").await.unwrap();
assert_eq!(lines.read_line().await.unwrap(), Some("done"));
let cancelled = timeout(Duration::from_secs(1), lines.read_line()).await;
assert!(cancelled.is_err(), "nothing more was written");
writer.write_all(b"fresh\n").await.unwrap();
assert_eq!(lines.read_line().await.unwrap(), Some("fresh"));
}
#[tokio::test(start_paused = true)]
async fn a_very_long_line_does_not_leave_its_capacity_behind() {
let (mut writer, reader) = duplex(64);
let mut lines = over(reader);
let long = "x".repeat(RETAINED_LINE * 4);
let send = async move {
writer.write_all(long.as_bytes()).await.unwrap();
writer.write_all(b"\nshort\n").await.unwrap();
};
let (_, ()) = tokio::join!(send, async {
assert_eq!(
lines.read_line().await.unwrap().map(str::len),
Some(RETAINED_LINE * 4)
);
assert_eq!(lines.read_line().await.unwrap(), Some("short"));
assert!(
lines.buf.capacity() <= RETAINED_LINE,
"the scratch buffer kept {} bytes after one long line",
lines.buf.capacity()
);
});
}
#[tokio::test(start_paused = true)]
async fn a_copied_out_long_line_is_not_held_a_second_time() {
let (mut writer, reader) = duplex(64);
let mut lines = over(reader);
let long = "x".repeat(RETAINED_LINE * 4);
let send = async move {
writer.write_all(long.as_bytes()).await.unwrap();
writer.write_all(b"\n").await.unwrap();
};
let (_, ()) = tokio::join!(send, async {
let line = lines.read_line_owned().await.unwrap();
assert_eq!(line.map(|line| line.len()), Some(RETAINED_LINE * 4));
assert!(
lines.buf.capacity() <= RETAINED_LINE,
"the scratch buffer kept {} bytes beside the copy",
lines.buf.capacity()
);
});
}
#[tokio::test(start_paused = true)]
async fn a_line_longer_than_the_reader_buffer_comes_back_whole() {
let (mut writer, reader) = duplex(64);
let mut lines = LineReader::new(BufReader::with_capacity(8, reader));
let long = "x".repeat(100);
let send = async move {
writer.write_all(long.as_bytes()).await.unwrap();
writer.write_all(b"\n").await.unwrap();
};
let (_, line) = tokio::join!(send, async {
lines.read_line().await.unwrap().map(str::to_owned)
});
assert_eq!(line.as_deref(), Some("x".repeat(100).as_str()));
}
}