clankerdiff_protocol/shared/
transport.rs1use async_channel::{Receiver, Sender};
2use thiserror::Error;
3
4pub struct LocalEnd<Out, In> {
5 tx: Sender<Out>,
6 rx: Receiver<In>,
7}
8
9#[derive(Debug, Error)]
10#[error("local transport closed")]
11pub struct TransportClosed;
12
13impl<Out, In> LocalEnd<Out, In> {
14 pub(crate) fn new(tx: Sender<Out>, rx: Receiver<In>) -> Self {
15 Self { tx, rx }
16 }
17
18 pub async fn send(&self, message: Out) -> Result<(), TransportClosed> {
19 self.tx.send(message).await.map_err(|_| TransportClosed)
20 }
21
22 pub async fn recv(&self) -> Result<In, TransportClosed> {
23 self.rx.recv().await.map_err(|_| TransportClosed)
24 }
25
26 pub fn close(&self) {
27 self.tx.close();
28 self.rx.close();
29 }
30}