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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Shared asynchronous byte-stream codec drivers.
use Error;
use ErrorKind;
use Result;
use Pin;
use Codec;
use Leb128DecodeError;
use AsyncInput;
use AsyncOutput;
use PinnedAsyncInputExt;
use WriteFullyFuture;
use try_reserve_vec;
use decode_leb128_unchecked;
use invalid_utf8_error;
use length_exceeded_error;
use map_leb128_decode_error;
/// Reads exactly enough bytes to fill `output`.
///
/// # Type Parameters
///
/// - `I`: Runtime-neutral asynchronous byte input.
///
/// # Parameters
///
/// - `input`: Source from which bytes are read.
/// - `output`: Destination slice that must be filled.
///
/// # Returns
///
/// Returns after every destination byte has been initialized.
///
/// # Errors
///
/// Returns an input error, including an unexpected-end-of-input error when the
/// source ends before `output` is full.
///
/// # Cancellation safety
///
/// This operation is not cancellation safe. Dropping it retains bytes already
/// consumed from `input` and modifications already made to `output`.
pub async
/// Writes every byte in `input`.
///
/// # Type Parameters
///
/// - `O`: Runtime-neutral asynchronous byte output.
///
/// # Parameters
///
/// - `output`: Destination to which bytes are written.
/// - `input`: Source bytes that must be written.
///
/// # Returns
///
/// Returns after every source byte has been written.
///
/// # Errors
///
/// Returns an output error, including a write-zero error when the output stops
/// making progress.
///
/// # Cancellation safety
///
/// This operation is not cancellation safe. Dropping it leaves any
/// already-written prefix in `output`.
pub async
/// Reads and decodes one LEB128-family payload.
///
/// # Type Parameters
///
/// - `N`: Maximum encoded payload length in bytes.
/// - `C`: LEB128-family codec used to decode the payload.
/// - `R`: Runtime-neutral asynchronous byte input.
///
/// # Parameters
///
/// - `reader`: Source from which the encoded payload is read.
///
/// # Returns
///
/// Returns the decoded codec value.
///
/// # Errors
///
/// Returns an input error or an invalid-data error when the payload is
/// malformed or incomplete at its maximum width.
///
/// # Cancellation safety
///
/// This operation is not cancellation safe. Dropping it retains any bytes
/// already consumed from `reader`.
pub async
/// Reads and validates an already length-delimited UTF-8 payload.
///
/// # Type Parameters
///
/// - `R`: Runtime-neutral asynchronous byte input.
///
/// # Parameters
///
/// - `reader`: Source from which payload bytes are read.
/// - `len`: Encoded payload length in bytes.
/// - `max_len`: Maximum accepted payload length in bytes.
///
/// # Returns
///
/// Returns the decoded UTF-8 string.
///
/// # Errors
///
/// Returns an input or allocation error, or an invalid-data error when `len`
/// exceeds `max_len` or the payload is not valid UTF-8.
///
/// # Cancellation safety
///
/// This operation is not cancellation safe. Dropping it retains bytes already
/// consumed from `reader`.
pub async
/// Reads and validates a UTF-8 payload into reusable caller-owned storage.
///
/// # Type Parameters
///
/// - `R`: Runtime-neutral asynchronous byte input.
///
/// # Parameters
///
/// - `reader`: Source from which payload bytes are read.
/// - `bytes`: Reusable destination buffer. It is cleared before a permitted
/// read and contains the received bytes when the read succeeds or when UTF-8
/// validation fails.
/// - `len`: Encoded payload length in bytes.
/// - `max_len`: Maximum accepted payload length in bytes.
///
/// # Returns
///
/// Returns after `bytes` contains one valid UTF-8 payload.
///
/// # Errors
///
/// Returns an input or allocation error, or an invalid-data error when `len`
/// exceeds `max_len` or the payload is not valid UTF-8.
///
/// # Cancellation safety
///
/// This operation is not cancellation safe. Dropping it retains bytes already
/// consumed from `reader` and modifications already made to `bytes`.
pub async
/// Reads a bounded payload into a caller-owned byte buffer.
///
/// The helper performs length validation, fallible capacity reservation, and
/// exact input transfer but leaves UTF-8 validation to its caller.
///
/// # Type Parameters
///
/// - `R`: Runtime-neutral asynchronous byte input.
///
/// # Parameters
///
/// - `reader`: Source from which exactly `len` bytes are read after validation.
/// - `bytes`: Destination buffer, cleared before the permitted read.
/// - `len`: Payload length in bytes; values above `max_len` are rejected.
/// - `max_len`: Maximum payload length accepted by this helper.
async