use std::time::Duration;
use crate::error::{
PopError, PopTimeoutError, PushError, PushTimeoutError, TryPopError, TryPushError,
};
pub trait BoundedQueue<T: Send>: Send + Sync + sealed::Sealed {
fn push(&self, item: T) -> Result<(), PushError<T>>;
fn pop(&self) -> Result<T, PopError>;
fn try_push(&self, item: T) -> Result<(), TryPushError<T>>;
fn try_pop(&self) -> Result<T, TryPopError>;
fn push_timeout(&self, item: T, timeout: Duration) -> Result<(), PushTimeoutError<T>>;
fn pop_timeout(&self, timeout: Duration) -> Result<T, PopTimeoutError>;
fn close(&self) -> bool;
fn is_closed(&self) -> bool;
fn capacity(&self) -> usize;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn is_full(&self) -> bool {
self.len() >= self.capacity()
}
}
pub(crate) mod sealed {
pub trait Sealed {}
}
macro_rules! forward_bounded_queue {
($ty:ident) => {
impl<T: Send> $crate::traits::sealed::Sealed for $ty<T> {}
impl<T: Send> $crate::BoundedQueue<T> for $ty<T> {
fn push(&self, item: T) -> Result<(), $crate::PushError<T>> {
$ty::push(self, item)
}
fn pop(&self) -> Result<T, $crate::PopError> {
$ty::pop(self)
}
fn try_push(&self, item: T) -> Result<(), $crate::TryPushError<T>> {
$ty::try_push(self, item)
}
fn try_pop(&self) -> Result<T, $crate::TryPopError> {
$ty::try_pop(self)
}
fn push_timeout(
&self,
item: T,
timeout: ::std::time::Duration,
) -> Result<(), $crate::PushTimeoutError<T>> {
$ty::push_timeout(self, item, timeout)
}
fn pop_timeout(
&self,
timeout: ::std::time::Duration,
) -> Result<T, $crate::PopTimeoutError> {
$ty::pop_timeout(self, timeout)
}
fn close(&self) -> bool {
$ty::close(self)
}
fn is_closed(&self) -> bool {
$ty::is_closed(self)
}
fn capacity(&self) -> usize {
$ty::capacity(self)
}
fn len(&self) -> usize {
$ty::len(self)
}
fn is_empty(&self) -> bool {
$ty::is_empty(self)
}
fn is_full(&self) -> bool {
$ty::is_full(self)
}
}
};
}
pub(crate) use forward_bounded_queue;