From 19ede6dfed7bb10d90067a558f74802fddb57d2b Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 14 May 2025 08:45:34 +0300 Subject: [PATCH] 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 --- src/pull_queue.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/pull_queue.rs b/src/pull_queue.rs index b4d7a82f..5c97ee5a 100644 --- a/src/pull_queue.rs +++ b/src/pull_queue.rs @@ -38,7 +38,7 @@ impl Clone for PullQueue { } } -impl PullQueue { +impl PullQueue { /// 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 PullQueue { 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 Unlocked { 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 {