| 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 | cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#, |
| 5 | 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#}, |
| 6 | 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#, |
| 7 | ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/, |
| 8 | }; |
| 9 | |
| 10 | #[cfg(feature = "alloc")] |
| 11 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/::Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#; |
| 12 | |
| 13 | /// Ring buffer for using in single thread. |
| 14 | /// |
| 15 | /// Does *not* implement [`Sync`]. And its [`Producer`] and [`Consumer`] do *not* implement [`Send`]. |
| 16 | /// |
| 17 | #[cfg_attr( |
| 18 | feature = "std", |
| 19 | doc = r##" |
| 20 | This code must fail to compile: |
| 21 | |
| 22 | ```compile_fail |
| 23 | use std::{thread, vec::Vec}; |
| 24 | use ringbuf::LocalRb; |
| 25 | |
| 26 | let (mut prod, mut cons) = LocalRb::<i32, Vec<_>>::new(256).split(); |
| 27 | thread::spawn(move || { |
| 28 | prod.push(123).unwrap(); |
| 29 | }) |
| 30 | .join(); |
| 31 | thread::spawn(move || { |
| 32 | assert_eq!(cons.pop().unwrap(), 123); |
| 33 | }) |
| 34 | .join(); |
| 35 | ``` |
| 36 | "## |
| 37 | )] |
| 38 | pub struct LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> { |
| 39 | storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#storage.: SharedStoragerust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/SharedStorage#<T, C>, |
| 40 | headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#head.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<usize>, |
| 41 | tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#tail.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#<usize>, |
| 42 | } |
| 43 | |
| 44 | 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 LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C> { |
| 45 | #[inline] |
| 46 | unsafe fn slicesrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbBase<T>`]slices().( |
| 47 | &self, |
| 48 | head: usize, |
| 49 | tail: usize, |
| 50 | ) -> (&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>]) { |
| 51 | self.storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#storage..as_mut_slicesrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]as_mut_slices().(head, tail) |
| 52 | } |
| 53 | |
| 54 | #[inline] |
| 55 | fn capacity_nonzerorust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbBase<T>`]capacity_nonzero().(&self) -> NonZeroUsizerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/NonZeroUsize# { |
| 56 | self.storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#storage..lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]len().() |
| 57 | } |
| 58 | |
| 59 | #[inline] |
| 60 | fn headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbBase<T>`]head().(&self) -> usize { |
| 61 | self.headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#head..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().() |
| 62 | } |
| 63 | |
| 64 | #[inline] |
| 65 | fn tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbBase<T>`]tail().(&self) -> usize { |
| 66 | self.tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#tail..getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]get().() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | 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 LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C> { |
| 71 | #[inline] |
| 72 | unsafe fn set_headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbRead<T>`]set_head().(&self, value: usize) { |
| 73 | self.headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#head..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(value); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | 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 LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C> { |
| 78 | #[inline] |
| 79 | unsafe fn set_tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbWrite<T>`]set_tail().(&self, value: usize) { |
| 80 | self.tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#tail..setrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]set().(value); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | 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 LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C> {} |
| 85 | |
| 86 | 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 LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C> { |
| 87 | fn droprust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][Drop]drop().(&mut self) { |
| 88 | self.clearrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/Rb#clear().(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl<T, C: Containerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/Container#<T>> LocalRbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#<T, C> { |
| 93 | /// Constructs ring buffer from container and counters. |
| 94 | /// |
| 95 | /// # Safety |
| 96 | /// |
| 97 | /// The items in container inside `head..tail` range must be initialized, items outside this range must be uninitialized. |
| 98 | /// `head` and `tail` values must be valid (see [`RbBase`](`crate::ring_buffer::RbBase`)). |
| 99 | pub unsafe fn from_raw_partsrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]from_raw_parts().(container: C, head: usize, tail: usize) -> Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`] { |
| 100 | Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`] { |
| 101 | storagerust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#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), |
| 102 | headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#head.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(head), |
| 103 | tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/LocalRb#tail.: Cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/Cell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`Cell<T>`]new().(tail), |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// Destructures ring buffer into underlying container and `head` and `tail` counters. |
| 108 | /// |
| 109 | /// # Safety |
| 110 | /// |
| 111 | /// Initialized contents of the container must be properly dropped. |
| 112 | pub unsafe fn into_raw_partsrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]into_raw_parts().(self) -> (C, usize, usize) { |
| 113 | let (head, tail) = (self.headrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbBase<T>`]head().(), self.tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`][`RbBase<T>`]tail().()); |
| 114 | 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); |
| 115 | (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/local/LocalRb#storage.).into_innerrust-analyzer cargo ringbuf 0.3.3 ring_buffer/storage/impl#[`SharedStorage<T, C>`]into_inner().(), head, tail) |
| 116 | } |
| 117 | |
| 118 | /// Splits ring buffer into producer and consumer. |
| 119 | /// |
| 120 | /// This method consumes the ring buffer and puts it on heap in [`Rc`]. If you don't want to use heap the see [`Self::split_ref`]. |
| 121 | #[cfg(feature = "alloc")] |
| 122 | pub fn splitrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]split().(self) -> (Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]>>, Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#<T, Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#<Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]>>) |
| 123 | where |
| 124 | Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 125 | { |
| 126 | let rc = Rcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/Rc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<T>`]new().(self); |
| 127 | unsafe { (Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(rc.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc rc/impl#[`Rc<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().(rc)) } |
| 128 | } |
| 129 | |
| 130 | /// Splits ring buffer into producer and consumer without using the heap. |
| 131 | /// |
| 132 | /// In this case producer and consumer stores a reference to the ring buffer, so you also need to store the buffer somewhere. |
| 133 | pub fn split_refrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<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/local/impl#[`LocalRb<T, C>`]>, Consumerrust-analyzer cargo ringbuf 0.3.3 consumer/Consumer#<T, &Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]>) |
| 134 | where |
| 135 | Selfrust-analyzer cargo ringbuf 0.3.3 ring_buffer/local/impl#[`LocalRb<T, C>`]: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#, |
| 136 | { |
| 137 | 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)) } |
| 138 | } |
| 139 | } |