use std::sync::atomic;
use crate::*;
#[derive(Debug)]
pub struct ReceiveGuard<'a, M, P>
where
M: Send,
P: PartialOrd + Ord,
{
msg: Message<M, P>,
pq: &'a PriorityQueue<M, P>,
}
impl<'a, M, P> ReceiveGuard<'a, M, P>
where
M: Send,
P: PartialOrd + Ord,
{
pub(crate) fn new(msg: Message<M, P>, pq: &'a PriorityQueue<M, P>) -> Self {
ReceiveGuard { msg, pq }
}
pub fn message(&self) -> &Message<M, P> {
&self.msg
}
pub fn into_message(mut self) -> Message<M, P> {
std::mem::take(&mut self.msg)
}
}
impl<M, P> Drop for ReceiveGuard<'_, M, P>
where
M: Send,
P: PartialOrd + Ord,
{
fn drop(&mut self) {
if self.pq.in_progress.fetch_sub(1, atomic::Ordering::SeqCst) == 1 {
self.pq.send_drained()
}
}
}