qubit-io 0.13.0

Byte-stream buffering and std::io utilities for Rust
Documentation
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================

use std::io::{
    Error,
    ErrorKind,
    Result,
    SeekFrom,
};
use std::mem::ManuallyDrop;
use std::ptr;

use crate::buffered::{
    DEFAULT_BUFFER_CAPACITY,
    EnsuredBufferedOutput,
};
use crate::traits::validate_write_count;
use crate::util::UncheckedSlice;
use crate::{
    Buffer,
    Output,
    Seekable,
    SeekableOutput,
};

/// Buffered item output over a wrapped output sink.
///
/// This type keeps a fixed-size item buffer in front of an underlying output so
/// small item writes can be accumulated before they are written to the I/O
/// target. Large writes may bypass the buffer after pending buffered items
/// have been flushed.
///
/// `BufferedOutput` is deliberately item-oriented. It performs no binary
/// encoding, text encoding, or record framing. Higher-level writers can either
/// use the [`Output`] implementation or write directly into
/// [`Self::spare_raw_parts_mut`] and then call [`Self::advance`] after
/// validating the range they initialized.
/// Callers that need to recover the wrapped writer should call
/// [`Self::flush`] first, then use [`Self::into_parts`], or call
/// [`Self::into_inner`] to flush and return the wrapped writer in one step.
/// Dropping a `BufferedOutput` makes a best-effort attempt to write pending
/// buffered items, but drop-time errors are ignored. For arbitrary item types,
/// `BufferedOutput` also supports [`Seekable`]-based seeking in item offsets.
#[derive(Debug)]
pub struct BufferedOutput<O>
where
    O: Output,
    O::Item: Copy + Default,
{
    inner: O,
    buffer: Buffer<O::Item>,
    panicked: bool,
}

