#![allow(clippy::all)]
#![allow(clippy::all)]
use std::{
ops::{Index, IndexMut},
pin::Pin,
sync::{Arc, Mutex},
task::{Context, Poll, Waker},
};
use futures::FutureExt;
use thiserror::Error;
pub mod cid;
pub mod error;
pub mod flow;
pub mod frame;
pub mod handshake;
pub mod metric;
pub mod net;
pub mod packet;
pub mod param;
pub mod role;
pub mod sid;
pub mod time;
pub mod token;
pub mod util;
pub mod varint;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Epoch {
Initial = 0,
Handshake = 1,
Data = 2,
}
pub trait GetEpoch {
fn epoch(&self) -> Epoch;
}
impl Epoch {
pub const EPOCHS: [Epoch; 3] = [Epoch::Initial, Epoch::Handshake, Epoch::Data];
pub fn iter() -> std::slice::Iter<'static, Epoch> {
Self::EPOCHS.iter()
}
pub const fn count() -> usize {
Self::EPOCHS.len()
}
}
impl<T> Index<Epoch> for [T]
where
T: Sized,
{
type Output = T;
fn index(&self, index: Epoch) -> &Self::Output {
self.index(index as usize)
}
}
impl<T> IndexMut<Epoch> for [T]
where
T: Sized,
{
fn index_mut(&mut self, index: Epoch) -> &mut Self::Output {
self.index_mut(index as usize)
}
}
#[derive(Debug, Default)]
pub enum Receiving<F> {
#[default]
Pending,
Waiting(Waker),
Rcvd(F),
Read,
Reset,
}
impl<F> Receiving<F> {
fn recv_frame(&mut self, frame: F) {
match std::mem::take(self) {
Self::Pending => {
*self = Self::Rcvd(frame);
}
Self::Waiting(waker) => {
waker.wake();
*self = Self::Rcvd(frame);
}
_ => (),
}
}
fn reset(&mut self) {
if let Self::Waiting(waker) = std::mem::replace(self, Self::Reset) {
waker.wake();
}
}
}
#[derive(Debug, Error)]
#[error("Reset")]
pub struct ResetError;
#[derive(Debug, Default, Clone)]
pub struct ArcReceiving<F>(Arc<Mutex<Receiving<F>>>);
impl<F> ArcReceiving<F> {
pub fn reset(&self) {
self.0.lock().unwrap().reset();
}
}
impl<F: Unpin> Future for ArcReceiving<F> {
type Output = Result<Option<F>, ResetError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.0.lock().unwrap().poll_unpin(cx)
}
}
#[cfg(test)]
mod tests {}