Skip to content

Commit

Permalink
Merge pull request #266 from espindola/avoid-unwrap
Browse files Browse the repository at this point in the history
Implement Borrow<QueueId> for QueuedWaker
  • Loading branch information
blackbeam authored Dec 19, 2023
2 parents 6dc09a7 + eb735aa commit 6cdf461
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions src/conn/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use keyed_priority_queue::KeyedPriorityQueue;
use tokio::sync::mpsc;

use std::{
cmp::{Ordering, Reverse},
borrow::Borrow,
cmp::Reverse,
collections::VecDeque,
convert::TryFrom,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -108,29 +109,19 @@ struct Waitlist {
}

impl Waitlist {
fn push(&mut self, w: Waker, queue_id: QueueId) {
self.queue.push(
QueuedWaker {
queue_id,
waker: Some(w),
},
queue_id,
);
fn push(&mut self, waker: Waker, queue_id: QueueId) {
self.queue.push(QueuedWaker { queue_id, waker }, queue_id);
}

fn pop(&mut self) -> Option<Waker> {
match self.queue.pop() {
Some((qw, _)) => Some(qw.waker.unwrap()),
Some((qw, _)) => Some(qw.waker),
None => None,
}
}

fn remove(&mut self, id: QueueId) {
let tmp = QueuedWaker {
queue_id: id,
waker: None,
};
self.queue.remove(&tmp);
self.queue.remove(&id);
}

fn peek_id(&mut self) -> Option<QueueId> {
Expand All @@ -154,26 +145,20 @@ impl QueueId {
#[derive(Debug)]
struct QueuedWaker {
queue_id: QueueId,
waker: Option<Waker>,
waker: Waker,
}

impl Eq for QueuedWaker {}

impl PartialEq for QueuedWaker {
fn eq(&self, other: &Self) -> bool {
self.queue_id == other.queue_id
impl Borrow<QueueId> for QueuedWaker {
fn borrow(&self) -> &QueueId {
&self.queue_id
}
}

impl Ord for QueuedWaker {
fn cmp(&self, other: &Self) -> Ordering {
self.queue_id.cmp(&other.queue_id)
}
}

impl PartialOrd for QueuedWaker {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
impl PartialEq for QueuedWaker {
fn eq(&self, other: &Self) -> bool {
self.queue_id == other.queue_id
}
}

Expand Down

0 comments on commit 6cdf461

Please sign in to comment.