use super::{
Rings,
message::Delivery,
poll::{PollUpstream, Ticked, Ticker},
};
mod spec;
pub use spec::ChannelOptions;
#[derive(Debug)]
pub enum Wake<K, T> {
Delivered(Delivery<T, K>),
Sweep,
Tick(Ticked),
}
#[derive(Debug)]
pub struct Outbox<K, T> {
tx: flume::Sender<Wake<K, T>>,
}
impl<K, T> Outbox<K, T> {
#[inline]
pub fn deliver(&self, delivery: Delivery<T, K>) -> Result<(), Delivery<T, K>> {
self
.tx
.send(Wake::Delivered(delivery))
.map_err(|e| match e.0 {
Wake::Delivered(delivery) => delivery,
Wake::Sweep | Wake::Tick(_) => {
unreachable!("`deliver` sends `Delivered` and flume returns what it took")
}
})
}
#[inline]
pub async fn deliver_async(&self, delivery: Delivery<T, K>) -> Result<(), Delivery<T, K>> {
self
.tx
.send_async(Wake::Delivered(delivery))
.await
.map_err(|e| match e.0 {
Wake::Delivered(delivery) => delivery,
Wake::Sweep | Wake::Tick(_) => unreachable!("`deliver_async` sends `Delivered`"),
})
}
#[inline]
pub fn try_deliver(&self, delivery: Delivery<T, K>) -> Result<(), Delivery<T, K>> {
self
.tx
.try_send(Wake::Delivered(delivery))
.map_err(|error| match error.into_inner() {
Wake::Delivered(delivery) => delivery,
Wake::Sweep | Wake::Tick(_) => {
unreachable!("`try_deliver` sends `Delivered` and flume returns what it took")
}
})
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.tx.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.tx.is_empty()
}
#[inline]
#[must_use]
pub fn capacity(&self) -> Option<usize> {
self.tx.capacity()
}
#[inline]
#[must_use]
pub fn is_disconnected(&self) -> bool {
self.tx.is_disconnected()
}
#[inline]
pub(crate) fn into_sink(self) -> flume::r#async::SendSink<'static, Wake<K, T>> {
self.tx.into_sink()
}
#[inline]
#[must_use]
pub fn nudge(&self) -> Nudge<K, T> {
Nudge {
tx: self.tx.clone(),
}
}
#[inline]
#[must_use]
pub fn ticker(&self, upstream: PollUpstream) -> Ticker<K, T> {
Ticker::new(upstream, self.tx.clone())
}
}
impl<K, T> Clone for Outbox<K, T> {
#[inline]
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
}
}
}
#[derive(Debug)]
pub struct Nudge<K, T> {
tx: flume::Sender<Wake<K, T>>,
}
impl<K, T> Clone for Nudge<K, T> {
#[inline]
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
}
}
}
impl<K, T> Rings for Nudge<K, T>
where
K: Send,
T: Send,
{
#[inline]
fn ring(&self) -> bool {
match self.tx.try_send(Wake::Sweep) {
Ok(()) | Err(flume::TrySendError::Full(_)) => true,
Err(flume::TrySendError::Disconnected(_)) => false,
}
}
}
#[derive(Debug)]
pub struct Inbox<K, T> {
rx: flume::Receiver<Wake<K, T>>,
}
impl<K, T> Inbox<K, T> {
#[inline]
pub fn park(&self, sweep: core::time::Duration) -> Option<Wake<K, T>> {
match self.rx.recv_timeout(sweep) {
Ok(wake) => Some(wake),
Err(flume::RecvTimeoutError::Timeout) => Some(Wake::Sweep),
Err(flume::RecvTimeoutError::Disconnected) => None,
}
}
#[inline]
pub async fn park_async(&self) -> Option<Wake<K, T>> {
self.rx.recv_async().await.ok()
}
#[inline]
pub fn drain(&self) -> impl Iterator<Item = Wake<K, T>> {
self.rx.drain()
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.rx.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.rx.is_empty()
}
#[inline]
#[must_use]
pub fn is_disconnected(&self) -> bool {
self.rx.is_disconnected()
}
#[inline]
#[must_use]
pub fn evictor(&self) -> Evictor<K, T> {
Evictor {
rx: self.rx.clone(),
}
}
}
#[derive(Debug)]
pub struct Evictor<K, T> {
rx: flume::Receiver<Wake<K, T>>,
}
impl<K, T> Evictor<K, T> {
#[inline]
#[must_use]
pub fn evict(&self) -> Option<Delivery<T, K>> {
while let Ok(wake) = self.rx.try_recv() {
if let Wake::Delivered(delivery) = wake {
return Some(delivery);
}
}
None
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.rx.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.rx.is_empty()
}
}
impl<K, T> Clone for Evictor<K, T> {
#[inline]
fn clone(&self) -> Self {
Self {
rx: self.rx.clone(),
}
}
}
#[inline]
#[must_use]
pub fn channel<K, T>(capacity: usize) -> (Outbox<K, T>, Inbox<K, T>) {
let (tx, rx) = flume::bounded(capacity);
(Outbox { tx }, Inbox { rx })
}
#[inline]
#[must_use]
pub fn open<K, T>(spec: ChannelOptions) -> (Outbox<K, T>, Inbox<K, T>) {
match spec {
ChannelOptions::Bounded(capacity) => channel(capacity),
ChannelOptions::Unbounded => {
let (tx, rx) = flume::unbounded();
(Outbox { tx }, Inbox { rx })
}
}
}
#[cfg(test)]
mod tests;