#![allow(async_fn_in_trait)]
use std::future::Future;
use facet::Facet;
use facet_core::Shape;
use crate::{MaybeSend, SelfRef};
pub trait MsgFamily: 'static {
type Msg<'a>: Facet<'a> + 'a;
fn shape() -> &'static Shape {
<Self::Msg<'static> as Facet<'static>>::SHAPE
}
}
pub trait Conduit {
type Msg: MsgFamily;
type Tx: ConduitTx<Msg = Self::Msg>;
type Rx: ConduitRx<Msg = Self::Msg>;
fn split(self) -> (Self::Tx, Self::Rx);
}
pub trait ConduitTx {
type Msg: MsgFamily;
type Prepared: MaybeSend + 'static;
type Error: std::error::Error + MaybeSend + 'static;
fn prepare_send(
&self,
item: <Self::Msg as MsgFamily>::Msg<'_>,
) -> Result<Self::Prepared, Self::Error>;
fn send_prepared(
&self,
prepared: Self::Prepared,
) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend + '_;
async fn close(self) -> std::io::Result<()>
where
Self: Sized;
}
pub type RecvResult<M, E> = Result<Option<SelfRef<<M as MsgFamily>::Msg<'static>>>, E>;
pub trait ConduitRx {
type Msg: MsgFamily;
type Error: std::error::Error + MaybeSend + 'static;
fn recv(&mut self)
-> impl Future<Output = RecvResult<Self::Msg, Self::Error>> + MaybeSend + '_;
}
pub trait ConduitAcceptor {
type Conduit: Conduit;
async fn accept(&mut self) -> std::io::Result<Self::Conduit>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionRole {
Initiator,
Acceptor,
}