Crates.io | direct_ring_buffer |
lib.rs | direct_ring_buffer |
version | 0.2.0 |
source | src |
created_at | 2024-05-30 01:23:45.810392 |
updated_at | 2024-06-03 15:08:31.579212 |
description | A high-performance, lock-free ring buffer for single-producer, single-consumer scenarios. |
homepage | https://github.com/ain1084/direct_ring_buffer |
repository | https://github.com/ain1084/direct_ring_buffer |
max_upload_size | |
id | 1256372 |
size | 54,578 |
This crate provides a high-performance, lock-free ring buffer for single-producer,
single-consumer scenarios. The main components of this crate are the Producer
and
Consumer
structures, which allow for efficient elements writing and reading,
respectively.
A ring buffer is a fixed-size buffer that works as a circular queue. This implementation
uses a lock-free approach, making it suitable for real-time applications where minimal
latency is crucial. The buffer supports generic types with the Copy
trait. The Copy
trait is mainly used to prevent the use of types implementing Drop
, as the buffer is
allocated uninitialized.
use direct_ring_buffer::{create_ring_buffer, Producer, Consumer};
let (mut producer, mut consumer) = create_ring_buffer::<u8>(5);
// Writing data to the buffer
producer.write_slices(|data, _offset| {
data[..3].copy_from_slice(&[1, 2, 3]);
3
}, None);
assert!(producer.write_element(4));
assert!(producer.write_element(5));
assert_eq!(producer.available(), 0);
// Reading data from the buffer
consumer.read_slices(|data, _offset| {
assert_eq!(data, &[1, 2, 3, 4, 5]);
4
}, None);
assert_eq!(consumer.read_element(), Some(5));
assert_eq!(consumer.read_element(), None);
assert_eq!(consumer.available(), 0);
write_slices
.write_element
.write_element
, filling the buffer.read_slices
.read_element
.This implementation ensures that elements are accessed safely through unsafe
blocks
with proper checks. The use of atomic operations ensures minimal overhead for
synchronization, making it suitable for high-performance applications.
Designed to handle multiple elements at once, reducing overhead for batch processing. Single-element operations may incur significant overhead.
Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.