Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
refactor(src/pull_queue.rs): avoid asserts
Lars Wirzenius committed 1 year ago
commit 19ede6dfed7bb10d90067a558f74802fddb57d2b
parent 582bd54b73feaab767b17988b31fdb4910ca4bfe
1 file changed +9 -11
modified src/pull_queue.rs
@@ -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> {