Skip to main content

BufWriter

Struct BufWriter 

pub struct BufWriter<W> { /* private fields */ }
Available on crate feature io only.
Expand description

Wraps a writer and buffers its output.

It can be excessively inefficient to work directly with something that implements AsyncWrite. A BufWriter<W> keeps an in-memory buffer of data and writes it to an underlying writer in large, infrequent batches. BufWriter<W> 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>.

If the underlying writer also implements AsyncRead or AsyncBufRead, BufWriter<W> forwards read operations directly to the inner reader without flushing the write buffer.

Dropping BufWriter<W> also discards any bytes left in the buffer, so it is critical to call flush before BufWriter<W> is dropped. Calling flush ensures that the buffer is empty and thus no data is lost.

Implementations§

§

impl<W> BufWriter<W>

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

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

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

Creates a new BufWriter with the specified buffer capacity.

Trait Implementations§

§

impl<W> AsyncBufRead for BufWriter<W>

§

async fn fill_buf(&mut self) -> Result<&[u8], Error>

Try fill the internal buffer with data
§

fn consume(&mut self, amount: usize)

Mark how much data is read
§

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

§

async fn read<B>(&mut self, buf: B) -> BufResult<usize, B>
where B: IoBufMut,

Read some bytes from this source into the IoBufMut buffer and return a BufResult, consisting of the buffer and a usize indicating how many bytes were read. Read more
§

async fn read_vectored<V>(&mut self, buf: V) -> BufResult<usize, V>

Like read, except that it reads into a type implements IoVectoredBufMut. Read more
§

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

§

async fn write<T>(&mut self, buf: T) -> BufResult<usize, T>
where T: IoBuf,

Write some bytes from the buffer into this source and return a BufResult, consisting of the buffer and a usize indicating how many bytes were written.
§

async fn write_vectored<T>(&mut self, buf: T) -> BufResult<usize, T>
where T: IoVectoredBuf,

Like write, except that it write bytes from a buffer implements IoVectoredBuf into the source. Read more
§

async fn flush(&mut self) -> Result<(), Error>

Attempts to flush the object, ensuring that any buffered data reach their destination.
§

async fn shutdown(&mut self) -> Result<(), Error>

Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down.
§

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<W> IntoInner for BufWriter<W>

§

type Inner = W

The inner type.
§

fn into_inner(self) -> <BufWriter<W> as IntoInner>::Inner

Get the inner buffer.

Auto Trait Implementations§

§

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

§

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> Unpin for BufWriter<W>
where W: Unpin,

§

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

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<A> AsyncReadExt for A
where A: AsyncRead + ?Sized,

§

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

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

async fn append<T>(&mut self, buf: T) -> BufResult<usize, T>
where T: IoBufMut,

Same as AsyncRead::read, but it appends data to the end of the buffer; in other words, it read to the beginning of the uninitialized area.
§

async fn read_exact<T>(&mut self, buf: T) -> BufResult<(), T>
where T: IoBufMut,

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

async fn read_to_string(&mut self, buf: String) -> BufResult<usize, String>

Read all bytes as String until underlying reader reaches EOF.
§

async fn read_to_end<A>( &mut self, buf: Vec<u8, A>, ) -> BufResult<usize, Vec<u8, A>>
where A: Allocator + 'static,

Read all bytes until underlying reader reaches EOF.
§

async fn read_vectored_exact<T>(&mut self, buf: T) -> BufResult<(), T>

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

fn framed<T, C, F>( self, codec: C, framer: F, ) -> Framed<Self::ReadHalf, Self::WriteHalf, C, F, T, T>
where Self: Sized + Splittable,

Create a framed::Framed reader/writer with the given codec and framer.
§

fn bytes( self, ) -> Framed<Self::ReadHalf, Self::WriteHalf, BytesCodec, NoopFramer, Bytes, Bytes>
where Self: Sized + Splittable,

Available on crate feature bytes only.
Convenience method to create a framed::BytesFramed reader/writter out of a splittable.
§

fn read_only(self) -> ReadOnly<Self>
where Self: Sized,

Create a Splittable that uses Self as ReadHalf and () as WriteHalf. Read more
§

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

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

async fn read_u8(&mut self) -> Result<u8, Error>

Read a big endian u8 from the underlying reader.
§

async fn read_u8_le(&mut self) -> Result<u8, Error>

Read a little endian u8 from the underlying reader.
§

async fn read_u16(&mut self) -> Result<u16, Error>

Read a big endian u16 from the underlying reader.
§

async fn read_u16_le(&mut self) -> Result<u16, Error>

Read a little endian u16 from the underlying reader.
§

async fn read_u32(&mut self) -> Result<u32, Error>

Read a big endian u32 from the underlying reader.
§

async fn read_u32_le(&mut self) -> Result<u32, Error>

Read a little endian u32 from the underlying reader.
§

async fn read_u64(&mut self) -> Result<u64, Error>

Read a big endian u64 from the underlying reader.
§

async fn read_u64_le(&mut self) -> Result<u64, Error>

Read a little endian u64 from the underlying reader.
§

async fn read_u128(&mut self) -> Result<u128, Error>

Read a big endian u128 from the underlying reader.
§

async fn read_u128_le(&mut self) -> Result<u128, Error>

Read a little endian u128 from the underlying reader.
§

async fn read_i8(&mut self) -> Result<i8, Error>

Read a big endian i8 from the underlying reader.
§

async fn read_i8_le(&mut self) -> Result<i8, Error>

Read a little endian i8 from the underlying reader.
§

async fn read_i16(&mut self) -> Result<i16, Error>

