Struct buf_redux::Buffer

source ·
pub struct Buffer { /* private fields */ }
Expand description

A deque-like datastructure for managing bytes.

Supports interacting via I/O traits like Read and Write, and direct access.

Implementations

Create a new buffer with a default capacity.

Create a new buffer with at least the given capacity.

If the global allocator returns extra capacity, Buffer will use all of it.

Allocate a buffer with a default capacity that never needs to move data to make room (consuming from the head simultaneously makes more room at the tail).

The default capacity varies based on the target platform:

  • Unix-derivative platforms; Linux, OS X, BSDs, etc: 8KiB (the default buffer size for std::io buffered types)
  • Windows: 64KiB because of legacy reasons, of course (see below)

Only available on platforms with virtual memory support and with the slice-deque feature enabled. The current platforms that are supported/tested are listed in the README for the slice-deque crate.

Allocate a buffer with at least the given capacity that never needs to move data to make room (consuming from the head simultaneously makes more room at the tail).

The capacity will be rounded up to the minimum size for the current target:

  • Unix-derivative platforms; Linux, OS X, BSDs, etc: the next multiple of the page size (typically 4KiB but can vary based on system configuration)
  • Windows: the next muliple of 64KiB; see this Microsoft dev blog post for why it’s 64KiB and not the page size (TL;DR: Alpha AXP needs it and it’s applied on all targets for consistency/portability)

Only available on platforms with virtual memory support and with the slice-deque feature enabled. The current platforms that are supported/tested are listed in the README for the slice-deque crate.

Return true if this is a ringbuffer.

Return the number of bytes currently in this buffer.

Equivalent to self.buf().len().

Return the number of bytes that can be read into this buffer before it needs to grow or the data in the buffer needs to be moved.

This may not constitute all free space in the buffer if bytes have been consumed from the head. Use free_space() to determine the total free space in the buffer.

Returns the total amount of free space in the buffer, including bytes already consumed from the head.

This will be greater than or equal to usable_space(). On supported platforms with the slice-deque feature enabled, it should be equal.

Return the total capacity of this buffer.

Returns true if there are no bytes in the buffer, false otherwise.

Move bytes down in the buffer to maximize usable space.

This is a no-op on supported platforms with the slice-deque feature enabled.

Ensure space for at least additional more bytes in the buffer.

This is a no-op if usable_space() >= additional. Note that this will reallocate even if there is enough free space at the head of the buffer for additional bytes, because that free space is not at the tail where it can be read into. If you prefer copying data down in the buffer before attempting to reallocate you may wish to call .make_room() first.

Panics

If self.capacity() + additional overflows.

Get an immutable slice of the available bytes in this buffer.

Call .consume() to remove bytes from the beginning of this slice.

Get a mutable slice representing the available bytes in this buffer.

Call .consume() to remove bytes from the beginning of this slice.

Read from rdr, returning the number of bytes read or any errors.

If there is no more room at the head of the buffer, this will return Ok(0).

Uses Read::initializer() to initialize the buffer if the nightly feature is enabled, otherwise the buffer is zeroed if it has never been written.

Panics

If the returned count from rdr.read() overflows the tail cursor of this buffer.

Copy from src to the tail of this buffer. Returns the number of bytes copied.

This will not grow the buffer if src is larger than self.usable_space(); instead, it will fill the usable space and return the number of bytes copied. If there is no usable space, this returns 0.

Write bytes from this buffer to wrt. Returns the number of bytes written or any errors.

If the buffer is empty, returns Ok(0).

Panics

If the count returned by wrt.write() would cause the head cursor to overflow or pass the tail cursor if added to it.

Write, at most, the given number of bytes from this buffer to wrt, continuing to write and ignoring interrupts until the number is reached or the buffer is empty.

Panics

If the count returned by wrt.write() would cause the head cursor to overflow or pass the tail cursor if added to it.

Write all bytes in this buffer to wrt, ignoring interrupts. Continues writing until the buffer is empty or an error is returned.

Panics

If self.write_to(wrt) panics.

Copy bytes to out from this buffer, returning the number of bytes written.

Push bytes to the end of the buffer, growing it if necessary.

If you prefer moving bytes down in the buffer to reallocating, you may wish to call .make_room() first.

Consume amt bytes from the head of this buffer.

Empty this buffer by consuming all bytes.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

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