Struct Bufferfish

Source
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

Source

pub fn new() -> Self

Creates a new Bufferfish with a default max capacity (1024 bytes).

Source

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.

Source

pub fn len(&self) -> usize

Returns the current length (bytes) of the buffer.

Source

pub fn is_empty(&self) -> bool

Returns true if the buffer is empty.

Source

pub fn to_vec(&self) -> Vec<u8>

Returns a Vec<u8> of the internal byte buffer.

Source

pub fn as_bytes(&self) -> Arc<[u8]>

Returns an Arc<[u8]> of the internal byte buffer for cheaply cloning and sharing the buffer.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn write_u8(&mut self, value: u8) -> Result<(), BufferfishError>

Writes a u8 to the buffer as one byte.

Source

pub fn write_u16(&mut self, value: u16) -> Result<(), BufferfishError>

Writes a u16 to the buffer as two bytes.

Source

pub fn write_u32(&mut self, value: u32) -> Result<(), BufferfishError>

Writes a u32 to the buffer as four bytes.

Source

pub fn write_i8(&mut self, value: i8) -> Result<(), BufferfishError>

Writes an i8 to the buffer as one byte.

Source

pub fn write_i16(&mut self, value: i16) -> Result<(), BufferfishError>

Writes an i16 to the buffer as two bytes.

Source

pub fn write_i32(&mut self, value: i32) -> Result<(), BufferfishError>

Writes an i32 to the buffer as four bytes.

Source

pub fn write_bool(&mut self, value: bool) -> Result<(), BufferfishError>

Writes a bool to the buffer as one byte.

Source

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.

Source

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

Source

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

Source

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.

Source

pub fn read_u8(&mut self) -> Result<u8, BufferfishError>

Reads a u8 from the buffer.

Source

pub fn read_u16(&mut self) -> Result<u16, BufferfishError>

Reads a u16 from the buffer.

Source

pub fn read_u32(&mut self) -> Result<u32, BufferfishError>

Reads a u32 from the buffer.

Source

pub fn read_i8(&mut self) -> Result<i8, BufferfishError>

Reads an i8 from the buffer.

Source

pub fn read_i16(&mut self) -> Result<i16, BufferfishError>

Reads an i16 from the buffer.

Source

pub fn read_i32(&mut self) -> Result<i32, BufferfishError>

Reads an i32 from the buffer.

Source

pub fn read_bool(&mut self) -> Result<bool, BufferfishError>

Reads a bool from the buffer.

Source

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.

Source

pub fn read_string(&mut self) -> Result<String, BufferfishError>

Reads a variable length string from the buffer.

Source

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

Source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<[u8]> for Bufferfish

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for Bufferfish

Source§

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

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

impl Default for Bufferfish

Source§

fn default() -> Bufferfish

Returns the “default value” for a type. Read more
Source§

impl Display for Bufferfish

Source§

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

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

impl From<&[u8]> for Bufferfish

Source§

fn from(slice: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<Bufferfish> for Bytes

Source§

fn from(buffer: Bufferfish) -> Self

Converts to this type from the input type.
Source§

impl From<Bufferfish> for Vec<u8>

Source§

fn from(buffer: Bufferfish) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for Bufferfish

Source§

fn from(bytes: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Bufferfish

Source§

fn from(vec: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Bufferfish

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Seek for Bufferfish

Source§

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

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · 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>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

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

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more
Source§

impl Write for Bufferfish

Source§

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

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

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

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · 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 is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

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

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

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

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

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

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

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

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

Auto Trait Implementations§

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

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.