| 1 | use superrust-analyzer cargo ringbuf 0.3.3 ring_buffer/::{Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#, Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/Rb#, RbBaserust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#, RbReadrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbRead#, RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#, SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#}; |
| 2 | use craterust-analyzer cargo ringbuf 0.3.3 crate/::{consumerrust-analyzer cargo ringbuf 0.3.3 consumer/::Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#, producerrust-analyzer cargo ringbuf 0.3.3 producer/::Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#}; |
| 3 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{ |
| 4 | memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::{ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#, MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#}, |
| 5 | numrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/::NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize#, |
| 6 | ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/, |
| 7 | syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#}, |
| 8 | }; |
| 9 | use crossbeam_utilsrust-analyzer cargo crossbeam-utils 0.8.15 crate/::CachePaddedrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/CachePadded#; |
| 10 | |
| 11 | #[cfg(feature = "alloc")] |
| 12 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::syncrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#; |
| 13 | |
| 14 | /// Ring buffer that could be shared between threads. |
| 15 | /// |
| 16 | /// Implements [`Sync`] *if `T` implements [`Send`]*. And therefore its [`Producer`] and [`Consumer`] implement [`Send`]. |
| 17 | /// |
| 18 | /// Note that there is no explicit requirement of `T: Send`. Instead [`SharedRb`] will work just fine even with `T: !Send` |
| 19 | /// until you try to send its [`Producer`] or [`Consumer`] to another thread. |
| 20 | #[cfg_attr( |
| 21 | feature = "std", |
| 22 | doc = r##" |
| 23 | ``` |
| 24 | use std::{thread, vec::Vec}; |
| 25 | use ringbuf::SharedRb; |
| 26 | |
| 27 | let (mut prod, mut cons) = SharedRb::<i32, Vec<_>>::new(256).split(); |
| 28 | thread::spawn(move || { |
| 29 | prod.push(123).unwrap(); |
| 30 | }) |
| 31 | .join(); |
| 32 | thread::spawn(move || { |
| 33 | assert_eq!(cons.pop().unwrap(), 123); |
| 34 | }) |
| 35 | .join(); |
| 36 | ``` |
| 37 | "## |
| 38 | )] |
| 39 | pub struct SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> { |
| 40 | storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#storage.: SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#<T, C>, |
| 41 | headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#head.: CachePaddedrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/CachePadded#<AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#>, |
| 42 | tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#tail.: CachePaddedrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/CachePadded#<AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#>, |
| 43 | } |
| 44 | |
| 45 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> RbBaserust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#<T> for SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C> { |
| 46 | #[inline] |
| 47 | unsafe fn slicesrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbBase<T>`]slices().( |
| 48 | &self, |
| 49 | head: usize, |
| 50 | tail: usize, |
| 51 | ) -> (&mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>], &mut [MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#<T>]) { |
| 52 | self.storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#storage..as_mut_slicesrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]as_mut_slices().(head, tail) |
| 53 | } |
| 54 | |
| 55 | #[inline] |
| 56 | fn capacity_nonzerorust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbBase<T>`]capacity_nonzero().(&self) -> NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize# { |
| 57 | self.storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#storage..lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]len().() |
| 58 | } |
| 59 | |
| 60 | #[inline] |
| 61 | fn headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbBase<T>`]head().(&self) -> usize { |
| 62 | self.headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#head..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Acquirerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Acquire#) |
| 63 | } |
| 64 | |
| 65 | #[inline] |
| 66 | fn tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbBase<T>`]tail().(&self) -> usize { |
| 67 | self.tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#tail..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Acquirerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Acquire#) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> RbReadrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbRead#<T> for SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C> { |
| 72 | #[inline] |
| 73 | unsafe fn set_headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbRead<T>`]set_head().(&self, value: usize) { |
| 74 | self.headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#head..storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]store().(value, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Releaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Release#) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<T> for SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C> { |
| 79 | #[inline] |
| 80 | unsafe fn set_tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbWrite<T>`]set_tail().(&self, value: usize) { |
| 81 | self.tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#tail..storerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]store().(value, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Releaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Release#) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/Rb#<T> for SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C> {} |
| 86 | |
| 87 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C> { |
| 88 | fn droprust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][Drop]drop().(&mut self) { |
| 89 | self.clearrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/Rb#clear().(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> SharedRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#<T, C> { |
| 94 | /// Constructs ring buffer from container and counters. |
| 95 | /// |
| 96 | /// # Safety |
| 97 | /// |
| 98 | /// The items in container inside `head..tail` range must be initialized, items outside this range must be uninitialized. |
| 99 | /// `head` and `tail` values must be valid (see [`RbBase`](`crate::ring_buffer::RbBase`)). |
| 100 | pub unsafe fn from_raw_partsrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]from_raw_parts().(container: C, head: usize, tail: usize) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`] { |
| 101 | Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`] { |
| 102 | storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#storage.: SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#::newrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]new().(container), |
| 103 | headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#head.: CachePaddedrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/CachePadded#::newrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/impl#[`CachePadded<T>`]new().(AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]new().(head)), |
| 104 | tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#tail.: CachePaddedrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/CachePadded#::newrust-analyzer cargo crossbeam-utils 0.8.15 cache_padded/impl#[`CachePadded<T>`]new().(AtomicUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicUsize#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicUsize]new().(tail)), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// Destructures ring buffer into underlying container and `head` and `tail` counters. |
| 109 | /// |
| 110 | /// # Safety |
| 111 | /// |
| 112 | /// Initialized contents of the container must be properly dropped. |
| 113 | pub unsafe fn into_raw_partsrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]into_raw_parts().(self) -> (C, usize, usize) { |
| 114 | let (head, tail) = (self.headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbBase<T>`]head().(), self.tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`][`RbBase<T>`]tail().()); |
| 115 | let self_ = ManuallyDroprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/ManuallyDrop#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/manually_drop/impl#[`ManuallyDrop<T>`]new().(self); |
| 116 | |
| 117 | (ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::readrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/read().(&self_.storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/SharedRb#storage.).into_innerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]into_inner().(), head, tail) |
| 118 | } |
| 119 | |
| 120 | /// Splits ring buffer into producer and consumer. |
| 121 | /// |
| 122 | /// This method consumes the ring buffer and puts it on heap in [`Arc`]. If you don't want to use heap the see [`Self::split_ref`]. |
| 123 | #[cfg(feature = "alloc")] |
| 124 | pub fn splitrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]split().(self) -> (Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]>>, Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#<T, Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]>>) |
| 125 | where |
| 126 | Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 127 | { |
| 128 | let arc = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(self); |
| 129 | unsafe { (Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(arc.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().()), Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#::newrust-analyzer cargo ringbuf 0.3.3 consumer/impl#[`Consumer<T, R>`]new().(arc)) } |
| 130 | } |
| 131 | |
| 132 | /// Splits ring buffer into producer and consumer without using the heap. |
| 133 | /// |
| 134 | /// In this case producer and consumer stores a reference to the ring buffer, so you also need to store the buffer somewhere. |
| 135 | pub fn split_refrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]split_ref().(&mut self) -> (Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]>, Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#<T, &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]>) |
| 136 | where |
| 137 | Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/shared/impl#[`SharedRb<T, C>`]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 138 | { |
| 139 | unsafe { (Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(self), Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#::newrust-analyzer cargo ringbuf 0.3.3 consumer/impl#[`Consumer<T, R>`]new().(self)) } |
| 140 | } |
| 141 | } |