pub struct Bufferfish { /* private fields */ }
Expand description
A wrapper around a Cursor<Vec<u8>>
that provides a simple API for reading
and writing bytes. This is meant to be used with its companion library in
TypeScript to provide consistent encoding and decoding interop.
Implementations§
Source§impl Bufferfish
impl Bufferfish
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new Bufferfish
with a max capacity (in bytes).
A value of 0 will allow the buffer to grow indefinitely.
Sourcepub fn as_bytes(&self) -> Arc<[u8]>
pub fn as_bytes(&self) -> Arc<[u8]>
Returns an Arc<[u8]>
of the internal byte buffer for cheaply cloning
and sharing the buffer.
Sourcepub fn set_max_capacity(&mut self, capacity: usize)
pub fn set_max_capacity(&mut self, capacity: usize)
Set the max capacity (in bytes) for the internal buffer. A value of 0 will allow the buffer to grow indefinitely.
Sourcepub fn extend<T: Into<Bufferfish>>(&mut self, other: T)
pub fn extend<T: Into<Bufferfish>>(&mut self, other: T)
Adds a Bufferfish
or Vec<u8>
to the end of the buffer.
See try_extends
for a version that returns a Result
.
§Panics
Panics if the buffer is at max capacity.
Sourcepub fn try_extend<T: Into<Bufferfish>>(
&mut self,
other: T,
) -> Result<(), BufferfishError>
pub fn try_extend<T: Into<Bufferfish>>( &mut self, other: T, ) -> Result<(), BufferfishError>
Adds a Bufferfish
or Vec<u8>
to the end of the buffer.
Returns a Result
if the buffer is at max capacity.
Sourcepub fn peek(&mut self) -> Result<u8, BufferfishError>
pub fn peek(&mut self) -> Result<u8, BufferfishError>
Returns the next byte in the buffer without advancing the cursor.
Returns a Result
if the cursor is at the end of the buffer.
Sourcepub fn peek_n(&mut self, n: usize) -> Result<Vec<u8>, BufferfishError>
pub fn peek_n(&mut self, n: usize) -> Result<Vec<u8>, BufferfishError>
Returns the next n-bytes in the buffer without advancing the cursor. Returns a Result if the cursor is at the end of the buffer.
Sourcepub fn write_u8(&mut self, value: u8) -> Result<(), BufferfishError>
pub fn write_u8(&mut self, value: u8) -> Result<(), BufferfishError>
Writes a u8 to the buffer as one byte.
Sourcepub fn write_u16(&mut self, value: u16) -> Result<(), BufferfishError>
pub fn write_u16(&mut self, value: u16) -> Result<(), BufferfishError>
Writes a u16 to the buffer as two bytes.
Sourcepub fn write_u32(&mut self, value: u32) -> Result<(), BufferfishError>
pub fn write_u32(&mut self, value: u32) -> Result<(), BufferfishError>
Writes a u32 to the buffer as four bytes.
Sourcepub fn write_i8(&mut self, value: i8) -> Result<(), BufferfishError>
pub fn write_i8(&mut self, value: i8) -> Result<(), BufferfishError>
Writes an i8 to the buffer as one byte.
Sourcepub fn write_i16(&mut self, value: i16) -> Result<(), BufferfishError>
pub fn write_i16(&mut self, value: i16) -> Result<(), BufferfishError>
Writes an i16 to the buffer as two bytes.
Sourcepub fn write_i32(&mut self, value: i32) -> Result<(), BufferfishError>
pub fn write_i32(&mut self, value: i32) -> Result<(), BufferfishError>
Writes an i32 to the buffer as four bytes.
Sourcepub fn write_bool(&mut self, value: bool) -> Result<(), BufferfishError>
pub fn write_bool(&mut self, value: bool) -> Result<(), BufferfishError>
Writes a bool to the buffer as one byte.
Sourcepub fn write_packed_bools(
&mut self,
values: &[bool],
) -> Result<(), BufferfishError>
pub fn write_packed_bools( &mut self, values: &[bool], ) -> Result<(), BufferfishError>
Writes a packed array of booleans to the buffer as a single byte. Can pack up to 8 booleans into a single byte.
Sourcepub fn write_string(&mut self, value: &str) -> Result<(), BufferfishError>
pub fn write_string(&mut self, value: &str) -> Result<(), BufferfishError>
Writes a variable length string to the buffer. It will be prefixed with its length in bytes as a u16 (two bytes).
Sourcepub fn write_array<T: Encodable>(
&mut self,
vec: &[T],
) -> Result<(), BufferfishError>
pub fn write_array<T: Encodable>( &mut self, vec: &[T], ) -> Result<(), BufferfishError>
Writes an array to the buffer, where the items implement the Encodable trait. The array will be prefixed with its length as a u16 (two bytes).
Sourcepub fn write_raw_bytes(&mut self, bytes: &[u8]) -> Result<(), BufferfishError>
pub fn write_raw_bytes(&mut self, bytes: &[u8]) -> Result<(), BufferfishError>
Writes an array of raw bytes to the buffer. Useful for encoding distinct structs into byte arrays and appending them to a buffer later.
Sourcepub fn read_u8(&mut self) -> Result<u8, BufferfishError>
pub fn read_u8(&mut self) -> Result<u8, BufferfishError>
Reads a u8 from the buffer.
Sourcepub fn read_u16(&mut self) -> Result<u16, BufferfishError>
pub fn read_u16(&mut self) -> Result<u16, BufferfishError>
Reads a u16 from the buffer.
Sourcepub fn read_u32(&mut self) -> Result<u32, BufferfishError>
pub fn read_u32(&mut self) -> Result<u32, BufferfishError>
Reads a u32 from the buffer.
Sourcepub fn read_i8(&mut self) -> Result<i8, BufferfishError>
pub fn read_i8(&mut self) -> Result<i8, BufferfishError>
Reads an i8 from the buffer.
Sourcepub fn read_i16(&mut self) -> Result<i16, BufferfishError>
pub fn read_i16(&mut self) -> Result<i16, BufferfishError>
Reads an i16 from the buffer.
Sourcepub fn read_i32(&mut self) -> Result<i32, BufferfishError>
pub fn read_i32(&mut self) -> Result<i32, BufferfishError>
Reads an i32 from the buffer.
Sourcepub fn read_bool(&mut self) -> Result<bool, BufferfishError>
pub fn read_bool(&mut self) -> Result<bool, BufferfishError>
Reads a bool from the buffer.
Sourcepub fn read_packed_bools(
&mut self,
count: u8,
) -> Result<Vec<bool>, BufferfishError>
pub fn read_packed_bools( &mut self, count: u8, ) -> Result<Vec<bool>, BufferfishError>
Attempts to read a packed array of booleans from the buffer. You must specify the number of booleans to read.
Sourcepub fn read_string(&mut self) -> Result<String, BufferfishError>
pub fn read_string(&mut self) -> Result<String, BufferfishError>
Reads a variable length string from the buffer.
Sourcepub fn read_array<T: Decodable>(&mut self) -> Result<Vec<T>, BufferfishError>
pub fn read_array<T: Decodable>(&mut self) -> Result<Vec<T>, BufferfishError>
Reads an array from the buffer, where the items implement the Decodable trait.
Trait Implementations§
Source§impl AsMut<[u8]> for Bufferfish
impl AsMut<[u8]> for Bufferfish
Source§impl AsRef<[u8]> for Bufferfish
impl AsRef<[u8]> for Bufferfish
Source§impl Debug for Bufferfish
impl Debug for Bufferfish
Source§impl Default for Bufferfish
impl Default for Bufferfish
Source§impl Display for Bufferfish
impl Display for Bufferfish
Source§impl From<&[u8]> for Bufferfish
impl From<&[u8]> for Bufferfish
Source§impl From<Bufferfish> for Bytes
impl From<Bufferfish> for Bytes
Source§fn from(buffer: Bufferfish) -> Self
fn from(buffer: Bufferfish) -> Self
Source§impl From<Bufferfish> for Vec<u8>
impl From<Bufferfish> for Vec<u8>
Source§fn from(buffer: Bufferfish) -> Self
fn from(buffer: Bufferfish) -> Self
Source§impl From<Bytes> for Bufferfish
impl From<Bytes> for Bufferfish
Source§impl PartialEq for Bufferfish
impl PartialEq for Bufferfish
Source§impl Seek for Bufferfish
impl Seek for Bufferfish
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)Source§impl Write for Bufferfish
impl Write for Bufferfish
Source§fn write(&mut self, bf: &[u8]) -> Result<usize>
fn write(&mut self, bf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)