ringbuf/traits/
observer.rs1use super::{utils::modulus, Based};
2use core::{mem::MaybeUninit, num::NonZeroUsize};
3
4pub trait Observer {
8 type Item: Sized;
9
10 fn capacity(&self) -> NonZeroUsize;
14
15 fn read_index(&self) -> usize;
19 fn write_index(&self) -> usize;
23
24 unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>]);
32
33 unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>]);
39
40 fn read_is_held(&self) -> bool;
42 fn write_is_held(&self) -> bool;
44
45 fn occupied_len(&self) -> usize {
49 let modulus = modulus(self);
50 (modulus.get() + self.write_index() - self.read_index()) % modulus
51 }
52
53 fn vacant_len(&self) -> usize {
57 let modulus = modulus(self);
58 (self.capacity().get() + self.read_index() - self.write_index()) % modulus
59 }
60
61 #[inline]
65 fn is_empty(&self) -> bool {
66 self.read_index() == self.write_index()
67 }
68
69 #[inline]
73 fn is_full(&self) -> bool {
74 self.vacant_len() == 0
75 }
76}
77
78pub trait DelegateObserver: Based
80where
81 Self::Base: Observer,
82{
83}
84
85impl<D: DelegateObserver> Observer for D
86where
87 D::Base: Observer,
88{
89 type Item = <D::Base as Observer>::Item;
90
91 #[inline]
92 fn capacity(&self) -> NonZeroUsize {
93 self.base().capacity()
94 }
95
96 #[inline]
97 fn read_index(&self) -> usize {
98 self.base().read_index()
99 }
100 #[inline]
101 fn write_index(&self) -> usize {
102 self.base().write_index()
103 }
104
105 #[inline]
106 unsafe fn unsafe_slices(&self, start: usize, end: usize) -> (&[MaybeUninit<Self::Item>], &[MaybeUninit<Self::Item>]) {
107 self.base().unsafe_slices(start, end)
108 }
109 #[inline]
110 unsafe fn unsafe_slices_mut(&self, start: usize, end: usize) -> (&mut [MaybeUninit<Self::Item>], &mut [MaybeUninit<Self::Item>]) {
111 self.base().unsafe_slices_mut(start, end)
112 }
113
114 #[inline]
115 fn read_is_held(&self) -> bool {
116 self.base().read_is_held()
117 }
118 #[inline]
119 fn write_is_held(&self) -> bool {
120 self.base().write_is_held()
121 }
122
123 #[inline]
124 fn occupied_len(&self) -> usize {
125 self.base().occupied_len()
126 }
127
128 #[inline]
129 fn vacant_len(&self) -> usize {
130 self.base().vacant_len()
131 }
132
133 #[inline]
134 fn is_empty(&self) -> bool {
135 self.base().is_empty()
136 }
137
138 #[inline]
139 fn is_full(&self) -> bool {
140 self.base().is_full()
141 }
142}