mirror of
https://seed.radicle.dev/rad:zwTxygwuz5LDGBq255RA2CbNGrz8.git
synced 2026-11-21 19:00:38 +08:00
refactor(src/pull_queue.rs): avoid asserts
I didn't like having asserts, and didn't want debug_asserts. With plain asserts, the program would have panicked. With debug_asserts, the program would have silently overwritten the item in the queue. Instead, change the method to return whether it was able to set, and have the caller cope with not being able to set. The caller needs to be able handle that (and was already doing so). Found-by: Fintan Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
+9
-11
@@ -38,7 +38,7 @@ impl<T> Clone for PullQueue<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PullQueue<T> {
|
||||
impl<T: Clone> PullQueue<T> {
|
||||
/// Create a new [`PullQueue`].
|
||||
// No `Default` for this type, because we don't want to require
|
||||
// the type T to implement that.
|
||||
@@ -55,8 +55,7 @@ impl<T> PullQueue<T> {
|
||||
let (mutex, var) = &*self.q;
|
||||
let mut locked = mutex.lock().map_err(|_| PullQueueError::Mutex)?;
|
||||
loop {
|
||||
if locked.is_empty() {
|
||||
locked.set(i);
|
||||
if locked.try_set(i.clone()) {
|
||||
var.notify_all();
|
||||
break;
|
||||
} else {
|
||||
@@ -123,14 +122,13 @@ impl<T> Unlocked<T> {
|
||||
self.ended && self.item.is_none()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.item.is_none()
|
||||
}
|
||||
|
||||
fn set(&mut self, item: T) {
|
||||
assert!(self.item.is_none());
|
||||
assert!(!self.ended);
|
||||
self.item = Some(item);
|
||||
fn try_set(&mut self, item: T) -> bool {
|
||||
if self.item.is_none() {
|
||||
self.item = Some(item);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn get(&mut self) -> Option<T> {
|
||||
|
||||
Reference in New Issue
Block a user