use std::{collections::VecDeque, sync::Arc};
#[cfg(feature = "blocking")]
pub(crate) struct PicoQ<U> {
buf: VecDeque<Arc<U>>,
cap: usize,
backpressure: bool,
}
#[cfg(feature = "tokio")]
pub(crate) struct PicoQ<U>
where
U: Sync + Send,
{
buf: VecDeque<Arc<U>>,
cap: usize,
backpressure: bool,
}
#[cfg(feature = "blocking")]
impl<U> PicoQ<U> {
pub fn new(cap: usize) -> Self {
Self {
buf: VecDeque::new(),
cap,
backpressure: cap.gt(&0),
}
}
pub fn is_full(&self) -> bool {
if !self.backpressure {
return false;
}
self.buf.len() == self.cap
}
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
pub fn push(&mut self, msg: Arc<U>) {
self.buf.push_back(msg);
}
pub fn pop(&mut self) -> Option<Arc<U>> {
self.buf.pop_front()
}
}
#[cfg(feature = "tokio")]
impl<U> PicoQ<U>
where
U: Sync + Send,
{
pub fn new(cap: usize) -> Self {
Self {
buf: VecDeque::new(),
cap,
backpressure: cap.gt(&0),
}
}
pub fn is_full(&self) -> bool {
if !self.backpressure {
return false;
}
self.buf.len() == self.cap
}
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
pub fn push(&mut self, msg: Arc<U>) {
self.buf.push_back(msg);
}
pub fn pop(&mut self) -> Option<Arc<U>> {
self.buf.pop_front()
}
}