#byte-buffer #byte #buffer #queue #rotating

rotbuf

RotBuf is a Queue implementation wrapped around the Bytes crates’ BytesMut data structure

3 releases

new 0.0.3 Apr 18, 2024
0.0.2 Apr 18, 2024
0.0.1 Apr 18, 2024

#869 in Encoding

Download history 263/week @ 2024-04-15

263 downloads per month

Apache-2.0

18KB
307 lines

Rotating Buffer (RotBuf)

A dynamically sized Queue implementation using the Bytes crate's BufferMut. The RotatingBuffer allows user to store sequenced bytes in a bytes buffer without needing to move data down the buffer.

To get started, you can easily create a RotatingBuffer knowing only the maximum size. Resizing is not currently implemented but may be implemented in the future, so choose your size wisely.

use rotbuf::RotatingBuffer;

fn create_rotating_buffer() -> RotatingBuffer {
    RotatingBuffer::new(10)
}

Enqueueing and Dequeueing

The simplest way to use the RotatingBuffer is to treat it like a queue, enqueing and dequeing one byte at a time.

enqueue is very easy, just provide it with any u8 (best representation for a singular byte).

dequeue returns an Option, containing either the front most byte in Some, or, if empty, None.

rb = RotatingBuffer::new(10);
rb.enqueue(50)?
match rb.dequeue() {
    Some(value) => println!("Look, we dequeued something: {}", value),
    None => println!("Womp womp, we were empty."),
}

enqueue in most cases will return an empty [Ok] to signify it was successful. If it reaches the capacity of the RotatingBuffer, it will return an Err with a RotatingBufferAtCapacity.

match rb.enqueue(50) {
    Ok(()) => println!("The value was enqueued"),
    Err(err) => println!("Oh no we must be at capacity: {}", err)
}

The RotatingBufferAtCapacity is an Error, but you can reclaim the value you provided by using the reclaim fn

match rb.enqueue(50) {
    Ok(()) => println!("The value was enqueued"),
    Err(err) => println!("Oh no we couldn't enqueue this byte: {}", err.reclaim())
}

Dependencies

~170KB