Crates.io | triskell |
lib.rs | triskell |
version | 0.1.2 |
source | src |
created_at | 2023-08-05 10:52:36.621497 |
updated_at | 2023-08-15 09:09:17.918673 |
description | A tri-partite ring buffer |
homepage | |
repository | https://github.com/fveilly/triskell |
max_upload_size | |
id | 936310 |
size | 53,932 |
A tri-partite ring buffer is similar to a circular buffer, but data is inserted in three revolving regions of the buffer space. This allows reads to return contiguous blocks of memory, even if they span a region that would normally include a wrap-around in a circular buffer. It's especially useful for APIs requiring blocks of contiguous memory, eliminating the need to copy data into an interim buffer before use.
It is heavily inspired by the article of Simon Cooke's Bip-Buffer.
use triskell::TRBuffer;
// Creates a TRBuffer of u8 and allocates 8 elements
let mut buffer: TRBuffer<u8> = TRBuffer::with_capacity(8);
{
// Reserves 4 slots at the back for insert
let reserved = buffer.reserve_back(4).unwrap();
reserved[0] = 2;
reserved[1] = 12;
reserved[2] = 34;
reserved[3] = 7;
}
// Stores the values into an available region
buffer.commit(4);
{
// Gets the front data stored in the region as a contiguous block
let block = buffer.read_front().unwrap();
assert_eq!(block[0], 2);
assert_eq!(block[1], 12);
assert_eq!(block[2], 34);
assert_eq!(block[3], 7);
}
// Release the first two front elements of the block
buffer.free_front(2);
{
let block = buffer.read_front().unwrap();
assert_eq!(block[0], 34);
assert_eq!(block[1], 7);
}
If a reservation exceeds the buffer capacity, the chosen allocation strategy will determine whether new space is allocated or if an error is returned.
use triskell::{TRBuffer, AllocationStrategy};
let mut buffer: TRBuffer<u8> = TRBuffer::new();
buffer.set_allocation_strategy(AllocationStrategy::Exact);
As its name imply a tri-partite ring buffer use three regions instead of two. This allows to read and write data at the back and/or at the front of the buffer.