1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Low-level implementation contract used by safe ring buffer operations.
use ;
use MaybeUninit;
/// Raw storage, cursor, and endpoint-ownership operations.
///
/// Implementing the safe [`super::Producer`] and [`super::Consumer`] interfaces
/// requires an explicit unsafe implementation of this contract.
///
/// # Safety
///
/// Capacity, indices, slices, and their overrides must describe the same live
/// storage. Capacity is constant and in `1..=usize::MAX / 2`; indices are below
/// twice the capacity. Raw slices must cover exactly the requested modular range;
/// a nonempty range must have a nonempty first slice.
/// Endpoint caches may conservatively underestimate available space or items,
/// but must never expose slots owned by the opposite endpoint.
///
/// All overrides of safe producer, consumer, and ring-buffer methods must preserve
/// their documented bounds, initialization, and transfer guarantees. In particular,
/// predicates, lengths, and slices used together by a default method must be consistent.
/// Valid raw slice access and capacity/index accessors must not unwind. Cursor
/// setters must publish the change before returning or unwinding, including when
/// a subsequent notification panics.
///
/// A type implementing `Consumer` must retain consumer rights for every data
/// view borrowed from it; a type implementing `Producer` must similarly retain
/// producer rights. A type implementing `RingBuffer` must grant exclusive access
/// to all storage through `&mut self`, even if earlier borrowed endpoints were
/// forgotten. Safe interfaces must not allow a competing role to be acquired
/// while a view is live. Sharing an endpoint that exposes `&Item` also requires
/// `Item: Sync`.
///
/// Ownership flags must atomically check and replace their previous state when
/// shared across threads. Raw access does not itself acquire endpoint rights:
/// callers must establish those rights and keep them for the duration of access.
///
/// A safe observer alone cannot implement a consumer:
///
/// ```compile_fail,E0277
/// use core::num::NonZeroUsize;
/// use ringbuf::traits::{Consumer, Observer};
/// struct Lying;
/// impl Observer for Lying {
/// type Item = String;
/// fn capacity(&self) -> NonZeroUsize { NonZeroUsize::new(1).unwrap() }
/// fn read_index(&self) -> usize { 0 }
/// fn write_index(&self) -> usize { 1 }
/// fn read_is_held(&self) -> bool { false }
/// fn write_is_held(&self) -> bool { false }
/// }
/// impl Consumer for Lying {}
/// Lying.try_pop();
/// ```
///
/// Nor does a safe observer satisfy the producer implementation contract:
///
/// ```compile_fail,E0277
/// use core::num::NonZeroUsize;
/// use ringbuf::traits::{Observer, Producer};
/// struct Lying;
/// impl Observer for Lying {
/// type Item = u8;
/// fn capacity(&self) -> NonZeroUsize { NonZeroUsize::new(1).unwrap() }
/// fn read_index(&self) -> usize { 0 }
/// fn write_index(&self) -> usize { 0 }
/// fn read_is_held(&self) -> bool { false }
/// fn write_is_held(&self) -> bool { false }
/// }
/// impl Producer for Lying {}
/// Lying.try_push(1).unwrap();
/// ```
pub unsafe
/// Opt in to forwarding the raw implementation contract to a base object.
///
/// # Safety
/// Both [`Based`] accessors must always refer to the same live base. Observer and
/// producer/consumer overrides must preserve its bounds and ownership guarantees.
/// The wrapper must satisfy the entire [`Raw`] contract for every safe interface
/// it implements. Forwarding raw operations grants no additional endpoint rights
/// and does not turn an endpoint into an exclusively owned `RingBuffer`.
/// Delegation must not duplicate endpoint rights or introduce additional storage
/// aliases beyond those available through the base interface.
///
/// Safe delegation cannot bypass the raw implementation obligation:
///
/// ```compile_fail,E0277
/// use ringbuf::traits::{Based, Consumer,
/// observer::DelegateObserver, consumer::DelegateConsumer};
/// struct Wrapper<B>(B);
/// impl<B> Based for Wrapper<B> {
/// type Base = B;
/// fn base(&self) -> &Self::Base { &self.0 }
/// fn base_mut(&mut self) -> &mut Self::Base { &mut self.0 }
/// }
/// impl<B: Consumer> DelegateObserver for Wrapper<B> {}
/// impl<B: Consumer> DelegateConsumer for Wrapper<B> {}
/// ```
pub unsafe
// SAFETY: DelegateRaw requires faithful delegation and consistent overrides.
unsafe