ringbuf/traits/
ring_buffer.rs1use super::{
2 consumer::{Consumer, DelegateConsumer},
3 producer::{DelegateProducer, Producer},
4 Observer,
5};
6
7pub trait RingBuffer: Observer + Consumer + Producer {
9 unsafe fn hold_read(&self, flag: bool) -> bool;
17 unsafe fn hold_write(&self, flag: bool) -> bool;
25
26 fn push_overwrite(&mut self, elem: Self::Item) -> Option<Self::Item> {
30 let ret = if self.is_full() { self.try_pop() } else { None };
31 let _ = self.try_push(elem);
32 ret
33 }
34
35 fn push_iter_overwrite<I: Iterator<Item = Self::Item>>(&mut self, iter: I) {
40 for elem in iter {
41 self.push_overwrite(elem);
42 }
43 }
44
45 fn push_slice_overwrite(&mut self, elems: &[Self::Item])
49 where
50 Self::Item: Copy,
51 {
52 if elems.len() > self.vacant_len() {
53 self.skip(usize::min(elems.len() - self.vacant_len(), self.occupied_len()));
54 }
55 self.push_slice(if elems.len() > self.vacant_len() {
56 &elems[(elems.len() - self.vacant_len())..]
57 } else {
58 elems
59 });
60 }
61}
62
63pub trait DelegateRingBuffer: DelegateProducer + DelegateConsumer
65where
66 Self::Base: RingBuffer,
67{
68}
69
70impl<D: DelegateRingBuffer> RingBuffer for D
71where
72 D::Base: RingBuffer,
73{
74 unsafe fn hold_read(&self, flag: bool) -> bool {
75 self.base().hold_read(flag)
76 }
77 unsafe fn hold_write(&self, flag: bool) -> bool {
78 self.base().hold_write(flag)
79 }
80
81 #[inline]
82 fn push_overwrite(&mut self, elem: Self::Item) -> Option<Self::Item> {
83 self.base_mut().push_overwrite(elem)
84 }
85
86 #[inline]
87 fn push_iter_overwrite<I: Iterator<Item = Self::Item>>(&mut self, iter: I) {
88 self.base_mut().push_iter_overwrite(iter)
89 }
90
91 #[inline]
92 fn push_slice_overwrite(&mut self, elems: &[Self::Item])
93 where
94 Self::Item: Copy,
95 {
96 self.base_mut().push_slice_overwrite(elems)
97 }
98}