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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
use std::{
collections::TryReserveError,
future::poll_fn,
io::{
self,
Error,
ErrorKind,
},
pin::Pin,
task::{
Context,
Poll,
},
};
use crate::{
AsyncInput,
Buffer,
async_io::MAX_READY_OPERATIONS_PER_POLL,
buffered::DEFAULT_BUFFER_CAPACITY,
};
use qubit_utils::SliceRange;
/// Buffered asynchronous item input.
///
/// This wrapper preserves unread items across [`Poll::Pending`] and performs
/// no runtime-specific work. The wrapped input can be `!Unpin`; pin projection
/// is kept internal and the input is never moved while this wrapper is pinned.
///
/// # Type Parameters
///
/// - `I`: Asynchronous item input type.
#[must_use]
#[derive(Debug)]
pub struct AsyncBufferedInput<I>
where
I: AsyncInput,
I::Item: Clone + Default,
{
/// Asynchronous input being buffered.
inner: I,
/// Storage retaining fetched but unread items.
buffer: Buffer<I::Item>,
}
impl<I> AsyncBufferedInput<I>
where
I: AsyncInput,
I::Item: Clone + Default,
{
/// Creates a buffered input with the default item capacity.
///
/// # Parameters
///
/// - `inner`: Asynchronous item input to buffer.
///
/// # Returns
///
/// Returns a buffered input with [`DEFAULT_BUFFER_CAPACITY`] items.
///
/// # Panics
///
/// Panics if `I::Item::default()` or `I::Item::clone()` panics, or the
/// default backing length exceeds [`Vec`]'s supported capacity.
#[inline(always)]
pub fn new(inner: I) -> Self {
Self::with_capacity(inner, DEFAULT_BUFFER_CAPACITY)
}
/// Creates a buffered input with a requested item capacity.
///
/// # Parameters
///
/// - `inner`: Asynchronous item input to buffer.
/// - `capacity`: Requested number of buffered items.
///
/// # Returns
///
/// Returns a buffered input whose actual capacity is at least one.
///
/// # Panics
///
/// Panics if `I::Item::default()` or `I::Item::clone()` panics, or the
/// requested backing length exceeds [`Vec`]'s supported capacity.
#[inline]
pub fn with_capacity(inner: I, capacity: usize) -> Self {
Self {
inner,
buffer: Buffer::with_capacity(capacity),
}
}
/// Tries to create a buffered input with a requested item capacity.
///
/// # Parameters
///
/// - `inner`: Asynchronous item input to buffer.
/// - `capacity`: Requested number of buffered items.
///
/// # Returns
///
/// Returns a buffered input whose actual capacity is at least one.
///
/// # Errors
///
/// Returns the allocation error when the backing buffer cannot be
/// allocated.
///
/// # Panics
///
/// Panics if initializing the backing buffer requires
/// `I::Item::default()` or `I::Item::clone()` and either operation panics.
#[inline]
pub fn try_with_capacity(
inner: I,
capacity: usize,
) -> Result<Self, TryReserveError> {
Ok(Self {
inner,
buffer: Buffer::try_with_capacity(capacity)?,
})
}
/// Returns a shared reference to the wrapped input.
///
/// # Returns
///
/// Returns the wrapped asynchronous input.
#[inline(always)]
#[must_use]
pub const fn inner(&self) -> &I {
&self.inner
}
/// Returns a mutable reference to the wrapped input.
///
/// Direct reads can invalidate the logical position represented by unread
/// buffered items.
///
/// # Returns
///
/// Returns the wrapped asynchronous input.
#[inline(always)]
#[must_use]
pub fn inner_mut(&mut self) -> &mut I {
&mut self.inner
}
/// Consumes this wrapper and preserves its unread buffer without I/O.
///
/// The returned input can be physically ahead of the returned buffer. To
/// continue the same logical stream, consume the buffer's readable window
/// before reading from that input.
///
/// # Returns
///
/// Returns the wrapped input and buffer. Unread items remain in the
/// buffer's readable window.
#[inline(always)]
#[must_use = "the returned inner input and unread buffer must be handled"]
pub fn into_parts(self) -> (I, Buffer<I::Item>) {
(self.inner, self.buffer)
}
/// Returns the internal item capacity.
///
/// # Returns
///
/// Returns the total number of items in the backing buffer.
#[inline(always)]
#[must_use]
pub fn capacity(&self) -> usize {
self.buffer.capacity()
}
/// Returns the number of unread buffered items.
///
/// # Returns
///
/// Returns the readable-window length.
#[inline(always)]
#[must_use]
pub const fn unread_len(&self) -> usize {
self.buffer.available()
}
/// Returns the unread buffered item window.
///
/// # Returns
///
/// Returns items already fetched but not yet returned to the caller.
#[inline(always)]
#[must_use]
pub fn unread(&self) -> &[I::Item] {
self.buffer.readable()
}
/// Tries to ensure that the internal item capacity is at least `capacity`.
///
/// Existing unread items are retained and this method performs no I/O.
///
/// # Parameters
///
/// - `capacity`: Minimum total item capacity to reserve.
///
/// # Returns
///
/// Returns `Ok(())` after the backing buffer has at least `capacity`
/// item slots.
///
/// # Errors
///
/// Returns the allocation error when the backing buffer cannot grow.
///
/// # Panics
///
/// Panics if growing the backing buffer requires `I::Item::default()` or
/// `I::Item::clone()` and either operation panics.
#[inline(always)]
pub fn try_reserve_capacity(
&mut self,
capacity: usize,
) -> Result<(), TryReserveError> {
self.buffer.try_reserve_capacity(capacity)
}
/// Advances the unread cursor without checking bounds.
///
/// # Parameters
///
/// - `count`: Number of unread items to consume.
///
/// # Safety
///
/// The caller must guarantee that `count <= self.unread_len()`.
#[inline(always)]
pub unsafe fn consume(&mut self, count: usize) {
// SAFETY: The caller guarantees that `count` fits the unread window.
unsafe {
self.buffer.consume(count);
}
}
/// Copies unread items into an indexed output range without consuming them.
///
/// # Parameters
///
/// - `output`: Destination item slice.
/// - `output_index`: First destination index to write.
/// - `count`: Number of unread items to copy.
///
/// # Panics
///
/// Panics if cloning an unread item panics. Debug builds also panic if the
/// output range does not fit or `count` exceeds the unread item count.
///
/// # Safety
///
/// The caller must guarantee that the indexed destination range fits,
/// `count <= self.unread_len()`, and the destination does not overlap the
/// unread window.
#[inline]
pub unsafe fn copy_unread_to(
&self,
output: &mut [I::Item],
output_index: usize,
count: usize,
) {
debug_assert!(
SliceRange::range_fits(output.len(), output_index, count),
"unchecked unread destination range exceeds output buffer"
);
debug_assert!(
count <= self.buffer.available(),
"unchecked unread copy exceeds available buffer items"
);
unsafe {
let source = self.buffer.readable().get_unchecked(..count);
let output =
output.get_unchecked_mut(output_index..output_index + count);
output.clone_from_slice(source);
}
}
/// Polls one refill while preserving unread items.
///
/// # Parameters
///
/// - `cx`: Task context used to register a wake-up.
///
/// # Returns
///
/// Returns `Ok(true)` after appending at least one item, `Ok(false)` at
/// EOF, or [`Poll::Pending`] while the wrapped input is not ready.
///
/// # Errors
///
/// Returns [`ErrorKind::InvalidInput`] when the buffer is full with no
/// consumed prefix to reclaim, or an error reported by the wrapped input.
pub fn poll_fill_more(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<bool>> {
// SAFETY: `inner` remains pinned for the duration of this projection.
let this = unsafe { self.as_mut().get_unchecked_mut() };
if this.buffer.available() == 0 {
this.buffer.clear();
} else if this.buffer.spare_capacity() == 0 {
this.buffer.compact();
if this.buffer.spare_capacity() == 0 {
return Poll::Ready(Err(Error::new(
ErrorKind::InvalidInput,
"buffered input is full; consume buffered items before refilling",
)));
}
}
let result = {
// SAFETY: The projection does not move `inner`.
let inner = unsafe { Pin::new_unchecked(&mut this.inner) };
inner.poll_read(cx, this.buffer.spare_mut())
};
match result {
Poll::Ready(Ok(0)) => Poll::Ready(Ok(false)),
Poll::Ready(Ok(read)) => {
// SAFETY: `poll_read` validated `read` against the spare tail.
unsafe {
this.buffer.advance(read);
}
Poll::Ready(Ok(true))
}
Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
Poll::Pending => Poll::Pending,
}
}
/// Polls refills until `count` unread items are available or EOF occurs.
///
/// # Parameters
///
/// - `cx`: Task context used to register a wake-up.
/// - `count`: Minimum number of unread items to make available.
///
/// # Returns
///
/// Returns `Ok(true)` when `count` items are available, `Ok(false)` at
/// EOF, or [`Poll::Pending`] while the wrapped input is not ready.
///
/// # Errors
///
/// Returns [`ErrorKind::InvalidInput`] when `count` exceeds the buffer
/// capacity, or an error reported by the wrapped input.
pub fn poll_fill_until(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
count: usize,
) -> Poll<io::Result<bool>> {
if count > self.as_ref().get_ref().buffer.capacity() {
return Poll::Ready(Err(Error::new(
ErrorKind::InvalidInput,
"requested available items exceed buffered input capacity",
)));
}
let mut ready_operations = 0;
while self.as_ref().get_ref().buffer.available() < count {
match self.as_mut().poll_fill_more(cx) {
Poll::Ready(Ok(true)) => {
ready_operations += 1;
if self.as_ref().get_ref().buffer.available() < count
&& ready_operations >= MAX_READY_OPERATIONS_PER_POLL
{
cx.waker().wake_by_ref();
return Poll::Pending;
}
}
Poll::Ready(Ok(false)) => return Poll::Ready(Ok(false)),
Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
Poll::Pending => return Poll::Pending,
}
}
Poll::Ready(Ok(true))
}
/// Polls refills until `count` unread items are available.
///
/// # Parameters
///
/// - `cx`: Task context used to register a wake-up.
/// - `count`: Minimum number of unread items to make available.
///
/// # Returns
///
/// Returns a ready success after `count` items are available, or
/// [`Poll::Pending`] while the wrapped input is not ready.
///
/// # Errors
///
/// Returns [`ErrorKind::UnexpectedEof`] after discarding an incomplete
/// unread window, [`ErrorKind::InvalidInput`] when `count` exceeds the
/// buffer capacity, or an error reported by the wrapped input.
pub fn poll_ensure_available(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
count: usize,
) -> Poll<io::Result<()>> {
match self.as_mut().poll_fill_until(cx, count) {
Poll::Ready(Ok(true)) => Poll::Ready(Ok(())),
Poll::Ready(Ok(false)) => {
// SAFETY: The complete unread window is available for discard.
let this = unsafe { self.as_mut().get_unchecked_mut() };
let available = this.buffer.available();
unsafe {
this.buffer.consume(available);
}
Poll::Ready(Err(Error::new(
ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
)))
}
Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
Poll::Pending => Poll::Pending,
}
}
}
impl<I> AsyncBufferedInput<I>
where
I: AsyncInput + Unpin,
I::Item: Clone + Default + Unpin,
{
/// Asynchronously reads and appends at least one item to the unread buffer.
///
/// # Returns
///
/// Returns `Ok(true)` when data was appended or `Ok(false)` at end of
/// input.
///
/// # Errors
///
/// Returns an error when the buffer is full and cannot reclaim space, or
/// when the wrapped input returns an error.
pub async fn fill_more_async(&mut self) -> io::Result<bool> {
poll_fn(|cx| Pin::new(&mut *self).poll_fill_more(cx)).await
}
/// Asynchronously fills the unread buffer until it contains at least
/// `count` items.
///
/// # Parameters
///
/// - `count`: Minimum number of unread items to make available.
///
/// # Returns
///
/// Returns `Ok(true)` when `count` items are available or `Ok(false)` when
/// the wrapped input reaches end of input first.
///
/// # Errors
///
/// Returns an error when `count` exceeds the buffer capacity or when the
/// wrapped input returns an error.
pub async fn fill_until_async(&mut self, count: usize) -> io::Result<bool> {
poll_fn(|cx| Pin::new(&mut *self).poll_fill_until(cx, count)).await
}
/// Asynchronously ensures that the unread buffer contains at least `count`
/// items.
///
/// # Parameters
///
/// - `count`: Minimum number of unread items to make available.
///
/// # Returns
///
/// Returns `Ok(())` after `count` items are available.
///
/// # Errors
///
/// Returns [`ErrorKind::UnexpectedEof`] after discarding incomplete unread
/// data when the wrapped input ends early, [`ErrorKind::InvalidInput`] when
/// `count` exceeds the buffer capacity, or an error from the wrapped input.
pub async fn ensure_available_async(
&mut self,
count: usize,
) -> io::Result<()> {
poll_fn(|cx| Pin::new(&mut *self).poll_ensure_available(cx, count))
.await
}
}
impl<I> AsyncInput for AsyncBufferedInput<I>
where
I: AsyncInput,
I::Item: Clone + Default,
{
/// Item type produced by the wrapped input.
type Item = I::Item;
/// Reports that this input already buffers items.
///
/// # Returns
///
/// Always returns `true`.
#[inline(always)]
fn is_buffered(&self) -> bool {
true
}
/// Polls one read through the retained item buffer.
///
/// A zero-length request completes immediately. Unread items are returned
/// before polling the wrapped input.
///
/// # Parameters
///
/// - `cx`: Task context used to register a wake-up.
/// - `output`: Destination item slice.
/// - `index`: Starting destination index.
/// - `count`: Maximum number of items to read.
///
/// # Returns
///
/// Returns [`Poll::Pending`] when the wrapped input is not ready. A ready
/// result contains the number of items read.
///
/// # Errors
///
/// Returns an I/O error reported by the wrapped input.
///
/// # Panics
///
/// May panic if a nonzero requested output range does not fit when the
/// method copies buffered or newly fetched items into `output`.
///
/// # Safety
///
/// The range `index..index + count` must be valid for `output`.
unsafe fn poll_read_unchecked(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
output: &mut [Self::Item],
index: usize,
count: usize,
) -> Poll<io::Result<usize>> {
if count == 0 {
return Poll::Ready(Ok(0));
}
// SAFETY: `inner` is never moved after projecting from this pinned
// wrapper. `buffer` does not structurally pin any value.
let this = unsafe { self.as_mut().get_unchecked_mut() };
if !this.buffer.is_empty() {
let read = count.min(this.buffer.available());
output[index..index + read]
.clone_from_slice(&this.buffer.readable()[..read]);
// SAFETY: `read` was bounded by the readable-window length.
unsafe {
this.buffer.consume(read);
}
return Poll::Ready(Ok(read));
}
this.buffer.clear();
if count >= this.buffer.capacity() {
let destination = &mut output[index..index + count];
// SAFETY: The pinned wrapper never moves `inner`.
let inner = unsafe { Pin::new_unchecked(&mut this.inner) };
return inner.poll_read(cx, destination);
}
let result = {
// SAFETY: The pinned wrapper never moves `inner`.
let inner = unsafe { Pin::new_unchecked(&mut this.inner) };
inner.poll_read(cx, this.buffer.data_mut())
};
match result {
Poll::Ready(Ok(0)) => Poll::Ready(Ok(0)),
Poll::Ready(Ok(fetched)) => {
// SAFETY: `AsyncInput::poll_read` validated `fetched` against
// the complete backing-buffer length.
unsafe {
this.buffer.advance(fetched);
}
let read = count.min(fetched);
output[index..index + read]
.clone_from_slice(&this.buffer.readable()[..read]);
// SAFETY: `read <= fetched` items are readable.
unsafe {
this.buffer.consume(read);
}
Poll::Ready(Ok(read))
}
Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
Poll::Pending => Poll::Pending,
}
}
}