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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use core::{cmp::min, pin::Pin, task::{Context, Poll}};
#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::vec::Vec;
#[allow(unused)]
use crate::{backend, io};
use io::{AsyncWrite as _, AsyncRead as _, AsyncBufRead as _};
/// Retains the current index state into a serialization buffer.
///
/// Typically used by a [BufferState](super::BufferState)
/// to keep track of which bytes still require processing.
pub struct BufferCursor {
offset: usize,
len: usize,
}
impl BufferCursor {
/// Instantiate a new cursor over the beginning of the provided `buf`.
#[inline(always)]
pub fn new(buf: &[u8]) -> Self {
Self::with_len(buf.len())
}
/// Instantiate a new cursor representing the beginning of a buffer
/// with a specified `len`.
#[inline(always)]
pub fn with_len(len: usize) -> Self {
Self {
offset: 0,
len,
}
}
/// Return the number of bytes the cursor has processed so far.
#[inline(always)]
pub fn len(&self) -> usize {
self.len
}
/// Return whether or not any bytes have been processed yet.
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Return whether or not more data is expected to be processed.
#[inline(always)]
pub fn is_pending(&self) -> bool {
self.offset < self.len
}
pub(crate) fn extend_len(&mut self, n: usize) {
self.len += n;
}
fn is_error(&self) -> bool {
self.len == 0
}
fn mark_as_error(&mut self) {
self.len = 0;
}
fn remaining(&self) -> usize {
self.len - self.offset
}
fn advance(&mut self, n: usize) {
self.offset += min(n, self.remaining());
}
pub fn start_write<W>(&mut self, mut writer: &mut W, data: &[u8], cx: &mut Context<'_>) -> backend::PollEncodeStatus<io::Error>
where
W: io::AsyncWrite + Unpin,
{
debug_assert!(!self.is_error() && self.is_pending() && self.len <= data.len());
while self.is_pending() {
match Pin::new(&mut writer).poll_write(cx, &data[self.offset..self.len]) {
Poll::Ready(r) => match r {
Ok(n) => {
if n == 0 {
self.mark_as_error();
return backend::PollEncodeStatus::Error(io::error::write_zero());
} else {
self.advance(n);
}
},
Err(e) => {
self.mark_as_error();
return backend::PollEncodeStatus::Error(e);
}
}
Poll::Pending => return backend::PollEncodeStatus::Pending,
}
}
backend::PollEncodeStatus::Fini
}
/// Attempt to write all of the bytes in `data`, starting from the current
/// offset of the cursor.
///
/// This implementation differs from a typical [AsyncWrite](io::AsyncWrite) operation in that
/// the contents of data are expected to be unchanging from one call to the
/// next, despite progress being made. The expectation is that the caller
/// does not need to retain any information about the progress of the write,
/// and simply needs to pass in the same references for each call.
pub fn write_remaining<W>(&mut self, writer: &mut W, data: &[u8], cx: &mut Context<'_>) -> backend::PollEncodeStatus<io::Error>
where
W: io::AsyncWrite + Unpin,
{
debug_assert!(!self.is_error() && self.is_pending() && self.len <= data.len());
if self.len > data.len() {
self.mark_as_error();
}
if self.is_error() {
backend::PollEncodeStatus::Error(io::error::invalid_input())
} else {
self.start_write(writer, data, cx)
}
}
pub fn start_read<R>(&mut self, mut reader: &mut R, data: &mut [u8], cx: &mut Context<'_>) -> backend::PollDecodeStatus<(), io::Error>
where
R: io::AsyncRead + Unpin,
{
debug_assert!(!self.is_error() && self.is_pending() && self.len <= data.len());
loop {
match Pin::new(&mut reader).poll_read(cx, &mut data[self.offset..self.len]) {
Poll::Ready(r) => match r {
Ok(n) => {
if n == 0 {
self.mark_as_error();
return backend::PollDecodeStatus::Error(io::error::unexpected_eof());
} else {
self.advance(n);
if !self.is_pending() {
return backend::PollDecodeStatus::Fini(());
}
}
},
Err(e) => {
self.mark_as_error();
return backend::PollDecodeStatus::Error(e);
}
},
Poll::Pending => return backend::PollDecodeStatus::Pending,
}
}
}
/// Attempt to read all remaining bytes that are expected into `data`, starting
/// from the current offset of the cursor.
///
/// This implementation differs from a typical [AsyncRead](io::AsyncRead) operation in that
/// the contents of data are expected to be unchanging from one call to the
/// next, despite progress being made. The expectation is that the caller
/// does not need to retain any information about the progress of the read,
/// and simply needs to pass in the same references for each call.
pub fn read_remaining<R>(&mut self, reader: &mut R, data: &mut [u8], cx: &mut Context<'_>) -> backend::PollDecodeStatus<(), io::Error>
where
R: io::AsyncRead + Unpin,
{
debug_assert!(!self.is_error() && self.is_pending() && self.len <= data.len());
if self.len > data.len() {
self.mark_as_error();
}
if self.is_error() {
backend::PollDecodeStatus::Error(io::error::invalid_input())
} else {
self.start_read(reader, data, cx)
}
}
/// Semantically equivalent to `read_remaining`, only this method takes advantage
/// of the [AsyncBufRead](io::AsyncBufRead) trait to minimize the number of copies required to transfer
/// the bytes into a pre-allocated [Vec].
#[cfg(any(feature = "std", feature = "alloc"))]
pub fn fill_vec<R>(&mut self, mut reader: &mut R, data: &mut Vec<u8>, cx: &mut Context<'_>) -> backend::PollDecodeStatus<(), io::Error>
where
R: io::AsyncBufRead + Unpin,
{
debug_assert!(!self.is_error() && self.is_pending());
if self.is_error() {
return backend::PollDecodeStatus::Error(io::error::invalid_input());
}
while self.is_pending() {
match Pin::new(&mut reader).poll_fill_buf(cx) {
Poll::Ready(r) => match r {
Ok(buf) => {
if buf.is_empty() {
self.mark_as_error();
return backend::PollDecodeStatus::Error(io::error::unexpected_eof());
} else {
let n = min(buf.len(), self.remaining());
data.extend_from_slice(&buf[..n]);
Pin::new(&mut reader).consume(n);
self.advance(n);
}
},
Err(err) => {
self.mark_as_error();
return backend::PollDecodeStatus::Error(err);
}
},
Poll::Pending => return backend::PollDecodeStatus::Pending,
}
}
backend::PollDecodeStatus::Fini(())
}
}