Crates.io | rotbuf |
lib.rs | rotbuf |
version | 0.0.3 |
source | src |
created_at | 2024-04-18 23:08:19.463049 |
updated_at | 2024-04-18 23:12:19.987366 |
description | RotBuf is a Queue implementation wrapped around the `Bytes` crates' BytesMut data structure. |
homepage | |
repository | https://github.com/StephenMal/RotBuf |
max_upload_size | |
id | 1213034 |
size | 29,889 |
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)
}
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())
}