pub struct BufWriter<W> { /* private fields */ }
Expand description

Wraps a writer and buffers its output.

It can be excessively inefficient to work directly with something that implements AsyncWrite. A 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 help when writing very large amounts at once, or writing just one or a few 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 discarded. Creating multiple instances of a BufWriter on the same stream can cause data loss. If you need to write out the contents of its buffer, you must manually call flush before the writer is dropped.

Implementations§

§

impl<W> BufWriter<W>where W: AsyncWrite,

pub fn new(inner: W) -> BufWriter<W>

Creates a new BufWriter with a default buffer capacity. The default is currently 8 KB, but may change in the future.

pub fn with_capacity(cap: usize, inner: W) -> BufWriter<W>

Creates a new BufWriter with the specified buffer capacity.

pub fn get_ref(&self) -> &W

Acquires a reference to the underlying sink or stream that this combinator is pulling from.

pub fn get_mut(&mut self) -> &mut W

Acquires a mutable reference to the underlying sink or stream that this combinator is pulling from.

Note that care must be taken to avoid tampering with the state of the sink or stream which may otherwise confuse this combinator.

pub fn get_pin_mut(self: Pin<&mut BufWriter<W>>) -> Pin<&mut W>

Acquires a pinned mutable reference to the underlying sink or stream that this combinator is pulling from.

Note that care must be taken to avoid tampering with the state of the sink or stream which may otherwise confuse this combinator.

pub fn into_inner(self) -> W

Consumes this combinator, returning the underlying sink or stream.

Note that this may discard intermediate state of this combinator, so care should be taken to avoid losing resources when this is called.

pub fn buffer(&self) -> &[u8]

Returns a reference to the internally buffered data.

Trait Implementations§

§

impl<W> AsyncBufRead for BufWriter<W>where W: AsyncBufRead,

§

fn poll_fill_buf( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_> ) -> Poll<Result<&[u8], Error>>

Attempt to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more
§

fn consume(self: Pin<&mut BufWriter<W>>, amt: usize)

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more
§

impl<W> AsyncRead for BufWriter<W>where W: AsyncRead,

§

