- while let Some((unit, job)) = self.queue.dequeue() {
- self.pending_queue.push((unit, job));
+ while let Some((unit, job, priority)) = self.queue.dequeue() {
+ // We want to keep the pieces of work in the `pending_queue` sorted
+ // by their priorities, and insert the current job at its correctly
+ // sorted position: following the lower priority jobs, and the ones
+ // with the same priority (since they were dequeued before the
+ // current one, we also keep that relation).
+ let idx = self
+ .pending_queue
+ .partition_point(|&(_, _, p)| p <= priority);
+ self.pending_queue.insert(idx, (unit, job, priority));