pub struct ByteBuffer { /* private fields */ }
Expand description

A resizeable buffer to store data in.

Provides a resizeable buffer with an initial capacity of N bytes. All data written to the ByteBuffer has to implement the ByteBufferWrite trait or be a slice of type u8.

Data read from the ByteBuffer has to implement the ByteBufferRead trait.

Examples

use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 200;

// stores the value in the buffer and moves the cursor by 4
// due to u32 being 4 bytes in size
buffer.write(&value);

buffer.move_cursor(0);

// prints 200
println!("The stored value is: {}!", buffer.read::<u32>().unwrap());

Implementations

The maximum size the ByteBuffer will allocate.

The maximum size the ByteBuffer should be able to allocate is isize::MAX due to LLVM’s GEP Inbounds instruction.

Sources

Rustonomicon

LLVM documentation

The minimum capacity a ByteBuffer should have in theory.

Most, if not all, modern operating systems have at least a minimum heap allocation block of 8 bytes. So it makes little sense to have a ByteBuffer smaller than 8 bytes.

Constructs a new ByteBuffer of capacity MIN_SIZE

See with_capacity.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();

Constructs a new ByteBuffer with the given capacity.

Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::with_capacity(256).unwrap();

Resize the ByteBuffer to the given capacity.

Behaviour
  • If the current length of the ByteBuffer exceeds the given capacity, the length will be brought back to equal the given capacity.
  • If the current cursor position exceeds the length of the buffer, the cursor will be moved back to equal the length of the ByteBuffer.

To prevent undefined behaviour.

Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();

buffer.resize(16);

Expands the capacity of the ByteBuffer by the given amount.

Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();

buffer.expand(4);

Shrinks the capacity of the ByteBuffer by the given amount.

Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();

buffer.shrink(4);

Writes a slice of type u8 to the ByteBuffer without safety checks.

Safety

This method is unsafe because undefined behaviour can result if the caller does not ensure all of the following:

  • The length of the slice doesn’t exceed the capacity.
  • The cursor position + length of the slice does not exceed the capacity.
  • The cursor position is not out of bounds
Behaviour

The current cursor position will be increased by the length of the slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let values: [u8; 4] = [0, 1, 2, 3];

unsafe {
    buffer.write_slice_unchecked(&values);
}

Writes a slice of type u8 to the ByteBuffer.

Behaviour
  • If the result of the current cursor position + length of the slice exceeds the capacity of the buffer, the buffer will resize to the next power of two that fits the result.
  • The current cursor position will be increased by the length of the slice.
Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let values: [u8; 4] = [0, 1, 2, 3];

buffer.write_slice(&values);

Writes the given value to the ByteBuffer.

The value has to implement the ByteBufferWrite trait.

Errors & Behaviour

See write_slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);

Writes the given value to the ByteBuffer in little endian ordering.

The value has to implement the ByteBufferWrite trait.

Errors & Behaviour

See write_slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write_le(&value);

Writes the given value to the ByteBuffer in big endian ordering.

The value has to implement the ByteBufferWrite trait.

Errors & Behaviour

See write_slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write_be(&value);

Reads a slice of type u8 from the ByteBuffer of the given size without safety checks.

Safety

This method is unsafe because undefined behaviour can result if the caller does not ensure all of the following:

  • The size does not exceed the capacity of the buffer.
  • The result of cursor position + the given size does not exceed the length of the buffer.
  • The cursor position is not out of bounds
Behaviour

The current cursor position will be increased by the given size.

Examples
 use bytey_byte_buffer::byte_buffer::ByteBuffer;

 let mut buffer = ByteBuffer::new().unwrap();
 let value: u32 = 12345;

 buffer.write(&value);
 buffer.move_cursor(0);

 unsafe {
 }

Reads a slice of type u8 from the ByteBuffer of the given size.

Behaviour

The current cursor position will be increased by the given size.

Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);
buffer.move_cursor(0);

println!("{:?}", buffer.read_slice(4));

Reads a value of type T that implements the ByteBufferRead trait from the buffer.

Errors & Behaviour

See read_slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);
buffer.move_cursor(0);

println!("{}", buffer.read::<u32>().unwrap());
buffer.move_cursor(0);

let x: u32 = buffer.read().unwrap();

Reads a value of type T that implements the ByteBufferRead trait from the buffer in little endian ordering.

Errors & Behaviour

See read_slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write_le(&value);
buffer.move_cursor(0);

println!("{}", buffer.read_le::<u32>().unwrap());
buffer.move_cursor(0);

let x: u32 = buffer.read_le().unwrap();

Reads a value of type T that implements the ByteBufferRead trait from the buffer in big endian ordering.

Errors & Behaviour

See read_slice.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write_be(&value);
buffer.move_cursor(0);

println!("{}", buffer.read_be::<u32>().unwrap());
buffer.move_cursor(0);

let x: u32 = buffer.read_be().unwrap();

Moves the current cursor position without safety checks.

Safety

This method is unsafe because undefined behaviour can result if the caller does not ensure the given location does not exceed the buffer’s length.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);

unsafe {
    buffer.move_cursor_unchecked(2);
}

Moves the current cursor position.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);

buffer.move_cursor(2);

Resets length without resizing array.

Behaviour

buffer’s length will be set to length and buffer’s cursor will be set to length if greater than length.

Errors
Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);

let _ = buffer.truncate(0).unwrap();

Returns the length of the ByteBuffer.

The length of the buffer is the last index written to - 1.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);

println!("{}", buffer.length());

Returns the capacity of the ByteBuffer.

The capacity of the buffer is the size of the heap allocation used to store data.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();

println!("{}", buffer.capacity());

Returns the current cursor position of the ByteBuffer.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let value: u32 = 12345;

buffer.write(&value);

println!("{}", buffer.cursor());

Returns the layout of the ByteBuffer.

Examples
use bytey_byte_buffer::byte_buffer::ByteBuffer;

let mut buffer = ByteBuffer::new().unwrap();
let layout = buffer.layout();

Returns a const pointer to the allocation.

Safety

This method is unsafe due to the unsafe nature of pointers itself.

This method can result in undefined behaviour if the buffer is resized and the underlying heap allocator moves the pointer

Returns a mutable pointer to the allocation.

Safety

This method is unsafe due to the unsafe nature of pointers itself.

This method can result in undefined behaviour if the buffer is resized and the underlying heap allocator moves the pointer

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. 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 resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

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.