Read a big endian i16 from the underlying reader.
§

async fn read_i16_le(&mut self) -> Result<i16, Error>

Read a little endian i16 from the underlying reader.
§

async fn read_i32(&mut self) -> Result<i32, Error>

Read a big endian i32 from the underlying reader.
§

async fn read_i32_le(&mut self) -> Result<i32, Error>

Read a little endian i32 from the underlying reader.
§

async fn read_i64(&mut self) -> Result<i64, Error>

Read a big endian i64 from the underlying reader.
§

async fn read_i64_le(&mut self) -> Result<i64, Error>

Read a little endian i64 from the underlying reader.
§

async fn read_i128(&mut self) -> Result<i128, Error>

Read a big endian i128 from the underlying reader.
§

async fn read_i128_le(&mut self) -> Result<i128, Error>

Read a little endian i128 from the underlying reader.
§

async fn read_f32(&mut self) -> Result<f32, Error>

Read a big endian f32 from the underlying reader.
§

async fn read_f32_le(&mut self) -> Result<f32, Error>

Read a little endian f32 from the underlying reader.
§

async fn read_f64(&mut self) -> Result<f64, Error>

Read a big endian f64 from the underlying reader.
§

async fn read_f64_le(&mut self) -> Result<f64, Error>

Read a little endian f64 from the underlying reader.
§

impl<A> AsyncWriteExt for A
where A: AsyncWrite + ?Sized,

§

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

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

async fn write_all<T>(&mut self, buf: T) -> BufResult<(), T>
where T: IoBuf,

Write the entire contents of a buffer into this writer.
§

async fn write_vectored_all<T>(&mut self, buf: T) -> BufResult<(), T>
where T: IoVectoredBuf,

Write the entire contents of a buffer into this writer. Like AsyncWrite::write_vectored, except that it tries to write the entire contents of the buffer into this writer.
§

fn framed<T, C, F>( self, codec: C, framer: F, ) -> Framed<Self::ReadHalf, Self::WriteHalf, C, F, T, T>
where Self: Sized + Splittable,

Create a framed::Framed reader/writer with the given codec and framer.
§

fn bytes( self, ) -> Framed<Self::ReadHalf, Self::WriteHalf, BytesCodec, NoopFramer, Bytes, Bytes>
where Self: Sized + Splittable,

Available on crate feature bytes only.
Convenience method to create a framed::BytesFramed reader/writer out of a splittable.
§

fn write_only(self) -> WriteOnly<Self>
where Self: Sized,

Create a Splittable that uses Self as WriteHalf and () as ReadHalf. Read more
§

async fn write_u8(&mut self, num: u8) -> Result<(), Error>

Write a big endian u8 into the underlying writer.
§

async fn write_u8_le(&mut self, num: u8) -> Result<(), Error>

Write a little endian u8 into the underlying writer.
§

async fn write_u16(&mut self, num: u16) -> Result<(), Error>

Write a big endian u16 into the underlying writer.
§

async fn write_u16_le(&mut self, num: u16) -> Result<(), Error>

Write a little endian u16 into the underlying writer.
§

async fn write_u32(&mut self, num: u32) -> Result<(), Error>

Write a big endian u32 into the underlying writer.
§

async fn write_u32_le(&mut self, num: u32) -> Result<(), Error>

Write a little endian u32 into the underlying writer.
§

async fn write_u64(&mut self, num: u64) -> Result<(), Error>

Write a big endian u64 into the underlying writer.
§

async fn write_u64_le(&mut self, num: u64) -> Result<(), Error>

Write a little endian u64 into the underlying writer.
§

async fn write_u128(&mut self, num: u128) -> Result<(), Error>

Write a big endian u128 into the underlying writer.
§

async fn write_u128_le(&mut self, num: u128) -> Result<(), Error>

Write a little endian u128 into the underlying writer.
§

async fn write_i8(&mut self, num: i8) -> Result<(), Error>

Write a big endian i8 into the underlying writer.
§

async fn write_i8_le(&mut self, num: i8) -> Result<(), Error>

Write a little endian i8 into the underlying writer.
§

async fn write_i16(&mut self, num: i16) -> Result<(), Error>

Write a big endian i16 into the underlying writer.
§

async fn write_i16_le(&mut self, num: i16) -> Result<(), Error>

Write a little endian i16 into the underlying writer.
§

async fn write_i32(&mut self, num: i32) -> Result<(), Error>

Write a big endian i32 into the underlying writer.
§

async fn write_i32_le(&mut self, num: i32) -> Result<(), Error>

Write a little endian i32 into the underlying writer.
§

async fn write_i64(&mut self, num: i64) -> Result<(), Error>

Write a big endian i64 into the underlying writer.
§

async fn write_i64_le(&mut self, num: i64) -> Result<(), Error>

Write a little endian i64 into the underlying writer.
§

async fn write_i128(&mut self, num: i128) -> Result<(), Error>

Write a big endian i128 into the underlying writer.
§

async fn write_i128_le(&mut self, num: i128) -> Result<(), Error>

Write a little endian i128 into the underlying writer.
§

async fn write_f32(&mut self, num: f32) -> Result<(), Error>

Write a big endian f32 into the underlying writer.
§

async fn write_f32_le(&mut self, num: f32) -> Result<(), Error>

Write a little endian f32 into the underlying writer.
§

async fn write_f64(&mut self, num: f64) -> Result<(), Error>

Write a big endian f64 into the underlying writer.
§

async fn write_f64_le(&mut self, num: f64) -> Result<(), Error>

Write a little endian f64 into the underlying writer.
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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 T
where 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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> Ungil for T
where T: Send,