use crate::flavor::{
flavor_dispatch, flavor_select_dispatch, queue_dispatch, Flavor, FlavorImpl, FlavorMC,
FlavorMP, Queue,
};
use crate::shared::*;
pub use crate::{AsyncRxTrait, AsyncTxTrait, BlockingRxTrait, BlockingTxTrait};
use std::mem::MaybeUninit;
#[allow(clippy::large_enum_variant)]
pub enum CompatFlavor<T> {
Array(crate::flavor::Array<T>),
List(crate::flavor::List<T>),
}
macro_rules! wrap_compat {
($self: expr, $method:ident $($arg:expr)*)=>{
match $self {
Self::Array(inner) => inner.$method($($arg)*),
Self::List(inner) => inner.$method($($arg)*),
}
};
}
impl<T> Queue for CompatFlavor<T> {
type Item = T;
queue_dispatch!(wrap_compat);
}
impl<T> FlavorImpl for CompatFlavor<T> {
flavor_dispatch!(wrap_compat);
}
impl<T> FlavorSelect for CompatFlavor<T> {
flavor_select_dispatch!(wrap_compat);
}
impl<T> FlavorMP for CompatFlavor<T> {}
impl<T> FlavorMC for CompatFlavor<T> {}
impl<T: Send + 'static> Flavor for CompatFlavor<T> {
type Send = RegistryMultiSend<T>;
type Recv = RegistryMultiRecv;
}
#[inline(always)]
fn new_list<T: Send + Unpin + 'static>() -> CompatFlavor<T> {
CompatFlavor::<T>::List(crate::flavor::List::new())
}
#[inline(always)]
fn new_array<T: Send + Unpin + 'static>(mut size: usize) -> CompatFlavor<T> {
if size <= 1 {
size = 1;
}
CompatFlavor::<T>::Array(crate::flavor::Array::<T>::new(size))
}
pub type Tx<T> = crate::Tx<CompatFlavor<T>>;
pub type MTx<T> = crate::MTx<CompatFlavor<T>>;
pub type Rx<T> = crate::Rx<CompatFlavor<T>>;
pub type MRx<T> = crate::MRx<CompatFlavor<T>>;
pub type AsyncTx<T> = crate::AsyncTx<CompatFlavor<T>>;
pub type MAsyncTx<T> = crate::MAsyncTx<CompatFlavor<T>>;
pub type AsyncRx<T> = crate::AsyncRx<CompatFlavor<T>>;
pub type MAsyncRx<T> = crate::MAsyncRx<CompatFlavor<T>>;
pub use crate::{
RecvError, RecvTimeoutError, SendError, SendTimeoutError, TryRecvError, TrySendError,
};
pub mod sink {
use super::*;
pub type AsyncSink<T> = crate::sink::AsyncSink<CompatFlavor<T>>;
}
pub mod stream {
use super::*;
pub type AsyncStream<T> = crate::stream::AsyncStream<CompatFlavor<T>>;
}
pub mod spsc {
use super::*;
macro_rules! init_share {
($flavor: expr) => {{
ChannelShared::new($flavor, RegistryMultiSend::new(), RegistryMultiRecv::new())
}};
}
pub fn unbounded_blocking<T: Unpin + Send + 'static>() -> (Tx<T>, Rx<T>) {
let shared = init_share!(new_list::<T>());
let tx = Tx::new(shared.clone());
let rx = Rx::new(shared);
(tx, rx)
}
pub fn unbounded_async<T: Unpin + Send + 'static>() -> (Tx<T>, AsyncRx<T>) {
let shared = init_share!(new_list::<T>());
let tx = Tx::new(shared.clone());
let rx = AsyncRx::new(shared);
(tx, rx)
}
pub fn bounded_blocking<T: Unpin + Send + 'static>(size: usize) -> (Tx<T>, Rx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = Tx::new(shared.clone());
let rx = Rx::new(shared);
(tx, rx)
}
pub fn bounded_async<T: Unpin + Send + 'static>(size: usize) -> (AsyncTx<T>, AsyncRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = AsyncTx::new(shared.clone());
let rx = AsyncRx::new(shared);
(tx, rx)
}
pub fn bounded_tx_async_rx_blocking<T: Unpin + Send + 'static>(
size: usize,
) -> (AsyncTx<T>, Rx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = AsyncTx::new(shared.clone());
let rx = Rx::new(shared);
(tx, rx)
}
pub fn bounded_tx_blocking_rx_async<T: Unpin + Send + 'static>(
size: usize,
) -> (Tx<T>, AsyncRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = Tx::new(shared.clone());
let rx = AsyncRx::new(shared);
(tx, rx)
}
}
pub mod mpsc {
use super::*;
macro_rules! init_share {
($flavor: expr) => {{
ChannelShared::new($flavor, RegistryMultiSend::new(), RegistryMultiRecv::new())
}};
}
pub fn unbounded_blocking<T: Send + 'static + Unpin>() -> (MTx<T>, Rx<T>) {
let shared = init_share!(new_list::<T>());
let tx = MTx::new(shared.clone());
let rx = Rx::new(shared);
(tx, rx)
}
pub fn unbounded_async<T: Send + 'static + Unpin>() -> (MTx<T>, AsyncRx<T>) {
let shared = init_share!(new_list::<T>());
let tx = MTx::new(shared.clone());
let rx = AsyncRx::new(shared);
(tx, rx)
}
pub fn bounded_blocking<T: Send + 'static + Unpin>(size: usize) -> (MTx<T>, Rx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MTx::new(shared.clone());
let rx = Rx::new(shared);
(tx, rx)
}
pub fn bounded_async<T: Send + 'static + Unpin>(size: usize) -> (MAsyncTx<T>, AsyncRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MAsyncTx::new(shared.clone());
let rx = AsyncRx::new(shared);
(tx, rx)
}
pub fn bounded_tx_async_rx_blocking<T: Send + 'static + Unpin>(
size: usize,
) -> (MAsyncTx<T>, Rx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MAsyncTx::new(shared.clone());
let rx = Rx::new(shared);
(tx, rx)
}
pub fn bounded_tx_blocking_rx_async<T: Send + 'static + Unpin>(
size: usize,
) -> (MTx<T>, AsyncRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MTx::new(shared.clone());
let rx = AsyncRx::new(shared);
(tx, rx)
}
}
pub mod mpmc {
use super::*;
macro_rules! init_share {
($flavor: expr) => {{
ChannelShared::new($flavor, RegistryMultiSend::new(), RegistryMultiRecv::new())
}};
}
pub fn unbounded_blocking<T: Send + 'static + Unpin>() -> (MTx<T>, MRx<T>) {
let shared = init_share!(new_list::<T>());
let tx = MTx::new(shared.clone());
let rx = MRx::new(shared);
(tx, rx)
}
pub fn unbounded_async<T: Send + 'static + Unpin>() -> (MTx<T>, MAsyncRx<T>) {
let shared = init_share!(new_list::<T>());
let tx = MTx::new(shared.clone());
let rx = MAsyncRx::new(shared);
(tx, rx)
}
pub fn bounded_blocking<T: Send + 'static + Unpin>(size: usize) -> (MTx<T>, MRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MTx::new(shared.clone());
let rx = MRx::new(shared);
(tx, rx)
}
pub fn bounded_async<T: Send + 'static + Unpin>(size: usize) -> (MAsyncTx<T>, MAsyncRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MAsyncTx::new(shared.clone());
let rx = MAsyncRx::new(shared);
(tx, rx)
}
pub fn bounded_tx_async_rx_blocking<T: Send + 'static + Unpin>(
size: usize,
) -> (MAsyncTx<T>, MRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MAsyncTx::new(shared.clone());
let rx = MRx::new(shared);
(tx, rx)
}
pub fn bounded_tx_blocking_rx_async<T: Send + 'static + Unpin>(
size: usize,
) -> (MTx<T>, MAsyncRx<T>) {
let shared = init_share!(new_array::<T>(size));
let tx = MTx::new(shared.clone());
let rx = MAsyncRx::new(shared);
(tx, rx)
}
}