|
|
@ -2,6 +2,7 @@ use std::fmt;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
|
|
use futures_core::ready;
|
|
|
|
use futures_core::ready;
|
|
|
|
|
|
|
|
use pin_project_lite::pin_project;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::io::write::WriteExt;
|
|
|
|
use crate::io::write::WriteExt;
|
|
|
|
use crate::io::{self, Seek, SeekFrom, Write};
|
|
|
|
use crate::io::{self, Seek, SeekFrom, Write};
|
|
|
@ -9,88 +10,88 @@ use crate::task::{Context, Poll};
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_CAPACITY: usize = 8 * 1024;
|
|
|
|
const DEFAULT_CAPACITY: usize = 8 * 1024;
|
|
|
|
|
|
|
|
|
|
|
|
/// Wraps a writer and buffers its output.
|
|
|
|
pin_project! {
|
|
|
|
///
|
|
|
|
/// Wraps a writer and buffers its output.
|
|
|
|
/// It can be excessively inefficient to work directly with something that
|
|
|
|
///
|
|
|
|
/// implements [`Write`]. For example, every call to
|
|
|
|
/// It can be excessively inefficient to work directly with something that
|
|
|
|
/// [`write`][`TcpStream::write`] on [`TcpStream`] results in a system call. A
|
|
|
|
/// implements [`Write`]. For example, every call to
|
|
|
|
/// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying
|
|
|
|
/// [`write`][`TcpStream::write`] on [`TcpStream`] results in a system call. A
|
|
|
|
/// writer in large, infrequent batches.
|
|
|
|
/// `BufWriter` keeps an in-memory buffer of data and writes it to an underlying
|
|
|
|
///
|
|
|
|
/// writer in large, infrequent batches.
|
|
|
|
/// `BufWriter` can improve the speed of programs that make *small* and
|
|
|
|
///
|
|
|
|
/// *repeated* write calls to the same file or network socket. It does not
|
|
|
|
/// `BufWriter` can improve the speed of programs that make *small* and
|
|
|
|
/// help when writing very large amounts at once, or writing just one or a few
|
|
|
|
/// *repeated* write calls to the same file or network socket. It does not
|
|
|
|
/// times. It also provides no advantage when writing to a destination that is
|
|
|
|
/// help when writing very large amounts at once, or writing just one or a few
|
|
|
|
/// in memory, like a `Vec<u8>`.
|
|
|
|
/// times. It also provides no advantage when writing to a destination that is
|
|
|
|
///
|
|
|
|
/// in memory, like a `Vec<u8>`.
|
|
|
|
/// When the `BufWriter` is dropped, the contents of its buffer will be written
|
|
|
|
///
|
|
|
|
/// out. However, any errors that happen in the process of flushing the buffer
|
|
|
|
/// When the `BufWriter` is dropped, the contents of its buffer will be written
|
|
|
|
/// when the writer is dropped will be ignored. Code that wishes to handle such
|
|
|
|
/// out. However, any errors that happen in the process of flushing the buffer
|
|
|
|
/// errors must manually call [`flush`] before the writer is dropped.
|
|
|
|
/// when the writer is dropped will be ignored. Code that wishes to handle such
|
|
|
|
///
|
|
|
|
/// errors must manually call [`flush`] before the writer is dropped.
|
|
|
|
/// This type is an async version of [`std::io::BufReader`].
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This type is an async version of [`std::io::BufReader`].
|
|
|
|
/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// [`std::io::BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// Let's write the numbers one through ten to a [`TcpStream`]:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Let's write the numbers one through ten to a [`TcpStream`]:
|
|
|
|
/// ```no_run
|
|
|
|
///
|
|
|
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
|
|
/// ```no_run
|
|
|
|
/// use async_std::net::TcpStream;
|
|
|
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
|
|
/// use async_std::prelude::*;
|
|
|
|
/// use async_std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// use async_std::prelude::*;
|
|
|
|
/// let mut stream = TcpStream::connect("127.0.0.1:34254").await?;
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let mut stream = TcpStream::connect("127.0.0.1:34254").await?;
|
|
|
|
/// for i in 0..10 {
|
|
|
|
///
|
|
|
|
/// let arr = [i+1];
|
|
|
|
/// for i in 0..10 {
|
|
|
|
/// stream.write(&arr).await?;
|
|
|
|
/// let arr = [i+1];
|
|
|
|
/// }
|
|
|
|
/// stream.write(&arr).await?;
|
|
|
|
/// #
|
|
|
|
/// }
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
/// #
|
|
|
|
/// ```
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// Because we're not buffering, we write each one in turn, incurring the
|
|
|
|
///
|
|
|
|
/// overhead of a system call per byte written. We can fix this with a
|
|
|
|
/// Because we're not buffering, we write each one in turn, incurring the
|
|
|
|
/// `BufWriter`:
|
|
|
|
/// overhead of a system call per byte written. We can fix this with a
|
|
|
|
///
|
|
|
|
/// `BufWriter`:
|
|
|
|
/// ```no_run
|
|
|
|
///
|
|
|
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
|
|
/// ```no_run
|
|
|
|
/// use async_std::io::BufWriter;
|
|
|
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
|
|
|
/// use async_std::net::TcpStream;
|
|
|
|
/// use async_std::io::BufWriter;
|
|
|
|
/// use async_std::prelude::*;
|
|
|
|
/// use async_std::net::TcpStream;
|
|
|
|
///
|
|
|
|
/// use async_std::prelude::*;
|
|
|
|
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
|
|
|
|
///
|
|
|
|
/// for i in 0..10 {
|
|
|
|
/// let mut stream = BufWriter::new(TcpStream::connect("127.0.0.1:34254").await?);
|
|
|
|
/// let arr = [i+1];
|
|
|
|
/// for i in 0..10 {
|
|
|
|
/// stream.write(&arr).await?;
|
|
|
|
/// let arr = [i+1];
|
|
|
|
/// };
|
|
|
|
/// stream.write(&arr).await?;
|
|
|
|
/// #
|
|
|
|
/// };
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
/// #
|
|
|
|
/// ```
|
|
|
|
/// # Ok(()) }) }
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
|
|
|
|
///
|
|
|
|
/// together by the buffer, and will all be written out in one system call when
|
|
|
|
/// By wrapping the stream with a `BufWriter`, these ten writes are all grouped
|
|
|
|
/// the `stream` is dropped.
|
|
|
|
/// together by the buffer, and will all be written out in one system call when
|
|
|
|
///
|
|
|
|
/// the `stream` is dropped.
|
|
|
|
/// [`Write`]: trait.Write.html
|
|
|
|
///
|
|
|
|
/// [`TcpStream::write`]: ../net/struct.TcpStream.html#method.write
|
|
|
|
/// [`Write`]: trait.Write.html
|
|
|
|
/// [`TcpStream`]: ../net/struct.TcpStream.html
|
|
|
|
/// [`TcpStream::write`]: ../net/struct.TcpStream.html#method.write
|
|
|
|
/// [`flush`]: trait.Write.html#tymethod.flush
|
|
|
|
/// [`TcpStream`]: ../net/struct.TcpStream.html
|
|
|
|
pub struct BufWriter<W> {
|
|
|
|
/// [`flush`]: trait.Write.html#tymethod.flush
|
|
|
|
inner: W,
|
|
|
|
pub struct BufWriter<W> {
|
|
|
|
buf: Vec<u8>,
|
|
|
|
#[pin]
|
|
|
|
written: usize,
|
|
|
|
inner: W,
|
|
|
|
|
|
|
|
buf: Vec<u8>,
|
|
|
|
|
|
|
|
written: usize,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct IntoInnerError<W>(W, std::io::Error);
|
|
|
|
pub struct IntoInnerError<W>(W, std::io::Error);
|
|
|
|
|
|
|
|
|
|
|
|
impl<W: Write> BufWriter<W> {
|
|
|
|
impl<W: Write> BufWriter<W> {
|
|
|
|
pin_utils::unsafe_pinned!(inner: W);
|
|
|
|
|
|
|
|
pin_utils::unsafe_unpinned!(buf: Vec<u8>);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
|
|
|
|
/// Creates a new `BufWriter` with a default buffer capacity. The default is currently 8 KB,
|
|
|
|
/// but may change in the future.
|
|
|
|
/// but may change in the future.
|
|
|
|
///
|
|
|
|
///
|
|
|
@ -178,6 +179,13 @@ impl<W: Write> BufWriter<W> {
|
|
|
|
&mut self.inner
|
|
|
|
&mut self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Gets a pinned mutable reference to the underlying writer.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// It is inadvisable to directly write to the underlying writer.
|
|
|
|
|
|
|
|
fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
|
|
|
|
|
|
|
|
self.project().inner
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Consumes BufWriter, returning the underlying writer
|
|
|
|
/// Consumes BufWriter, returning the underlying writer
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This method will not write leftover data, it will be lost.
|
|
|
|
/// This method will not write leftover data, it will be lost.
|
|
|
@ -234,16 +242,15 @@ impl<W: Write> BufWriter<W> {
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// [`LineWriter`]: struct.LineWriter.html
|
|
|
|
/// [`LineWriter`]: struct.LineWriter.html
|
|
|
|
fn poll_flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
fn poll_flush_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
let Self {
|
|
|
|
let mut this = self.project();
|
|
|
|
inner,
|
|
|
|
let len = this.buf.len();
|
|
|
|
buf,
|
|
|
|
|
|
|
|
written,
|
|
|
|
|
|
|
|
} = unsafe { Pin::get_unchecked_mut(self) };
|
|
|
|
|
|
|
|
let mut inner = unsafe { Pin::new_unchecked(inner) };
|
|
|
|
|
|
|
|
let len = buf.len();
|
|
|
|
|
|
|
|
let mut ret = Ok(());
|
|
|
|
let mut ret = Ok(());
|
|
|
|
while *written < len {
|
|
|
|
while *this.written < len {
|
|
|
|
match inner.as_mut().poll_write(cx, &buf[*written..]) {
|
|
|
|
match this
|
|
|
|
|
|
|
|
.inner
|
|
|
|
|
|
|
|
.as_mut()
|
|
|
|
|
|
|
|
.poll_write(cx, &this.buf[*this.written..])
|
|
|
|
|
|
|
|
{
|
|
|
|
Poll::Ready(Ok(0)) => {
|
|
|
|
Poll::Ready(Ok(0)) => {
|
|
|
|
ret = Err(io::Error::new(
|
|
|
|
ret = Err(io::Error::new(
|
|
|
|
io::ErrorKind::WriteZero,
|
|
|
|
io::ErrorKind::WriteZero,
|
|
|
@ -251,7 +258,7 @@ impl<W: Write> BufWriter<W> {
|
|
|
|
));
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Ready(Ok(n)) => *written += n,
|
|
|
|
Poll::Ready(Ok(n)) => *this.written += n,
|
|
|
|
Poll::Ready(Err(ref e)) if e.kind() == io::ErrorKind::Interrupted => {}
|
|
|
|
Poll::Ready(Err(ref e)) if e.kind() == io::ErrorKind::Interrupted => {}
|
|
|
|
Poll::Ready(Err(e)) => {
|
|
|
|
Poll::Ready(Err(e)) => {
|
|
|
|
ret = Err(e);
|
|
|
|
ret = Err(e);
|
|
|
@ -260,10 +267,10 @@ impl<W: Write> BufWriter<W> {
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *written > 0 {
|
|
|
|
if *this.written > 0 {
|
|
|
|
buf.drain(..*written);
|
|
|
|
this.buf.drain(..*this.written);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*written = 0;
|
|
|
|
*this.written = 0;
|
|
|
|
Poll::Ready(ret)
|
|
|
|
Poll::Ready(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -278,20 +285,20 @@ impl<W: Write> Write for BufWriter<W> {
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buf.len() >= self.buf.capacity() {
|
|
|
|
if buf.len() >= self.buf.capacity() {
|
|
|
|
self.inner().poll_write(cx, buf)
|
|
|
|
self.get_pin_mut().poll_write(cx, buf)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Pin::new(&mut *self.buf()).poll_write(cx, buf)
|
|
|
|
Pin::new(&mut *self.project().buf).poll_write(cx, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
self.inner().poll_flush(cx)
|
|
|
|
self.get_pin_mut().poll_flush(cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
self.inner().poll_close(cx)
|
|
|
|
self.get_pin_mut().poll_close(cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -314,6 +321,6 @@ impl<W: Write + Seek> Seek for BufWriter<W> {
|
|
|
|
pos: SeekFrom,
|
|
|
|
pos: SeekFrom,
|
|
|
|
) -> Poll<io::Result<u64>> {
|
|
|
|
) -> Poll<io::Result<u64>> {
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
ready!(self.as_mut().poll_flush_buf(cx))?;
|
|
|
|
self.inner().poll_seek(cx, pos)
|
|
|
|
self.get_pin_mut().poll_seek(cx, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|