fn poll_read( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_>, buf: &mut [u8] ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into buf. Read more
§

fn poll_read_vectored( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_>, bufs: &mut [IoSliceMut<'_>] ) -> Poll<Result<usize, Error>>

Attempt to read from the AsyncRead into bufs using vectored IO operations. Read more
§

impl<W> AsyncSeek for BufWriter<W>where W: AsyncWrite + AsyncSeek,

§

fn poll_seek( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_>, pos: SeekFrom ) -> Poll<Result<u64, Error>>

Seek to the offset, in bytes, in the underlying writer.

Seeking always writes out the internal buffer before seeking.

§

impl<W> AsyncWrite for BufWriter<W>where W: AsyncWrite,

§

fn poll_write( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize, Error>>

Attempt to write bytes from buf into the object. Read more
§

fn poll_write_vectored( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>

Attempt to write bytes from bufs into the object using vectored IO operations. Read more
§

fn poll_flush( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_> ) -> Poll<Result<(), Error>>

Attempt to flush the object, ensuring that any buffered data reach their destination. Read more
§

fn poll_close( self: Pin<&mut BufWriter<W>>, cx: &mut Context<'_> ) -> Poll<Result<(), Error>>

Attempt to close the object. Read more
§

impl<W> Debug for BufWriter<W>where W: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<'__pin, W> Unpin for BufWriter<W>where __Origin<'__pin, W>: Unpin,

Auto Trait Implementations§

§

impl<W> RefUnwindSafe for BufWriter<W>where W: RefUnwindSafe,

§

impl<W> Send for BufWriter<W>where W: Send,

§

impl<W> Sync for BufWriter<W>where W: Sync,

§

impl<W> UnwindSafe for BufWriter<W>where W: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Any for Twhere T: Any,

§

fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>

§

fn type_name(&self) -> &'static str

§

impl<T> AnySync for Twhere T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync + 'static>

§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
§

impl<R> AsyncBufReadExt for Rwhere R: AsyncBufRead + ?Sized,

§

fn fill_buf(&mut self) -> FillBuf<'_, Self> where Self: Unpin,

Creates a future which will wait for a non-empty buffer to be available from this I/O object or EOF to be reached. Read more
§

fn consume_unpin(&mut self, amt: usize)where Self: Unpin,

A convenience for calling AsyncBufRead::consume on Unpin IO types. Read more
§

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8, Global> ) -> ReadUntil<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes associated with this I/O object into buf until the delimiter byte or EOF is reached. This method is the async equivalent to BufRead::read_until. Read more
§

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes associated with this I/O object into buf until a newline (the 0xA byte) or EOF is reached, This method is the async equivalent to BufRead::read_line. Read more
§

fn lines(self) -> Lines<Self>where Self: Sized,

Returns a stream over the lines of this reader. This method is the async equivalent to BufRead::lines. Read more
§

impl<R> AsyncBufReadExt for Rwhere R: AsyncBufRead + ?Sized,

§

fn fill_buf(&mut self) -> FillBuf<'_, Self> where Self: Unpin,

Returns the contents of the internal buffer, filling it with more data if empty. Read more
§

fn consume(&mut self, amt: usize)where Self: Unpin,

Consumes amt buffered bytes. Read more
§

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8, Global> ) -> ReadUntilFuture<'a, Self> where Self: Unpin,

Reads all bytes and appends them into buf until the delimiter byte or EOF is found. Read more
§

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self> where Self: Unpin,

Reads all bytes and appends them into buf until a newline (the 0xA byte) or EOF is found. Read more
§

fn lines(self) -> Lines<Self>where Self: Unpin + Sized,

Returns a stream over the lines of this byte stream. Read more
§

fn split(self, byte: u8) -> Split<Self>where Self: Sized,

Returns a stream over the contents of this reader split on the specified byte. Read more
§

impl<R> AsyncReadExt for Rwhere R: AsyncRead + ?Sized,

§

fn chain<R>(self, next: R) -> Chain<Self, R>where Self: Sized, R: AsyncRead,

Creates an adaptor which will chain this stream with another. Read more
§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where Self: Unpin,

Tries to read some bytes directly into the given buf in asynchronous manner, returning a future type. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectored<'a, Self> where Self: Unpin,

Creates a future which will read from the AsyncRead into bufs using vectored IO operations. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where Self: Unpin,

Creates a future which will read exactly enough bytes to fill buf, returning an error if end of file (EOF) is hit sooner. Read more
§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8, Global> ) -> ReadToEnd<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToString<'a, Self> where Self: Unpin,

Creates a future which will read all the bytes from this AsyncRead. Read more
§

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)where Self: AsyncWrite + Sized,

Helper method for splitting this read/write object into two halves. Read more
§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an AsyncRead adapter which will read at most limit bytes from the underlying reader. Read more
§

impl<R> AsyncReadExt for Rwhere R: AsyncRead + ?Sized,

§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self> where Self: Unpin,

Reads some bytes from the byte stream. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectoredFuture<'a, Self> where Self: Unpin,

Like [read()][AsyncReadExt::read()], except it reads into a slice of buffers. Read more
§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8, Global> ) -> ReadToEndFuture<'a, Self> where Self: Unpin,

Reads the entire contents and appends them to a Vec. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToStringFuture<'a, Self> where Self: Unpin,

Reads the entire contents and appends them to a String. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self> where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
§

fn bytes(self) -> Bytes<Self>where Self: Sized,

Converts this AsyncRead into a Stream of bytes. Read more
§

fn chain<R>(self, next: R) -> Chain<Self, R>where R: AsyncRead, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
§

fn boxed_reader<'a>(self) -> Pin<Box<dyn AsyncRead + Send + 'a, Global>>where Self: Sized + Send + 'a,

Boxes the reader and changes its type to dyn AsyncRead + Send + 'a. Read more
§

impl<S> AsyncSeekExt for Swhere S: AsyncSeek + ?Sized,

§

fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self> where Self: Unpin,

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more
§

fn stream_position(&mut self) -> Seek<'_, Self> where Self: Unpin,

Creates a future which will return the current seek position from the start of the stream. Read more
§

impl<S> AsyncSeekExt for Swhere S: AsyncSeek + ?Sized,

§

fn seek(&mut self, pos: SeekFrom) -> SeekFuture<'_, Self> where Self: Unpin,

Seeks to a new position in a byte stream. Read more
§

impl<W> AsyncWriteExt for Wwhere W: AsyncWrite + ?Sized,

§

fn flush(&mut self) -> Flush<'_, Self> where Self: Unpin,

Creates a future which will entirely flush this AsyncWrite. Read more
§

fn close(&mut self) -> Close<'_, Self> where Self: Unpin,

Creates a future which will entirely close this AsyncWrite.
§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self> where Self: Unpin,

Creates a future which will write bytes from buf into the object. Read more
§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectored<'a, Self> where Self: Unpin,

Creates a future which will write bytes from bufs into the object using vectored IO operations. Read more
§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self> where Self: Unpin,

Write data into this object. Read more
§

fn into_sink<Item>(self) -> IntoSink<Self, Item>where Item: AsRef<[u8]>, Self: Sized,

Allow using an AsyncWrite as a Sink<Item: AsRef<[u8]>>. Read more
§

impl<W> AsyncWriteExt for Wwhere W: AsyncWrite + ?Sized,

§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self> where Self: Unpin,

Writes some bytes into the byte stream. Read more
§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectoredFuture<'a, Self> where Self: Unpin,

Like [write()][AsyncWriteExt::write()], except that it writes a slice of buffers. Read more
§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self> where Self: Unpin,

Writes an entire buffer into the byte stream. Read more
§

fn flush(&mut self) -> FlushFuture<'_, Self> where Self: Unpin,

Flushes the stream to ensure that all buffered contents reach their destination. Read more
§

fn close(&mut self) -> CloseFuture<'_, Self> where Self: Unpin,

Closes the writer. Read more
§

fn boxed_writer<'a>(self) -> Pin<Box<dyn AsyncWrite + Send + 'a, Global>>where Self: Sized + Send + 'a,

Boxes the writer and changes its type to dyn AsyncWrite + Send + 'a. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> BufReadExt for Twhere T: AsyncBufRead + ?Sized,

§

fn read_until<'a>( &'a mut self, byte: u8, buf: &'a mut Vec<u8, Global> ) -> ReadUntilFuture<'a, Self>where Self: Unpin,

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
§

fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLineFuture<'a, Self>where Self: Unpin,

Reads all bytes and appends them into buf until a newline (the 0xA byte) is reached. Read more
§

fn lines(self) -> Lines<Self>where Self: Unpin + Sized,

Returns a stream over the lines of this byte stream. Read more
§

fn split(self, byte: u8) -> Split<Self>where Self: Sized,

Returns a stream over the contents of this reader split on the byte byte. Read more
§

impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
§

impl<T> ReadExt for Twhere T: AsyncRead + ?Sized,

§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadFuture<'a, Self>where Self: Unpin,

Reads some bytes from the byte stream. Read more
§

fn read_vectored<'a>( &'a mut self, bufs: &'a mut [IoSliceMut<'a>] ) -> ReadVectoredFuture<'a, Self>where Self: Unpin,

