use consortium_codec::CodecFor;
use crate::transport::{RecvTransport, SendTransport};
use crate::{Channel, ChannelError, ReceivedMessage, Rx, Tx};
pub struct Transceiver<TyTx, TyRx, TrTx, TrRx, C: CodecFor<TyTx> + CodecFor<TyRx>> {
tx: Channel<Tx, TyTx, TrTx, C>,
rx: Channel<Rx, TyRx, TrRx, C>,
}
impl<TyTx, TyRx, TrTx, TrRx, C: CodecFor<TyTx> + CodecFor<TyRx>>
Transceiver<TyTx, TyRx, TrTx, TrRx, C>
{
pub fn new(tx: Channel<Tx, TyTx, TrTx, C>, rx: Channel<Rx, TyRx, TrRx, C>) -> Self {
Self { tx, rx }
}
pub fn tx(&self) -> &Channel<Tx, TyTx, TrTx, C> {
&self.tx
}
pub fn rx(&self) -> &Channel<Rx, TyRx, TrRx, C> {
&self.rx
}
#[allow(clippy::type_complexity)]
pub fn split(self) -> (Channel<Tx, TyTx, TrTx, C>, Channel<Rx, TyRx, TrRx, C>) {
(self.tx, self.rx)
}
}
impl<TyTx, TyRx, TrTx, TrRx, C> Transceiver<TyTx, TyRx, TrTx, TrRx, C>
where
TrTx: SendTransport,
C: CodecFor<TyTx> + CodecFor<TyRx>,
{
pub async fn send(&mut self, msg: &TyTx) -> Result<(), ChannelError<C, TrTx>> {
self.tx.send(msg).await
}
}
impl<TyTx, TyRx, TrTx, TrRx, C> Transceiver<TyTx, TyRx, TrTx, TrRx, C>
where
TrRx: RecvTransport,
C: CodecFor<TyTx> + CodecFor<TyRx>,
{
pub async fn recv(&mut self) -> Result<ReceivedMessage<'_, TyRx, C>, ChannelError<C, TrRx>> {
self.rx.recv().await
}
}