24
24
/// assert_eq!(r.recv(), Some('b'));
25
25
/// ```
26
26
pub mod bounded {
27
- use std:: sync:: atomic:: Ordering ;
28
- use std:: sync:: atomic:: AtomicUsize ;
29
27
use std:: marker:: PhantomData ;
30
28
use std:: mem:: ManuallyDrop ;
29
+ use std:: sync:: atomic:: AtomicUsize ;
30
+ use std:: sync:: atomic:: Ordering ;
31
31
use utils:: CachePadded ;
32
32
33
33
use buffer:: Buffer ;
@@ -135,7 +135,7 @@ pub mod bounded {
135
135
unsafe { self . buffer . write ( index. wrapping_add ( self . lap ( ) ) , value) } ;
136
136
return Ok ( ( ) ) ;
137
137
}
138
- // But if the slot lags one lap behind the tail...
138
+ // But if the slot lags one lap behind the tail...
139
139
} else if index. wrapping_add ( self . lap ( ) ) == tail {
140
140
let head = self . head . load ( Ordering :: Acquire ) ;
141
141
@@ -179,7 +179,10 @@ pub mod bounded {
179
179
{
180
180
// Reads the value from the slot and update the stamp.
181
181
let value = unsafe { self . buffer . read_value ( index) } ;
182
- unsafe { self . buffer . write_index ( index. wrapping_add ( self . lap ( ) ) , Ordering :: Release ) } ;
182
+ unsafe {
183
+ self . buffer
184
+ . write_index ( index. wrapping_add ( self . lap ( ) ) , Ordering :: Release )
185
+ } ;
183
186
return Some ( ManuallyDrop :: into_inner ( value) ) ;
184
187
}
185
188
// But if the slot lags one lap behind the head...
@@ -194,7 +197,7 @@ pub mod bounded {
194
197
}
195
198
}
196
199
}
197
-
200
+
198
201
/// Returns `true` if the queue is empty.
199
202
///
200
203
/// Inaccurate in the presence of concurrent method invocations.
@@ -208,21 +211,21 @@ pub mod bounded {
208
211
// when the queue was not empty, so it is safe to just return `false`.
209
212
tail. wrapping_add ( self . lap ( ) ) == head
210
213
}
211
-
214
+
212
215
/// Returns `true` if the queue is full.
213
216
///
214
217
/// Inaccurate in the presence of concurrent method invocations.
215
218
pub fn is_full ( & self ) -> bool {
216
219
let tail = self . tail . load ( Ordering :: Relaxed ) ;
217
220
let head = self . head . load ( Ordering :: Relaxed ) ;
218
-
221
+
219
222
// Is the head lagging one lap behind tail?
220
223
//
221
224
// Note: If the tail changes just before we load the head, that means there was a moment
222
225
// when the queue was not full, so it is safe to just return `false`.
223
226
head. wrapping_add ( self . lap ( ) ) == tail
224
227
}
225
-
228
+
226
229
/// Returns the current number of elements inside the queue.
227
230
///
228
231
/// Inaccurate in the presence of concurrent method invocations.
@@ -233,7 +236,7 @@ pub mod bounded {
233
236
234
237
let hix = head & ( self . lap ( ) - 1 ) ;
235
238
let tix = tail & ( self . lap ( ) - 1 ) ;
236
-
239
+
237
240
if hix < tix {
238
241
tix - hix
239
242
} else if hix > tix {
@@ -250,7 +253,7 @@ pub mod bounded {
250
253
fn drop ( & mut self ) {
251
254
// Get the index of the head.
252
255
let hix = self . head . load ( Ordering :: Relaxed ) & ( self . lap ( ) - 1 ) ;
253
-
256
+
254
257
// Loop over all slots that hold a message and drop them.
255
258
for i in 0 ..self . len ( ) {
256
259
// Compute the index of the next slot holding a message.
0 commit comments