1#![allow(clippy::all)]
2#![allow(clippy::all)]
15use std::{
16 ops::{Index, IndexMut},
17 pin::Pin,
18 sync::{Arc, Mutex},
19 task::{Context, Poll, Waker},
20};
21
22use futures::FutureExt;
23use thiserror::Error;
24
25pub mod cid;
27pub mod error;
29pub mod flow;
31pub mod frame;
33pub mod handshake;
35pub mod metric;
37pub mod net;
39pub mod packet;
41pub mod param;
43pub mod role;
45pub mod sid;
47pub mod time;
49pub mod token;
51pub mod util;
53pub mod varint;
55
56#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
58pub enum Epoch {
59 Initial = 0,
60 Handshake = 1,
61 Data = 2,
62}
63
64pub trait GetEpoch {
65 fn epoch(&self) -> Epoch;
66}
67
68impl Epoch {
69 pub const EPOCHS: [Epoch; 3] = [Epoch::Initial, Epoch::Handshake, Epoch::Data];
70 pub fn iter() -> std::slice::Iter<'static, Epoch> {
74 Self::EPOCHS.iter()
75 }
76
77 pub const fn count() -> usize {
79 Self::EPOCHS.len()
80 }
81}
82
83impl<T> Index<Epoch> for [T]
84where
85 T: Sized,
86{
87 type Output = T;
88
89 fn index(&self, index: Epoch) -> &Self::Output {
90 self.index(index as usize)
91 }
92}
93
94impl<T> IndexMut<Epoch> for [T]
95where
96 T: Sized,
97{
98 fn index_mut(&mut self, index: Epoch) -> &mut Self::Output {
99 self.index_mut(index as usize)
100 }
101}
102
103#[derive(Debug, Default)]
104pub enum Receiving<F> {
105 #[default]
106 Pending,
107 Waiting(Waker),
108 Rcvd(F),
109 Read,
110 Reset,
111}
112
113impl<F> Receiving<F> {
114 fn recv_frame(&mut self, frame: F) {
115 match std::mem::take(self) {
116 Self::Pending => {
117 *self = Self::Rcvd(frame);
118 }
119 Self::Waiting(waker) => {
120 waker.wake();
121 *self = Self::Rcvd(frame);
122 }
123 _ => (),
124 }
125 }
126
127 fn reset(&mut self) {
128 if let Self::Waiting(waker) = std::mem::replace(self, Self::Reset) {
129 waker.wake();
130 }
131 }
132}
133
134#[derive(Debug, Error)]
135#[error("Reset")]
136pub struct ResetError;
137
138#[derive(Debug, Default, Clone)]
139pub struct ArcReceiving<F>(Arc<Mutex<Receiving<F>>>);
140
141impl<F> ArcReceiving<F> {
142 pub fn reset(&self) {
143 self.0.lock().unwrap().reset();
144 }
145}
146
147impl<F: Unpin> Future for ArcReceiving<F> {
148 type Output = Result<Option<F>, ResetError>;
149
150 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
151 self.0.lock().unwrap().poll_unpin(cx)
152 }
153}
154
155#[cfg(test)]
156mod tests {}