impl<O> BufferedOutput<O>
where
    O: Output,
    O::Item: Copy + Default,
{
    /// Creates a buffered item output with the default capacity.
    ///
    /// # Parameters
    ///
    /// * `inner` - The output that receives items when the internal buffer is
    ///   flushed.
    ///
    /// # Returns
    ///
    /// A new buffered item output using `DEFAULT_BUFFER_CAPACITY`.
    #[inline(always)]
    #[must_use]
    pub fn new(inner: O) -> Self {
        Self::with_capacity(inner, DEFAULT_BUFFER_CAPACITY)
    }

    /// Creates a buffered item output with at least the requested capacity.
    ///
    /// # Parameters
    ///
    /// * `inner` - The output that receives items when the internal buffer is
    ///   flushed.
    /// * `capacity` - The requested internal buffer capacity in items.
    ///
    /// # Returns
    ///
    /// A new buffered item output whose actual buffer capacity is
    /// `capacity.max(1)`.
    #[inline(always)]
    #[must_use]
    pub fn with_capacity(inner: O, capacity: usize) -> Self {
        Self {
            inner,
            buffer: Buffer::with_capacity(capacity),
            panicked: false,
        }
    }

    /// Ensures that an output is buffered.
    ///
    /// # Parameters
    ///
    /// * `output` - The output to keep or wrap.
    ///
    /// # Returns
    ///
    /// [`EnsuredBufferedOutput::AlreadyBuffered`] when `output` already
    /// reports buffered status, or [`EnsuredBufferedOutput::Buffered`]
    /// wrapping `output` in [`BufferedOutput`] otherwise.
    #[inline(always)]
    #[must_use]
    pub fn ensure(output: O) -> EnsuredBufferedOutput<O> {
        if output.is_buffered() {
            EnsuredBufferedOutput::AlreadyBuffered(output)
        } else {
            EnsuredBufferedOutput::Buffered(Self::new(output))
        }
    }

    /// Ensures that an output is buffered and boxes the resulting output.
    ///
    /// # Parameters
    ///
    /// * `output` - The concrete output to keep or wrap.
    ///
    /// # Returns
    ///
    /// A boxed output trait object. The original output is boxed directly when
    /// it already reports buffered status; otherwise it is first wrapped in
    /// [`BufferedOutput`].
    #[inline(always)]
    #[must_use]
    pub fn ensure_boxed<'a>(output: O) -> Box<dyn Output<Item = O::Item> + 'a>
    where
        O: 'a,
        O::Item: 'a,
    {
        if output.is_buffered() {
            Box::new(output)
        } else {
            Box::new(Self::new(output))
        }
    }

    /// Returns a shared reference to the wrapped writer.
    ///
    /// # Returns
    ///
    /// An immutable reference to the underlying writer. Pending items may
    /// still be present in the internal buffer and are not flushed by this
    /// method.
    #[inline(always)]
    pub const fn inner(&self) -> &O {
        &self.inner
    }

    /// Returns an exclusive reference to the wrapped writer.
    ///
    /// Pending items may still be present in the internal buffer and are not
    /// flushed by this method.
    ///
    /// # Returns
    ///
    /// A mutable reference to the underlying writer.
    #[inline(always)]
    pub fn inner_mut(&mut self) -> &mut O {
        &mut self.inner
    }

    /// Consumes this buffered output after flushing pending items.
    ///
    /// # Returns
    ///
    /// The wrapped output after all buffered items have been written.
    ///
    /// # Errors
    ///
    /// Returns any error produced while flushing pending items or flushing the
    /// wrapped output. If an error is returned, this value is dropped and any
    /// remaining pending items are flushed only on a best-effort basis.
    #[inline]
    pub fn into_inner(mut self) -> Result<O> {
        self.flush()?;
        let (inner, _) = self.into_parts();
        Ok(inner)
    }

    /// Consumes this buffered output without flushing pending items.
    ///
    /// This method performs no I/O. Pending items that have been accepted into
    /// the internal buffer but not written to the wrapped writer remain in the
    /// readable window of the returned buffer.
    ///
    /// # Returns
    ///
    /// The wrapped writer and the buffer holding pending items in logical write
    /// order.
    #[inline(always)]
    #[must_use]
    pub fn into_parts(self) -> (O, Buffer<O::Item>) {
        let this = ManuallyDrop::new(self);
        // SAFETY: `this` will not be dropped, so reading both fields moves them
        // out exactly once. The `panicked` flag is intentionally discarded.
        unsafe {
            let inner = ptr::read(&this.inner);
            let buffer = ptr::read(&this.buffer);
            (inner, buffer)
        }
    }

    /// Returns the internal buffer capacity.
    ///
    /// # Returns
    ///
    /// The total number of items that can be held by the internal buffer.
    #[inline(always)]
    #[must_use]
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Returns the unused capacity in the internal buffer.
    ///
    /// # Returns
    ///
    /// The number of items that can still be appended to the internal buffer
    /// before it must be flushed.
    #[inline(always)]
    #[must_use]
    pub fn spare_capacity(&self) -> usize {
        self.buffer.spare_capacity()
    }

    /// Returns raw spare-buffer parts for hot-path callers.
    ///
    /// The returned slice is the full internal backing storage. `index` is the
    /// start of the spare item window, and `count` is the number of spare
    /// items. Callers that need a slice can use `&mut buffer[index..index +
    /// count]`; callers that already validated bounds can pass `buffer` and
    /// `index` directly to indexed unchecked codecs.
    ///
    /// Mutating items outside `index..index + count` changes pending output
    /// items and may corrupt the logical stream.
    ///
    /// # Returns
    ///
    /// The backing storage, the spare start index, and the spare item count.
    #[inline(always)]
    #[must_use]
    pub fn spare_raw_parts_mut(&mut self) -> (&mut [O::Item], usize, usize) {
        self.buffer.spare_raw_parts_mut()
    }

    /// Marks spare items as written without checking bounds.
    ///
    /// # Parameters
    ///
    /// * `count` - Number of initialized spare items to make pending for
    ///   output.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `count <= self.spare_capacity()` and
    /// that the corresponding items in the spare range reported by
    /// [`Self::spare_raw_parts_mut`] have been initialized.
    #[inline(always)]
    pub unsafe fn advance(&mut self, count: usize) {
        // SAFETY: The caller guarantees that `count` is within spare capacity.
        unsafe {
            self.buffer.advance(count);
        }
    }

    /// Ensures that at least `count` items are available in the spare buffer.
    ///
    /// # Parameters
    ///
    /// * `count` - Number of spare items required.
    ///
    /// # Errors
    ///
    /// Returns any non-interrupted I/O error produced while flushing buffered
    /// items. Returns [`ErrorKind::InvalidInput`] if `count` exceeds the buffer
    /// capacity. Returns [`ErrorKind::InvalidData`] if the wrapped writer
    /// reports more items than the pending buffer range contained.
    pub fn ensure_spare_capacity(&mut self, count: usize) -> Result<()> {
        if count > self.buffer.capacity() {
            return Err(Error::new(
                ErrorKind::InvalidInput,
                "requested spare capacity exceeds buffered output capacity",
            ));
        }
        if self.spare_capacity() < count {
            self.flush_buffer()?;
        }
        Ok(())
    }

    /// Writes items from the input slice and reports the accepted item count.
    ///
    /// This is the buffered implementation for single-write callers.
    /// Small inputs are appended to the buffer and reported as fully accepted;
    /// large inputs may be delegated to the wrapped writer after pending items
    /// are flushed.
    ///
    /// # Parameters
    ///
    /// * `input` - The items to write.
    ///
    /// # Returns
    ///
    /// The number of items accepted. Buffered writes return `input.len()`;
    /// direct writes return the item count reported by the wrapped writer.
    ///
    /// # Errors
    ///
    /// Returns any I/O error produced while flushing pending items or writing a
    /// large input directly to the wrapped writer. Flush failures include
    /// [`ErrorKind::WriteZero`] if the writer reports that zero items were
    /// written before the buffer is drained, and [`ErrorKind::InvalidData`] if
    /// it reports more items than the requested range contained.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `input_index..input_index + count` is a
    /// valid range inside `input` and that the addition does not overflow.
    #[inline]
    pub unsafe fn write_unchecked(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<usize> {
        // Keep this boundary in sync with `std::io::BufWriter`: it uses
        // `< spare_capacity()` intentionally so buffer-sized writes skip the
        // memcpy+advance hot path. That path is only for strictly smaller
        // inputs as an in-memory append optimization.
        if count < self.spare_capacity() {
            // SAFETY: The branch proves that the input fits in spare capacity.
            unsafe {
                self.write_to_buffer(input, input_index, count);
            }
            Ok(count)
        } else {
            // SAFETY: The caller guarantees the source range is valid.
            unsafe { self.write_cold(input, input_index, count) }
        }
    }

    /// Writes items from the full input slice.
    ///
    /// # Parameters
    ///
    /// * `input` - Source items.
    ///
    /// # Returns
    ///
    /// The number of items accepted from `input`.
    ///
    /// # Errors
    ///
    /// Returns any I/O error produced while flushing pending items or writing a
    /// large input directly to the wrapped writer. Returns
    /// [`ErrorKind::InvalidData`] if the wrapped writer reports accepting more
    /// items than requested.
    #[inline(always)]
    pub fn write(&mut self, input: &[O::Item]) -> Result<usize> {
        // SAFETY: The full input slice is a valid source range.
        let written = unsafe { self.write_unchecked(input, 0, input.len()) }?;
        validate_write_count(written, input.len())?;
        Ok(written)
    }

    /// Writes all items through the internal buffer.
    ///
    /// Small inputs are appended to the internal buffer.  Inputs that do not
    /// fit may flush the buffer first, and inputs at least as large as the
    /// buffer may be written directly to the wrapped writer.
    ///
    /// # Parameters
    ///
    /// * `input` - The items to write.
    ///
    /// # Returns
    ///
    /// `Ok(())` after all items from `input` have been accepted.
    ///
    /// # Errors
    ///
    /// Returns any I/O error produced while flushing pending items or writing a
    /// large input directly to the wrapped writer. Flush failures include
    /// [`ErrorKind::WriteZero`] if the writer reports that zero items were
    /// written before the buffer is drained, and [`ErrorKind::InvalidData`] if
    /// it reports more items than the requested range contained.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `input_index..input_index + count` is a
    /// valid range inside `input` and that the addition does not overflow.
    #[inline]
    pub unsafe fn write_fully_unchecked(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<()> {
        // Keep this boundary in sync with `std::io::BufWriter`: it uses
        // `< spare_capacity()` intentionally so buffer-sized writes skip the
        // memcpy+advance hot path. That path is only for strictly smaller
        // inputs as an in-memory append optimization.
        if count < self.spare_capacity() {
            // SAFETY: The branch proves that the input fits in spare capacity.
            unsafe {
                self.write_to_buffer(input, input_index, count);
            }
            Ok(())
        } else {
            // SAFETY: The caller guarantees the source range is valid.
            unsafe { self.write_fully_cold(input, input_index, count) }
        }
    }

    /// Writes all items from the full input slice through the internal buffer.
    ///
    /// # Parameters
    ///
    /// * `input` - Source items.
    ///
    /// # Errors
    ///
    /// Returns any I/O error produced while flushing pending items or writing a
    /// large input directly to the wrapped writer.
    #[inline(always)]
    pub fn write_fully(&mut self, input: &[O::Item]) -> Result<()> {
        // SAFETY: The full input slice is a valid source range.
        unsafe { self.write_fully_unchecked(input, 0, input.len()) }
    }

    /// Flushes buffered items and then flushes the wrapped output.
    ///
    /// # Returns
    ///
    /// `Ok(())` once pending buffered items and the wrapped output are
    /// flushed.
    ///
    /// # Errors
    ///
    /// Returns any non-interrupted I/O error produced while flushing buffered
    /// items, [`ErrorKind::WriteZero`] if the wrapped writer cannot make
    /// progress while draining the buffer, [`ErrorKind::InvalidData`] if the
    /// writer reports an impossible item count, or any error returned by
    /// [`Output::flush`] on the wrapped output.
    #[inline(always)]
    pub fn flush(&mut self) -> Result<()> {
        self.flush_buffer()
            .and_then(|()| Output::flush(&mut self.inner))
    }

    /// Flushes buffered items to the wrapped writer.
    ///
    /// The method retries interrupted writes.  If an error occurs after some
    /// items have been written, the already-written items are removed from the
    /// front of the buffer and the unwritten suffix is kept for a later retry.
    ///
    /// # Returns
    ///
    /// `Ok(())` once all currently buffered items have been written to the
    /// wrapped writer.
    ///
    /// # Errors
    ///
    /// Returns any non-interrupted I/O error produced by the wrapped writer.
    /// Returns [`ErrorKind::WriteZero`] if the writer reports a zero-length
    /// write before all buffered items are drained. Returns
    /// [`ErrorKind::InvalidData`] if the writer reports more items than the
    /// pending buffer range contained.
    fn flush_buffer(&mut self) -> Result<()> {
        while !self.buffer.is_empty() {
            let position = self.buffer.position();
            let available = self.buffer.available();
            // SAFETY: `position..position + available` is the current readable
            // range maintained by `Buffer`.
            self.panicked = true;
            let result = unsafe {
                self.inner.write_unchecked(
                    self.buffer.data(),
                    position,
                    available,
                )
            };
            self.panicked = false;
            match result {
                Ok(0) => {
                    self.buffer.compact();
                    return Err(Error::new(
                        ErrorKind::WriteZero,
                        "failed to write buffered data",
                    ));
                }
                Ok(written) => {
                    if let Err(error) = validate_write_count(written, available)
                    {
                        self.buffer.compact();
                        return Err(error);
                    }
                    // SAFETY: The validated count is in `0..=available`.
                    unsafe {
                        self.buffer.consume(written);
                    }
                }
                Err(error) if error.kind() == ErrorKind::Interrupted => {}
                Err(error) => {
                    self.buffer.compact();
                    return Err(error);
                }
            }
        }
        self.buffer.clear();
        Ok(())
    }

    /// Returns the logical output position without flushing pending items.
    ///
    /// The returned position is the wrapped output's current position plus the
    /// number of items currently pending in this buffer.
    ///
    /// # Returns
    ///
    /// The logical stream position in output items.
    ///
    /// # Errors
    ///
    /// Returns any error produced while querying the wrapped output position.
    /// Returns [`ErrorKind::InvalidData`] if adding the pending item count
    /// overflows `u64`.
    pub fn stream_position(&mut self) -> Result<u64>
    where
        O: SeekableOutput,
    {
        let position =
            Seekable::seek_to(&mut self.inner, SeekFrom::Current(0))?;
        position
            .checked_add(self.buffer.available() as u64)
            .ok_or_else(|| {
                Error::new(
                    ErrorKind::InvalidData,
                    "buffered pending items overflow wrapped output position",
                )
            })
    }

    /// Seeks the wrapped output in items, flushing buffered items first.
    ///
    /// Pending items accepted into the internal buffer are written to the
    /// wrapped output before [`Seekable::seek_to`] is invoked, so the seek
    /// position is relative to data already committed to the underlying sink.
    ///
    /// # Parameters
    ///
    /// * `position` - The target seek position in output items.
    ///
    /// # Returns
    ///
    /// The new stream position in items.
    ///
    /// # Errors
    ///
    /// Returns any non-interrupted I/O error produced while flushing buffered
    /// items, [`ErrorKind::WriteZero`] if the wrapped output cannot make
    /// progress while draining the buffer, [`ErrorKind::InvalidData`] if the
    /// writer reports an impossible item count, or any error returned by
    /// [`Seekable::seek_to`] on the wrapped output.
    #[inline(always)]
    pub fn seek_to(&mut self, position: SeekFrom) -> Result<u64>
    where
        O: SeekableOutput,
    {
        match position {
            SeekFrom::Current(0) => self.stream_position(),
            other => self
                .flush_buffer()
                .and_then(|()| Seekable::seek_to(&mut self.inner, other)),
        }
    }

    /// Writes items into the internal buffer without checking spare capacity.
    ///
    /// # Parameters
    ///
    /// * `input` - The source items.
    /// * `input_index` - The starting index in `input`.
    /// * `count` - The number of items to copy.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `input_index..input_index + count` is valid
    /// in `input`, that `count <= self.spare_capacity()`, and that the copied
    /// source range does not overlap with the destination range in the internal
    /// buffer.
    #[inline(always)]
    unsafe fn write_to_buffer(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) {
        debug_assert!(
            UncheckedSlice::range_fits(input.len(), input_index, count),
            "unchecked write range exceeds input buffer"
        );
        debug_assert!(
            count <= self.spare_capacity(),
            "unchecked write exceeds spare buffer capacity"
        );
        let (destination, destination_index, _) =
            self.buffer.spare_raw_parts_mut();
        // SAFETY: The caller guarantees valid source and destination ranges and
        // that they do not overlap.
        unsafe {
            UncheckedSlice::copy_nonoverlapping(
                input,
                input_index,
                destination,
                destination_index,
                count,
            );
            self.buffer.advance(count);
        }
    }

    /// Writes items to the wrapped writer and validates the reported count.
    ///
    /// # Parameters
    ///
    /// * `input` - Source storage.
    /// * `input_index` - Start index inside `input`.
    /// * `count` - Maximum number of items to write.
    ///
    /// # Returns
    ///
    /// The number of items accepted by the wrapped writer.
    ///
    /// # Errors
    ///
    /// Returns the wrapped writer's I/O error, or [`ErrorKind::InvalidData`]
    /// if it reports an item count larger than `count`.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `input_index..input_index + count` is a
    /// valid range inside `input` and that the addition does not overflow.
    #[inline(always)]
    unsafe fn write_inner(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<usize> {
        // SAFETY: The caller guarantees the source range is valid.
        let written =
            unsafe { self.inner.write_unchecked(input, input_index, count) }?;
        validate_write_count(written, count)?;
        Ok(written)
    }

    /// Writes all items in an indexed source range to the wrapped writer.
    ///
    /// # Parameters
    ///
    /// * `input` - Source storage.
    /// * `input_index` - Start index inside `input`.
    /// * `count` - Number of items to write.
    ///
    /// # Errors
    ///
    /// Returns the wrapped writer's I/O error, [`ErrorKind::WriteZero`] if the
    /// writer cannot make progress, or [`ErrorKind::InvalidData`] if it
    /// reports an impossible item count.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `input_index..input_index + count` is a
    /// valid range inside `input` and that the addition does not overflow.
    unsafe fn write_fully_inner(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<()> {
        let mut written = 0;
        while written < count {
            let remaining = count - written;
            // SAFETY: `written < count`, so this suffix remains inside the
            // caller-validated source range.
            match unsafe {
                self.write_inner(input, input_index + written, remaining)
            } {
                Ok(0) => {
                    return Err(Error::new(
                        ErrorKind::WriteZero,
                        "failed to write whole buffer",
                    ));
                }
                Ok(count) => written += count,
                Err(error) if error.kind() == ErrorKind::Interrupted => {}
                Err(error) => return Err(error),
            }
        }
        Ok(())
    }

    /// Handles slow-path raw writes that must flush or bypass the buffer.
    ///
    /// # Parameters
    ///
    /// * `input` - The items to write after the fast path determined that they
    ///   do not fit comfortably in the current spare buffer capacity.
    ///
    /// # Returns
    ///
    /// `Ok(())` after all items from `input` have been accepted either by the
    /// buffer or by the wrapped writer.
    ///
    /// # Errors
    ///
    /// Returns any I/O error produced while flushing pending items or writing a
    /// large input directly to the wrapped writer. Flush failures include
    /// [`ErrorKind::WriteZero`] if the writer reports that zero items were
    /// written before the buffer is drained, and [`ErrorKind::InvalidData`] if
    /// it reports more items than the requested range contained.
    #[cold]
    #[inline(never)]
    unsafe fn write_fully_cold(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<()> {
        if count > self.spare_capacity() {
            self.flush_buffer()?;
        }
        if count >= self.buffer.capacity() {
            // SAFETY: The range covers the full source slice.
            unsafe { self.write_fully_inner(input, input_index, count) }
        } else {
            // SAFETY: After the optional flush, any input smaller than the
            // buffer capacity fits in the empty or sufficiently spare buffer.
            unsafe {
                self.write_to_buffer(input, input_index, count);
            }
            Ok(())
        }
    }

    /// Handles slow-path raw writes for single-write semantics.
    ///
    /// The method may accept fewer items than the input length when the write
    /// is delegated directly to the wrapped output.
    ///
    /// # Parameters
    ///
    /// * `input` - The items to write after the fast path determined that they
    ///   do not fit comfortably in the current spare buffer capacity.
    ///
    /// # Returns
    ///
    /// The number of items accepted. Buffered writes return `input.len()`;
    /// direct writes return the item count reported by the wrapped writer.
    ///
    /// # Errors
    ///
    /// Returns any I/O error produced while flushing pending items or writing a
    /// large input directly to the wrapped writer. Flush failures include
    /// [`ErrorKind::WriteZero`] if the writer reports that zero items were
    /// written before the buffer is drained, and [`ErrorKind::InvalidData`] if
    /// it reports more items than the requested range contained.
    #[cold]
    #[inline(never)]
    unsafe fn write_cold(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<usize> {
        if count > self.spare_capacity() {
            self.flush_buffer()?;
        }
        if count >= self.buffer.capacity() {
            // SAFETY: The range covers the full source slice.
            unsafe { self.write_inner(input, input_index, count) }
        } else {
            // SAFETY: After the optional flush, any input smaller than the
            // buffer capacity fits in the empty or sufficiently spare buffer.
            unsafe {
                self.write_to_buffer(input, input_index, count);
            }
            Ok(count)
        }
    }
}

impl<O> Output for BufferedOutput<O>
where
    O: Output,
    O::Item: Copy + Default,
{
    type Item = O::Item;

    /// Reports that this output already buffers items internally.
    #[inline(always)]
    fn is_buffered(&self) -> bool {
        true
    }

    /// Writes items through the internal buffer.
    #[inline(always)]
    unsafe fn write_unchecked(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) -> Result<usize> {
        // SAFETY: Forwarded from the trait caller.
        unsafe {
            BufferedOutput::write_unchecked(self, input, input_index, count)
        }
    }

    /// Writes items from the full input slice.
    #[inline(always)]
    fn write(&mut self, input: &[Self::Item]) -> Result<usize> {
        BufferedOutput::write(self, input)
    }

    /// Writes all items through the internal buffer.
    #[inline(always)]
    unsafe fn write_fully_unchecked(
        &mut self,
        input: &[Self::Item],
        index: usize,
        count: usize,
    ) -> Result<()> {
        // SAFETY: Forwarded from the trait caller.
        unsafe {
            BufferedOutput::write_fully_unchecked(self, input, index, count)
        }
    }

    /// Writes all items from the full input slice through the internal buffer.
    #[inline(always)]
    fn write_fully(&mut self, input: &[Self::Item]) -> Result<()> {
        BufferedOutput::write_fully(self, input)
    }

    /// Flushes pending items through the internal buffer.
    #[inline(always)]
    fn flush(&mut self) -> Result<()> {
        BufferedOutput::flush(self)
    }
}

impl<O> Seekable for BufferedOutput<O>
where
    O: SeekableOutput,
    <O as Output>::Item: Copy + Default,
{
    type Unit = <O as Output>::Item;

    /// Seeks the buffered output in item offsets.
    #[inline(always)]
    fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
        BufferedOutput::seek_to(self, position)
    }
}

impl<O> Drop for BufferedOutput<O>
where
    O: Output,
    O::Item: Copy + Default,
{
    fn drop(&mut self) {
        if !self.panicked {
            drop(self.flush_buffer());
        }
    }
}