#![allow(async_fn_in_trait)]
use std::future::Future;
use crate::Backing;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransportMode {
Bare,
Stable,
}
#[cfg(not(target_arch = "wasm32"))]
pub trait MaybeSend: Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Send> MaybeSend for T {}
#[cfg(target_arch = "wasm32")]
pub trait MaybeSend {}
#[cfg(target_arch = "wasm32")]
impl<T> MaybeSend for T {}
#[cfg(not(target_arch = "wasm32"))]
pub trait MaybeSync: Sync {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Sync> MaybeSync for T {}
#[cfg(target_arch = "wasm32")]
pub trait MaybeSync {}
#[cfg(target_arch = "wasm32")]
impl<T> MaybeSync for T {}
#[cfg(not(target_arch = "wasm32"))]
pub trait MaybeSendFuture: Future + Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Future + Send> MaybeSendFuture for T {}
#[cfg(target_arch = "wasm32")]
pub trait MaybeSendFuture: Future {}
#[cfg(target_arch = "wasm32")]
impl<T: Future> MaybeSendFuture for T {}
pub trait Link {
type Tx: LinkTx;
type Rx: LinkRx;
fn split(self) -> (Self::Tx, Self::Rx);
fn supports_transport_mode(mode: TransportMode) -> bool
where
Self: Sized,
{
let _ = mode;
true
}
}
pub trait LinkTx: MaybeSend + MaybeSync + 'static {
fn send(&self, bytes: Vec<u8>) -> impl Future<Output = std::io::Result<()>> + MaybeSend + '_;
fn close(self) -> impl Future<Output = std::io::Result<()>> + MaybeSend
where
Self: Sized;
}
pub trait LinkRx: MaybeSend + 'static {
type Error: std::error::Error + MaybeSend + MaybeSync + 'static;
fn recv(
&mut self,
) -> impl Future<Output = Result<Option<Backing>, Self::Error>> + MaybeSend + '_;
}
pub struct SplitLink<Tx, Rx> {
pub tx: Tx,
pub rx: Rx,
}
impl<Tx: LinkTx, Rx: LinkRx> Link for SplitLink<Tx, Rx> {
type Tx = Tx;
type Rx = Rx;
fn split(self) -> (Tx, Rx) {
(self.tx, self.rx)
}
}