1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Internal states for reading and writing frames.
/// Internal state for reading frames.
#[derive(Debug)]
pub struct ReadState<'buf> {
/// The current index in the buffer.
///
/// Represents the number of bytes read into the buffer.
pub index: usize,
/// EOF was reached while decoding.
pub eof: bool,
/// The buffer is currently framable.
pub is_framable: bool,
/// The buffer must be shifted before reading more bytes.
///
/// Makes room for more bytes to be read into the buffer, keeping the already read bytes.
pub shift: bool,
/// Total number of bytes decoded in a framing round.
pub total_consumed: usize,
/// The underlying buffer to read into.
pub buffer: &'buf mut [u8],
}
impl<'buf> ReadState<'buf> {
/// Creates a new [`ReadState`].
#[inline]
pub const fn new(buffer: &'buf mut [u8]) -> Self {
Self {
index: 0,
eof: false,
is_framable: false,
shift: false,
total_consumed: 0,
buffer,
}
}
/// Resets the state to its initial values.
#[inline]
pub const fn reset(self) -> Self {
Self::new(self.buffer)
}
/// Creates an empty [`ReadState`].
#[inline]
pub const fn empty() -> Self {
Self::new(&mut [])
}
/// Returns the number of bytes that can be framed.
#[inline]
pub const fn framable(&self) -> usize {
self.index - self.total_consumed
}
}
/// Internal state for writing frames.
#[derive(Debug)]
pub struct WriteState<'buf> {
/// The underlying buffer to write to.
pub buffer: &'buf mut [u8],
}
impl<'buf> WriteState<'buf> {
/// Creates a new [`WriteState`].
#[inline]
pub const fn new(buffer: &'buf mut [u8]) -> Self {
Self { buffer }
}
/// Resets the state to its initial values.
#[inline]
pub const fn reset(self) -> Self {
Self::new(self.buffer)
}
/// Creates an empty [`WriteState`].
#[inline]
pub const fn empty() -> Self {
Self::new(&mut [])
}
}
/// Internal state for reading and writing frames.
#[derive(Debug)]
pub struct ReadWriteState<'buf> {
/// Internal read state.
pub read: ReadState<'buf>,
/// Internal write state.
pub write: WriteState<'buf>,
}
impl<'buf> ReadWriteState<'buf> {
/// Creates a new [`ReadWriteState`] with the given [`ReadState`] and [`WriteState`].
#[inline]
pub const fn new(read: ReadState<'buf>, write: WriteState<'buf>) -> Self {
Self { read, write }
}
/// Creates a new [`ReadWriteState`] with empty [`ReadState`] and [`WriteState`].
#[inline]
pub const fn reset(self) -> Self {
Self::new(self.read.reset(), self.write.reset())
}
}