[−][src]Struct rtrb::RingBuffer
A bounded single-producer single-consumer queue.
Elements can be written with a Producer
and read with a Consumer
,
both of which can be obtained with RingBuffer::split()
.
See also the crate-level documentation.
Implementations
impl<T> RingBuffer<T>
[src]
pub fn new(capacity: usize) -> RingBuffer<T>
[src]
Creates a RingBuffer
with the given capacity
.
The returned RingBuffer
is typically immediately split into
the Producer
and the Consumer
side by split()
.
If you want guaranteed wrap-around behavior,
use with_chunks()
.
Examples
use rtrb::RingBuffer; let rb = RingBuffer::<f32>::new(100);
Specifying an explicit type with the turbofish is is only necessary if it cannot be deduced by the compiler.
use rtrb::RingBuffer; let (mut producer, consumer) = RingBuffer::new(100).split(); assert_eq!(producer.push(0.0f32), Ok(()));
pub fn with_chunks(chunks: usize, chunk_size: usize) -> RingBuffer<T>
[src]
Creates a RingBuffer
with a capacity of chunks * chunk_size
.
On top of multiplying the two numbers for us,
this also guarantees that the ring buffer wrap-around happens
at an integer multiple of chunk_size
.
This means that if Consumer::read_chunk()
is used exclusively with
chunk_size
(and Consumer::pop()
is not used in-between),
the first slice returned from ReadChunk::as_slices()
will always contain the entire chunk and the second slice will always be empty.
Same for Producer::write_chunk()
/WriteChunk::as_mut_slices()
and
Producer::write_chunk_uninit()
/WriteChunkUninit::as_mut_slices()
(as long as Producer::push()
is not used in-between).
If above conditions have been violated, the wrap-around guarantee can be restored
wit reset()
.
pub fn split(self) -> (Producer<T>, Consumer<T>)
[src]
Splits the RingBuffer
into Producer
and Consumer
.
Examples
use rtrb::RingBuffer; let (producer, consumer) = RingBuffer::<f32>::new(100).split();
pub fn reset(producer: &mut Producer<T>, consumer: &mut Consumer<T>)
[src]
Resets a ring buffer.
This drops all elements that are currently in the queue
(running their destructors if T
implements Drop
) and
resets the internal read and write positions to the beginning of the buffer.
This also resets the guarantees given by with_chunks()
.
Exclusive access to both Producer
and Consumer
is needed for this operation.
They can be moved between threads, for example, with a RingBuffer<Producer<T>>
and a RingBuffer<Consumer<T>>
, respectively.
Panics
Panics if the producer
and consumer
do not originate from the same RingBuffer
.
Examples
use rtrb::RingBuffer; let (mut p, mut c) = RingBuffer::new(2).split(); p = std::thread::spawn(move || { assert_eq!(p.push(10), Ok(())); p }).join().unwrap(); RingBuffer::reset(&mut p, &mut c); // The full capacity is now available for writing: if let Ok(mut chunk) = p.write_chunk(p.buffer().capacity()) { let (first, second) = chunk.as_mut_slices(); // The first slice is now guaranteed to span the whole buffer: first[0] = 20; first[1] = 30; assert!(second.is_empty()); chunk.commit_all(); } else { unreachable!(); }
pub fn capacity(&self) -> usize
[src]
Returns the capacity of the queue.
Examples
use rtrb::RingBuffer; let rb = RingBuffer::<f32>::new(100); assert_eq!(rb.capacity(), 100);
Trait Implementations
impl<T: Debug> Debug for RingBuffer<T>
[src]
impl<T> Drop for RingBuffer<T>
[src]
impl<T> Eq for RingBuffer<T>
[src]
impl<T> PartialEq<RingBuffer<T>> for RingBuffer<T>
[src]
pub fn eq(&self, other: &Self) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
.
Examples
use rtrb::RingBuffer; let (p, c) = RingBuffer::<f32>::new(1000).split(); assert_eq!(p.buffer(), c.buffer()); let rb1 = RingBuffer::<f32>::new(1000); let rb2 = RingBuffer::<f32>::new(1000); assert_ne!(rb1, rb2);
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for RingBuffer<T> where
T: RefUnwindSafe,
[src]
T: RefUnwindSafe,
impl<T> !Send for RingBuffer<T>
[src]
impl<T> !Sync for RingBuffer<T>
[src]
impl<T> Unpin for RingBuffer<T> where
T: Unpin,
[src]
T: Unpin,
impl<T> UnwindSafe for RingBuffer<T> where
T: RefUnwindSafe + UnwindSafe,
[src]
T: RefUnwindSafe + UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,