use std::future::{poll_fn, Future};
use std::task::{Context, Poll};
use crate::{ErrorCode, RecvStream, SendStream};
pub trait Session: Clone + Send + Sync + Unpin {
type SendStream: SendStream;
type RecvStream: RecvStream;
type Error: ErrorCode;
fn poll_accept_uni(&self, cx: &mut Context<'_>) -> Poll<Result<Self::RecvStream, Self::Error>>;
#[allow(clippy::type_complexity)]
fn poll_accept_bi(
&self,
cx: &mut Context<'_>,
) -> Poll<Result<(Self::SendStream, Self::RecvStream), Self::Error>>;
#[allow(clippy::type_complexity)]
fn poll_open_bi(
&self,
cx: &mut Context<'_>,
) -> Poll<Result<(Self::SendStream, Self::RecvStream), Self::Error>>;
fn poll_open_uni(&self, cx: &mut Context<'_>) -> Poll<Result<Self::SendStream, Self::Error>>;
fn close(&self, code: u32, reason: &[u8]);
fn poll_closed(&self, cx: &mut Context<'_>) -> Poll<Self::Error>;
fn poll_recv_datagram(&self, cx: &mut Context<'_>) -> Poll<Result<bytes::Bytes, Self::Error>>;
fn send_datagram(&self, payload: bytes::Bytes) -> Result<(), Self::Error>;
fn accept_uni(&self) -> impl Future<Output = Result<Self::RecvStream, Self::Error>> + Send {
poll_fn(|cx| self.poll_accept_uni(cx))
}
fn accept_bi(
&self,
) -> impl Future<Output = Result<(Self::SendStream, Self::RecvStream), Self::Error>> + Send
{
poll_fn(|cx| self.poll_accept_bi(cx))
}
fn open_bi(
&self,
) -> impl Future<Output = Result<(Self::SendStream, Self::RecvStream), Self::Error>> + Send
{
poll_fn(|cx| self.poll_open_bi(cx))
}
fn open_uni(&self) -> impl Future<Output = Result<Self::SendStream, Self::Error>> + Send {
poll_fn(|cx| self.poll_open_uni(cx))
}
fn closed(&self) -> impl Future<Output = Self::Error> + Send {
poll_fn(|cx| self.poll_closed(cx))
}
fn recv_datagram(&self) -> impl Future<Output = Result<bytes::Bytes, Self::Error>> + Send {
poll_fn(|cx| self.poll_recv_datagram(cx))
}
}