Like read, except that it reads into a slice of buffers. Read more
§

fn read_to_end<'a>( &'a mut self, buf: &'a mut Vec<u8, Global> ) -> ReadToEndFuture<'a, Self>where Self: Unpin,

Reads all bytes from the byte stream. Read more
§

fn read_to_string<'a>( &'a mut self, buf: &'a mut String ) -> ReadToStringFuture<'a, Self>where Self: Unpin,

Reads all bytes from the byte stream and appends them into a string. Read more
§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExactFuture<'a, Self>where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an adaptor which will read at most limit bytes from it. Read more
§

fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
§

fn bytes(self) -> Bytes<Self>where Self: Sized,

Transforms this Read instance to a Stream over its bytes. Read more
§

fn chain<R>(self, next: R) -> Chain<Self, R>where R: AsyncRead, Self: Sized,

Creates an adaptor which will chain this stream with another. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<T> SeekExt for Twhere T: AsyncSeek + ?Sized,

§

fn seek(&mut self, pos: SeekFrom) -> SeekFuture<'_, Self>where Self: Unpin,

Seeks to a new position in a byte stream. Read more
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcastable for Twhere T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

upcast boxed dyn
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> WriteExt for Twhere T: AsyncWrite + ?Sized,

§

fn write<'a>(&'a mut self, buf: &'a [u8]) -> WriteFuture<'a, Self>where Self: Unpin,

Writes some bytes into the byte stream. Read more
§

fn flush(&mut self) -> FlushFuture<'_, Self>where Self: Unpin,

Flushes the stream to ensure that all buffered contents reach their destination. Read more
§

fn write_vectored<'a>( &'a mut self, bufs: &'a [IoSlice<'a>] ) -> WriteVectoredFuture<'a, Self>where Self: Unpin,

Like write, except that it writes from a slice of buffers. Read more
§

fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAllFuture<'a, Self>where Self: Unpin,

Writes an entire buffer into the byte stream. Read more
§

fn write_fmt<'a>(&'a mut self, fmt: Arguments<'_>) -> WriteFmtFuture<'a, Self>where Self: Unpin,

Writes a formatted string into this writer, returning any error encountered. Read more