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 {