Struct jack::RingBuffer

source ·
pub struct RingBuffer(_);
Expand description

A lock-free ringbuffer. The key attribute of a ringbuffer is that it can be safely accessed by two threads simultaneously, one reading from the buffer and the other writing to it - without using any synchronization or mutual exclusion primitives. For this to work correctly, there can only be a single reader and a single writer thread. Their identities cannot be interchanged.

Example

let ringbuf = jack::RingBuffer::new(1024).unwrap();
let (mut reader, mut writer) = ringbuf.into_reader_writer();

let buf = [0_u8, 1, 2, 3];
let num = writer.write_buffer(&buf);
assert_eq!(num, buf.len());

// Potentially in a another thread:
let mut outbuf = [0_u8; 8];
let num = reader.read_buffer(&mut outbuf);

Implementations§

source§

impl RingBuffer

source

pub fn new(size: usize) -> Result<Self, Error>

Allocates a ringbuffer of a specified size.

source

pub fn mlock(&mut self)

Lock a ringbuffer data block into memory.

source

pub fn reset(&mut self)

Resets the ring buffer, making an empty buffer.

source

pub fn into_reader_writer(self) -> (RingBufferReader, RingBufferWriter)

Create a reader and writer, to use the ring buffer.

source

pub fn from_reader_writer(r: RingBufferReader, w: RingBufferWriter) -> Self

Re-create the ring buffer object from reader and writer. useful if you need to call reset. The reader and the writer pair must have been created from the same RingBuffer object. Not needed for deallocation, disposing of both reader and writer will deallocate buffer resources automatically.

panics if the reader and the writer were created from different RingBuffer objects.

Trait Implementations§

source§

impl Drop for RingBuffer

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for RingBuffer

source§

impl Sync for RingBuffer

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere
U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.