| 1 | use craterust-analyzer cargo ringbuf 0.3.3 crate/::{ |
| 2 | ring_bufferrust-analyzer cargo ringbuf 0.3.3 ring_buffer/::{RbBaserust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#, RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#, RbWraprust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#, RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#, RbWriteCacherust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/RbWriteCache#}, |
| 3 | utilsrust-analyzer cargo ringbuf 0.3.3 utils/::write_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_slice()., |
| 4 | }; |
| 5 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::MaybeUninitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/MaybeUninit#}; |
| 6 | |
| 7 | #[cfg(feature = "std")] |
| 8 | use craterust-analyzer cargo ringbuf 0.3.3 crate/::utilsrust-analyzer cargo ringbuf 0.3.3 utils/::slice_assume_init_mutrust-analyzer cargo ringbuf 0.3.3 utils/slice_assume_init_mut().; |
| 9 | #[cfg(feature = "std")] |
| 10 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/; |
| 11 | #[cfg(feature = "std")] |
| 12 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 13 | |
| 14 | /// Producer part of ring buffer. |
| 15 | /// |
| 16 | /// # Mode |
| 17 | /// |
| 18 | /// It can operate in immediate (by default) or postponed mode. |
| 19 | /// Mode could be switched using [`Self::postponed`]/[`Self::into_postponed`] and [`Self::into_immediate`] methods. |
| 20 | /// |
| 21 | /// + In immediate mode removed and inserted items are automatically synchronized with the other end. |
| 22 | /// + In postponed mode synchronization occurs only when [`Self::sync`] or [`Self::into_immediate`] is called or when `Self` is dropped. |
| 23 | /// The reason to use postponed mode is that multiple subsequent operations are performed faster due to less frequent cache synchronization. |
| 24 | pub struct Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> |
| 25 | where |
| 26 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<T>, |
| 27 | { |
| 28 | targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target.: R, |
| 29 | _phantomrust-analyzer cargo ringbuf 0.3.3 producer/Producer#_phantom.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<T>, |
| 30 | } |
| 31 | |
| 32 | impl<T, R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, R> |
| 33 | where |
| 34 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<T>, |
| 35 | { |
| 36 | /// Creates producer from the ring buffer reference. |
| 37 | /// |
| 38 | /// # Safety |
| 39 | /// |
| 40 | /// There must be only one producer containing the same ring buffer reference. |
| 41 | pub unsafe fn newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(target: R) -> Selfrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`] { |
| 42 | Selfrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`] { |
| 43 | targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target., |
| 44 | _phantomrust-analyzer cargo ringbuf 0.3.3 producer/Producer#_phantom.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Returns reference to the underlying ring buffer. |
| 49 | #[inline] |
| 50 | pub fn rbrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]rb().(&self) -> &R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb# { |
| 51 | &self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target. |
| 52 | } |
| 53 | |
| 54 | /// Consumes `self` and returns underlying ring buffer reference. |
| 55 | pub fn into_rb_refrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]into_rb_ref().(self) -> R { |
| 56 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target. |
| 57 | } |
| 58 | |
| 59 | /// Returns postponed producer that borrows [`Self`]. |
| 60 | pub fn postponedrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]postponed().(&mut self) -> PostponedProducerrust-analyzer cargo ringbuf 0.3.3 producer/PostponedProducer#<T, &R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#> { |
| 61 | unsafe { Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(RbWraprust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#(RbWriteCacherust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/RbWriteCache#::newrust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/impl#[`RbWriteCache<T, R>`]new().(&self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target.))) } |
| 62 | } |
| 63 | |
| 64 | /// Transforms [`Self`] into postponed producer. |
| 65 | pub fn into_postponedrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]into_postponed().(self) -> PostponedProducerrust-analyzer cargo ringbuf 0.3.3 producer/PostponedProducer#<T, R> { |
| 66 | unsafe { Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(RbWraprust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#(RbWriteCacherust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/RbWriteCache#::newrust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/impl#[`RbWriteCache<T, R>`]new().(self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target.))) } |
| 67 | } |
| 68 | |
| 69 | /// Returns capacity of the ring buffer. |
| 70 | /// |
| 71 | /// The capacity of the buffer is constant. |
| 72 | #[inline] |
| 73 | pub fn capacityrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]capacity().(&self) -> usize { |
| 74 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..capacity_nonzerorust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#capacity_nonzero().().getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/nonzero/impl#[`NonZero<T>`]get().() |
| 75 | } |
| 76 | |
| 77 | /// Checks if the ring buffer is empty. |
| 78 | #[inline] |
| 79 | pub fn is_emptyrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]is_empty().(&self) -> bool { |
| 80 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..is_emptyrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#is_empty().() |
| 81 | } |
| 82 | |
| 83 | /// Checks if the ring buffer is full. |
| 84 | /// |
| 85 | /// *The result may become irrelevant at any time because of concurring consumer activity.* |
| 86 | #[inline] |
| 87 | pub fn is_fullrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]is_full().(&self) -> bool { |
| 88 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..is_fullrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#is_full().() |
| 89 | } |
| 90 | |
| 91 | /// The number of items stored in the buffer. |
| 92 | /// |
| 93 | /// *Actual number may be less than the returned value because of concurring consumer activity.* |
| 94 | #[inline] |
| 95 | pub fn lenrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]len().(&self) -> usize { |
| 96 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..occupied_lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#occupied_len().() |
| 97 | } |
| 98 | |
| 99 | /// The number of remaining free places in the buffer. |
| 100 | /// |
| 101 | /// *Actual number may be greater than the returning value because of concurring consumer activity.* |
| 102 | #[inline] |
| 103 | pub fn free_lenrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]free_len().(&self) -> usize { |
| 104 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..vacant_lenrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbBase#vacant_len().() |
| 105 | } |
| 106 | |
| 107 | /// Provides a direct access to the ring buffer vacant memory. |
| 108 | /// Returns a pair of slices of uninitialized memory, the second one may be empty. |
| 109 | /// |
| 110 | /// # Safety |
| 111 | /// |
| 112 | /// Vacant memory is uninitialized. Initialized items must be put starting from the beginning of first slice. |
| 113 | /// When first slice is fully filled then items must be put to the beginning of the second slice. |
| 114 | /// |
| 115 | /// *This method must be followed by `Self::advance` call with the number of items being put previously as argument.* |
| 116 | /// *No other mutating calls allowed before that.* |
| 117 | #[inline] |
| 118 | pub unsafe fn free_space_as_slicesrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]free_space_as_slices().( |
| 119 | &mut self, |
| 120 | ) -> (&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>]) { |
| 121 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..vacant_slicesrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#vacant_slices().() |
| 122 | } |
| 123 | |
| 124 | /// Moves `tail` counter by `count` places. |
| 125 | /// |
| 126 | /// # Safety |
| 127 | /// |
| 128 | /// First `count` items in free space must be initialized. |
| 129 | #[inline] |
| 130 | pub unsafe fn advancerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]advance().(&mut self, count: usize) { |
| 131 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..advance_tailrust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#advance_tail().(count) |
| 132 | } |
| 133 | |
| 134 | /// Appends an item to the ring buffer. |
| 135 | /// |
| 136 | /// On failure returns an `Err` containing the item that hasn't been appended. |
| 137 | pub fn pushrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]push().(&mut self, elem: T) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), T> { |
| 138 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.is_fullrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]is_full().() { |
| 139 | unsafe { |
| 140 | self.free_space_as_slicesrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]free_space_as_slices().() |
| 141 | .0 |
| 142 | .get_unchecked_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get_unchecked_mut().(0) |
| 143 | .writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]write().(elem) |
| 144 | }; |
| 145 | unsafe { self.advancerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]advance().(1) }; |
| 146 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 147 | } else { |
| 148 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(elem) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// Appends items from an iterator to the ring buffer. |
| 153 | /// Elements that haven't been added to the ring buffer remain in the iterator. |
| 154 | /// |
| 155 | /// Returns count of items been appended to the ring buffer. |
| 156 | /// |
| 157 | /// *Inserted items are committed to the ring buffer all at once in the end,* |
| 158 | /// *e.g. when buffer is full or iterator has ended.* |
| 159 | pub fn push_iterrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]push_iter().<I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = T>>(&mut self, iter: &mut I) -> usize { |
| 160 | let (left, right) = unsafe { self.free_space_as_slicesrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]free_space_as_slices().() }; |
| 161 | let mut count = 0; |
| 162 | for place in left.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().chainrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#chain().(right.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().()) { |
| 163 | match iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 164 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(elem) => unsafe { place.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/maybe_uninit/impl#[`MaybeUninit<T>`]as_mut_ptr().().writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]write().(elem) }, |
| 165 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => break, |
| 166 | } |
| 167 | count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 168 | } |
| 169 | unsafe { self.advancerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]advance().(count) }; |
| 170 | count |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | impl<T: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy#, R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, R> |
| 175 | where |
| 176 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<T>, |
| 177 | { |
| 178 | /// Appends items from slice to the ring buffer. |
| 179 | /// Elements must be [`Copy`]. |
| 180 | /// |
| 181 | /// Returns count of items been appended to the ring buffer. |
| 182 | pub fn push_slicerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]push_slice().(&mut self, elems: &[T]) -> usize { |
| 183 | let (left, right) = unsafe { self.free_space_as_slicesrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]free_space_as_slices().() }; |
| 184 | let count = if elems.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 185 | write_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_slice().(&mut left[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#elems.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()., elems); |
| 186 | elems.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 187 | } else { |
| 188 | let (left_elems, elems) = elems.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at().(left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 189 | write_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_slice().(left, left_elems); |
| 190 | left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 191 | +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().if elems.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().right.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 192 | write_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_slice().(&mut right[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#elems.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()., elems); |
| 193 | elems.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 194 | } else { |
| 195 | write_slicerust-analyzer cargo ringbuf 0.3.3 utils/write_slice().(right, &elems[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#right.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 196 | right.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() |
| 197 | } |
| 198 | }; |
| 199 | unsafe { self.advancerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]advance().(count) }; |
| 200 | count |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /// Postponed producer. |
| 205 | pub type PostponedProducerrust-analyzer cargo ringbuf 0.3.3 producer/PostponedProducer#<T, R> = Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, RbWraprust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#<RbWriteCacherust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/RbWriteCache#<T, R>>>; |
| 206 | |
| 207 | impl<T, R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> PostponedProducerrust-analyzer cargo ringbuf 0.3.3 producer/PostponedProducer#<T, R> |
| 208 | where |
| 209 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<T>, |
| 210 | { |
| 211 | /// Create new postponed producer. |
| 212 | /// |
| 213 | /// # Safety |
| 214 | /// |
| 215 | /// There must be only one producer containing the same ring buffer reference. |
| 216 | pub unsafe fn new_postponedrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, RbWrap<RbWriteCache<T, R>>>`]new_postponed().(target: R) -> Selfrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, RbWrap<RbWriteCache<T, R>>>`] { |
| 217 | Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(RbWraprust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#(RbWriteCacherust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/RbWriteCache#::newrust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/impl#[`RbWriteCache<T, R>`]new().(target))) |
| 218 | } |
| 219 | |
| 220 | /// Synchronize changes with the ring buffer. |
| 221 | /// |
| 222 | /// Postponed producer requires manual synchronization to make pushed items visible for the consumer. |
| 223 | pub fn syncrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, RbWrap<RbWriteCache<T, R>>>`]sync().(&mut self) { |
| 224 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..0rust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#0..syncrust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/impl#[`RbWriteCache<T, R>`]sync().(); |
| 225 | } |
| 226 | |
| 227 | /// Don't publish and drop items inserted since last synchronization. |
| 228 | pub fn discardrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, RbWrap<RbWriteCache<T, R>>>`]discard().(&mut self) { |
| 229 | self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..0rust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#0..discardrust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/impl#[`RbWriteCache<T, R>`]discard().(); |
| 230 | } |
| 231 | |
| 232 | /// Synchronize and transform back to immediate producer. |
| 233 | pub fn into_immediaterust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, RbWrap<RbWriteCache<T, R>>>`]into_immediate().(self) -> Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<T, R> { |
| 234 | unsafe { Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#::newrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]new().(self.targetrust-analyzer cargo ringbuf 0.3.3 producer/Producer#target..0rust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbWrap#0..releaserust-analyzer cargo ringbuf 0.3.3 ring_buffer/cache/impl#[`RbWriteCache<T, R>`]release().()) } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #[cfg(feature = "std")] |
| 239 | impl<R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<u8, R> |
| 240 | where |
| 241 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<u8>, |
| 242 | { |
| 243 | /// Reads at most `count` bytes from `Read` instance and appends them to the ring buffer. |
| 244 | /// If `count` is `None` then as much as possible bytes will be read. |
| 245 | /// |
| 246 | /// Returns `Ok(n)` if `read` succeeded. `n` is number of bytes been read. |
| 247 | /// `n == 0` means that either `read` returned zero or ring buffer is full. |
| 248 | /// |
| 249 | /// If `read` is failed then original error is returned. In this case it is guaranteed that no items was read from the reader. |
| 250 | /// To achieve this we read only one contiguous slice at once. So this call may read less than `remaining` items in the buffer even if the reader is ready to provide more. |
| 251 | pub fn read_fromrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<u8, R>`]read_from().<P: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#>( |
| 252 | &mut self, |
| 253 | reader: &mut P, |
| 254 | count: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>, |
| 255 | ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 256 | let (left, _) = unsafe { self.free_space_as_slicesrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]free_space_as_slices().() }; |
| 257 | let count = cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(count.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()), left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 258 | let left_init = unsafe { slice_assume_init_mutrust-analyzer cargo ringbuf 0.3.3 utils/slice_assume_init_mut().(&mut left[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#count]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().) }; |
| 259 | |
| 260 | let read_count = reader.readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(left_init)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 261 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(read_count <= count); |
| 262 | unsafe { self.advancerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]advance().(read_count) }; |
| 263 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(read_count) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | #[cfg(feature = "std")] |
| 268 | impl<R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<u8, R> |
| 269 | where |
| 270 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<u8>, |
| 271 | { |
| 272 | fn writerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<u8, R>`][Write]write().(&mut self, buffer: &[u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 273 | let n = self.push_slicerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]push_slice().(buffer); |
| 274 | if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().buffer.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 275 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::WouldBlockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#WouldBlock#.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 276 | } else { |
| 277 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(n) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | fn flushrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<u8, R>`][Write]flush().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 282 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | impl<R: RbRefrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write# for Producerrust-analyzer cargo ringbuf 0.3.3 producer/Producer#<u8, R> |
| 287 | where |
| 288 | R::Rbrust-analyzer cargo ringbuf 0.3.3 ring_buffer/rb/RbRef#Rb#: RbWriterust-analyzer cargo ringbuf 0.3.3 ring_buffer/base/RbWrite#<u8>, |
| 289 | { |
| 290 | fn write_strrust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<u8, R>`][Write]write_str().(&mut self, s: &str) -> corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 291 | let n = self.push_slicerust-analyzer cargo ringbuf 0.3.3 producer/impl#[`Producer<T, R>`]push_slice().(s.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 292 | if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().s.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() { |
| 293 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Error#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[Error][Default]default().()) |
| 294 | } else { |
| 295 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 296 | } |
| 297 | } |
| 298 | } |