Struct eos_rs::api::io::BufWriter

source ·
pub struct BufWriter<W>where
    W: Write,{ /* private fields */ }
Expand description

Wraps a writer and buffers its output.

It can be excessively inefficient to work directly with something that implements Write. 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 Write instance. 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>.

It is critical to call flush before BufWriter<W> is dropped. Though dropping will attempt to flush the contents of the buffer, any errors that happen in the process of dropping will be ignored. Calling flush ensures that the buffer is empty and thus dropping will not even attempt file operations.

Implementations§

source§

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

source

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

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

Examples
use acid_io::{BufWriter, Cursor};

let buf: Vec<u8> = Vec::new();
let mut writer = BufWriter::new(Cursor::new(buf));
source

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

Creates a new BufWriter<W> with the specified buffer capacity.

Examples

Creating a writer with a buffer of a hundred bytes.

use acid_io::{BufWriter, Cursor};

let buf: Vec<u8> = Vec::new();
let mut writer = BufWriter::with_capacity(100, Cursor::new(buf));
source

pub fn write_to_buf(&mut self, buf: &[u8]) -> usize

Buffer some data without flushing it, regardless of the size of the data. Writes as much as possible without exceeding capacity. Returns the number of bytes written.

source

pub fn get_ref(&self) -> &W

Gets a reference to the underlying writer.

Examples
use acid_io::BufWriter;

let mut buffer = BufWriter::new(Vec::new());

// we can use reference just like buffer
let reference: &Vec<u8> = buffer.get_ref();
source

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

Gets a mutable reference to the underlying writer.

It is inadvisable to directly write to the underlying writer.

Examples
use acid_io::BufWriter;

let mut buffer = BufWriter::new(Vec::new());

// we can use reference just like buffer
let reference: &mut Vec<u8> = buffer.get_mut();
source

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

Returns a reference to the internally buffered data.

Examples
use acid_io::BufWriter;

let buf_writer = BufWriter::new(Vec::new());

// See how many bytes are currently buffered
let bytes_buffered = buf_writer.buffer().len();
source

pub fn capacity(&self) -> usize

Returns the number of bytes the internal buffer can hold without flushing.

Examples
use acid_io::BufWriter;

let buf_writer = BufWriter::new(Vec::new());

// Check the capacity of the inner buffer
let capacity = buf_writer.capacity();
// Calculate how many bytes can be written without flushing
let without_flush = capacity - buf_writer.buffer().len();
source

pub fn into_inner(self) -> Result<W, IntoInnerError<BufWriter<W>>>

Unwraps this BufWriter<W>, returning the underlying writer.

The buffer is written out before returning the writer.

Errors

An [Err] will be returned if an error occurs while flushing the buffer.

Examples
use acid_io::BufWriter;

let mut buffer = BufWriter::new(Vec::new());

let inner: Vec<u8> = buffer.into_inner().unwrap();
source

pub fn into_parts(self) -> (W, Result<Vec<u8, Global>, WriterPanicked>)

Disassembles this BufWriter<W>, returning the underlying writer, and any buffered but unwritten data.

If the underlying writer panicked, it is not known what portion of the data was written. In this case, we return WriterPanicked for the buffered data (from which the buffer contents can still be recovered).

into_parts makes no attempt to flush data and cannot fail.

Examples
use acid_io::{BufWriter, Write};

let mut buffer = [0u8; 10];
let mut stream = BufWriter::new(buffer.as_mut());
write!(stream, "too much data").unwrap();
stream.flush().expect_err("it doesn't fit");
let (recovered_writer, buffered_data) = stream.into_parts();
assert_eq!(recovered_writer.len(), 0);
assert_eq!(&buffered_data.unwrap(), b"ata");

Trait Implementations§

source§

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

source§

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

Formats the value using the given formatter. Read more
source§

impl<W> Drop for BufWriter<W>where W: Write,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<W> Seek for BufWriter<W>where W: Write + Seek,

source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>

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

Seeking always writes out the internal buffer before seeking.

source§

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

Rewind to the beginning of a stream. Read more
source§

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

Returns the length of this stream (in bytes). Read more
source§

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

Returns the current seek position from the start of the stream. Read more
source§

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

source§

fn write(&mut self, buf: &[u8]) -> Result<usize, Error>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

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

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

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
source§

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

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

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

Creates a “by reference” adapter for this instance of Write. Read more

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

§

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

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dstwhere T: Cast<Dst>,

Casts the value.
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dstwhere Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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<Src, Dst> LosslessTryInto<Dst> for Srcwhere Dst: LosslessTryFrom<Src>,

source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
source§

impl<Src, Dst> LossyInto<Dst> for Srcwhere Dst: LossyFrom<Src>,

source§

fn lossy_into(self) -> Dst

Performs the conversion.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

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

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dstwhere T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dstwhere T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.