Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async LineWriter fix #2131 #2477

Merged
merged 9 commits into from Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 47 additions & 2 deletions futures-util/src/io/buf_writer.rs
Expand Up @@ -6,6 +6,7 @@ use pin_project_lite::pin_project;
use std::fmt;
use std::io::{self, Write};
use std::pin::Pin;
use std::ptr;

pin_project! {
/// Wraps a writer and buffers its output.
Expand All @@ -31,7 +32,7 @@ pin_project! {
// TODO: Examples
pub struct BufWriter<W> {
#[pin]
inner: W,
pub(super) inner: W,
buf: Vec<u8>,
written: usize,
}
Expand All @@ -49,7 +50,7 @@ impl<W: AsyncWrite> BufWriter<W> {
Self { inner, buf: Vec::with_capacity(cap), written: 0 }
}

fn flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
pub(super) fn flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();

let len = this.buf.len();
Expand Down Expand Up @@ -83,6 +84,50 @@ impl<W: AsyncWrite> BufWriter<W> {
pub fn buffer(&self) -> &[u8] {
&self.buf
}

/// Capacity of `buf`. how many chars can be held in buffer
pub(super) fn capacity(&self) -> usize {
self.buf.capacity()
}

/// Remaining number of bytes to reach `buf` 's capacity
#[inline]
pub(super) fn spare_capacity(&self) -> usize {
self.buf.capacity() - self.buf.len()
}

/// Write a byte slice directly into buffer
///
/// Will truncate the number of bytes written to `spare_capacity()` so you want to
/// calculate the size of your slice to avoid losing bytes
///
/// Based on `std::io::BufWriter`
pub(super) fn write_to_buf(self: Pin<&mut Self>, buf: &[u8]) -> usize {
let available = self.spare_capacity();
let amt_to_buffer = available.min(buf.len());

// SAFETY: `amt_to_buffer` is <= buffer's spare capacity by construction.
unsafe {
self.write_to_buffer_unchecked(&buf[..amt_to_buffer]);
}

amt_to_buffer
}

/// Write byte slice directly into `self.buf`
///
/// Based on `std::io::BufWriter`
#[inline]
unsafe fn write_to_buffer_unchecked(self: Pin<&mut Self>, buf: &[u8]) {
debug_assert!(buf.len() <= self.spare_capacity());
let this = self.project();
let old_len = this.buf.len();
let buf_len = buf.len();
let src = buf.as_ptr();
let dst = this.buf.as_mut_ptr().add(old_len);
ptr::copy_nonoverlapping(src, dst, buf_len);
this.buf.set_len(old_len + buf_len);
}
}

impl<W: AsyncWrite> AsyncWrite for BufWriter<W> {
Expand Down
108 changes: 108 additions & 0 deletions futures-util/src/io/line_writer.rs
@@ -0,0 +1,108 @@
use super::buf_writer::BufWriter;
use futures_core::ready;
use futures_core::task::{Context, Poll};
use futures_io::AsyncWrite;
use pin_project_lite::pin_project;
use std::io;
use std::pin::Pin;

pin_project! {
/// Wrap a writer, like [`BufWriter`] does, but prioritizes buffering lines
///
/// This was written based on `std::io::LineWriter` which goes into further details
/// explaining the code.
///
/// Buffering is actually done using `BufWriter`. This class will leverage `BufWriter`
/// to write on-each-line.
#[derive(Debug)]
pub struct LineWriter<W: AsyncWrite> {
#[pin]
buf_writer: BufWriter<W>,
}
}

impl<W: AsyncWrite> LineWriter<W> {
/// Create a new `LineWriter` with default buffer capacity. The default is currently 1KB
/// which was taken from `std::io::LineWriter`
pub fn new(inner: W) -> LineWriter<W> {
LineWriter::with_capacity(1024, inner)
}

/// Creates a new `LineWriter` with the specified buffer capacity.
pub fn with_capacity(capacity: usize, inner: W) -> LineWriter<W> {
LineWriter { buf_writer: BufWriter::with_capacity(capacity, inner) }
}

/// Flush `buf_writer` if last char is "new line"
fn flush_if_completed_line(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
let this = self.project();
match this.buf_writer.buffer().last().copied() {
Some(b'\n') => this.buf_writer.flush_buf(cx),
_ => Poll::Ready(Ok(())),
}
}

/// Returns a reference to `buf_writer`'s internally buffered data.
pub fn buffer(&self) -> &[u8] {
&self.buf_writer.buffer()
}
/// Acquires a reference to the underlying sink or stream that this combinator is
/// pulling from.
pub fn get_ref(&self) -> &W {
self.buf_writer.get_ref()
}
}

impl<W: AsyncWrite> AsyncWrite for LineWriter<W> {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let mut this = self.as_mut().project();
let newline_index = match memchr::memrchr(b'\n', buf) {
None => {
ready!(self.as_mut().flush_if_completed_line(cx)?);
return self.project().buf_writer.poll_write(cx, buf);
}
Some(newline_index) => newline_index + 1,
};

ready!(this.buf_writer.as_mut().poll_flush(cx)?);

let lines = &buf[..newline_index];

let _buf_writer = this.buf_writer.project();
let flushed = ready!(_buf_writer.inner.poll_write(cx, lines))?;
FelipeLema marked this conversation as resolved.
Show resolved Hide resolved

if flushed == 0 {
return Poll::Ready(Ok(0));
}

let tail = if flushed >= newline_index {
&buf[flushed..]
} else if newline_index - flushed <= this.buf_writer.capacity() {
&buf[flushed..newline_index]
} else {
let scan_area = &buf[flushed..];
let scan_area = &scan_area[..this.buf_writer.capacity()];
match memchr::memrchr(b'\n', scan_area) {
Some(newline_index) => &scan_area[..newline_index + 1],
None => scan_area,
}
};

let buffered = _buf_writer.write_to_buf(tail); // TODO crap!
Poll::Ready(Ok(flushed + buffered))
}

/// Forward to `buf_writer` 's `BufWriter::poll_flush()`
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.as_mut().project().buf_writer.poll_flush(cx)
}

/// Forward to `buf_writer` 's `BufWriter::poll_close()`
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.as_mut().project().buf_writer.poll_close(cx)
}
}
3 changes: 3 additions & 0 deletions futures-util/src/io/mod.rs
Expand Up @@ -61,6 +61,9 @@ pub use self::buf_reader::BufReader;
mod buf_writer;
pub use self::buf_writer::BufWriter;

mod line_writer;
pub use self::line_writer::LineWriter;

mod chain;
pub use self::chain::Chain;

Expand Down
26 changes: 26 additions & 0 deletions futures/tests/io_line_writer.rs
@@ -0,0 +1,26 @@
use futures::executor::block_on;

use futures::io::{AsyncWriteExt, LineWriter};

#[test]
fn line_writer() {
let mut writer = LineWriter::new(Vec::new());

block_on(writer.write(&[0])).unwrap();
assert_eq!(*writer.get_ref(), []);

block_on(writer.write(&[1])).unwrap();
assert_eq!(*writer.get_ref(), []);

block_on(writer.flush()).unwrap();
assert_eq!(*writer.get_ref(), [0, 1]);

block_on(writer.write(&[0, b'\n', 1, b'\n', 2])).unwrap();
assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n']);

block_on(writer.flush()).unwrap();
assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2]);

block_on(writer.write(&[3, b'\n'])).unwrap();
assert_eq!(*writer.get_ref(), [0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n']);
}