You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

58 lines
1.1 KiB

use async_channel::{
unbounded as unbounded_async, Receiver as AsyncReceiver, RecvError, SendError,
Sender as AsyncSender,
};
use crossbeam::channel::{unbounded, Receiver, Sender};
pub struct Channel<T> {
pub tx: Sender<T>,
pub rx: Receiver<T>,
}
impl<T> Channel<T> {
pub fn new() -> Self {
let (tx, rx) = unbounded::<T>();
Self { tx, rx }
}
pub fn send(&self, msg: T) {
self.tx.send(msg).unwrap()
}
pub fn recv(&self) -> T {
self.rx.recv().unwrap()
}
}
impl<T> Default for Channel<T> {
fn default() -> Self {
Channel::new()
}
}
#[derive(Clone)]
pub struct AsyncChannel<T> {
pub tx: AsyncSender<T>,
pub rx: AsyncReceiver<T>,
}
impl<T> AsyncChannel<T> {
pub fn new() -> Self {
let (tx, rx) = unbounded_async::<T>();
Self { tx, rx }
}
pub async fn send(&self, msg: T) -> Result<(), SendError<T>> {
self.tx.send(msg).await
}
pub async fn recv(&self) -> Result<T, RecvError> {
self.rx.recv().await
}
}
impl<T> Default for AsyncChannel<T> {
fn default() -> Self {
AsyncChannel::new()
}
}