qubit-io 0.15.0

Runtime-neutral synchronous and asynchronous item-stream I/O 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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================

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

use crate::buffered::{
    DEFAULT_BUFFER_CAPACITY,
    EnsuredBufferedOutput,
};
use crate::traits::validate_write_count;
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.
/// Call [`Self::flush`] before [`Self::into_parts`] when pending items must be
/// written before recovering the wrapped writer.
/// 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.
///
/// # Type Parameters
///
/// - `O`: Wrapped item output type.
#[must_use]
#[derive(Debug)]
pub struct BufferedOutput<O>
where
    O: Output,
    O::Item: Clone + Default,
{
    /// Output receiving buffered items.
    inner: O,
    /// Storage retaining accepted but unwritten items.
    buffer: Buffer<O::Item>,
    /// Whether a wrapped write is currently unwinding.
    panicked: bool,
}

impl<O> BufferedOutput<O>
where
    O: Output,
    O::Item: Clone + Default,
{
    /// Creates a buffered item output with the default capacity.
    ///
    /// # Parameters
    ///
    /// - `inner`: Output that receives items when the internal buffer is
    ///   flushed.
    ///
    /// # Returns
    ///
    /// Returns a new buffered item output using `DEFAULT_BUFFER_CAPACITY`.
    ///
    /// # Panics
    ///
    /// Panics if `O::Item::default()` or `O::Item::clone()` panics, or the
    /// default backing length exceeds [`Vec`]'s supported capacity.
    #[inline(always)]
    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`: Output that receives items when the internal buffer is
    ///   flushed.
    /// - `capacity`: Requested internal buffer capacity in items.
    ///
    /// # Returns
    ///
    /// Returns a new buffered item output whose actual buffer capacity is
    /// `capacity.max(1)`.
    ///
    /// # Panics
    ///
    /// Panics if `O::Item::default()` or `O::Item::clone()` panics, or the
    /// requested backing length exceeds [`Vec`]'s supported capacity.
    #[inline]
    pub fn with_capacity(inner: O, capacity: usize) -> Self {
        Self {
            inner,
            buffer: Buffer::with_capacity(capacity),
            panicked: false,
        }
    }

    /// Tries to create a buffered item output with a requested capacity.
    ///
    /// # Parameters
    ///
    /// - `inner`: Output receiving items when the buffer is flushed.
    /// - `capacity`: Requested internal buffer capacity in items.
    ///
    /// # Returns
    ///
    /// Returns a buffered item output whose actual buffer capacity is
    /// `capacity.max(1)`.
    ///
    /// # Errors
    ///
    /// Returns the allocation error when the backing buffer cannot be
    /// allocated.
    ///
    /// # Panics
    ///
    /// Panics if initializing the backing buffer requires
    /// `O::Item::default()` or `O::Item::clone()` and either operation panics.
    #[inline]
    pub fn try_with_capacity(
        inner: O,
        capacity: usize,
    ) -> std::result::Result<Self, TryReserveError> {
        Ok(Self {
            inner,
            buffer: Buffer::try_with_capacity(capacity)?,
            panicked: false,
        })
    }

    /// Ensures that an output is buffered.
    ///
    /// # Parameters
    ///
    /// - `output`: Output to keep or wrap.
    ///
    /// # Returns
    ///
    /// [`EnsuredBufferedOutput::AlreadyBuffered`] when `output` already
    /// reports buffered status, or [`EnsuredBufferedOutput::Buffered`]
    /// wrapping `output` in [`BufferedOutput`] otherwise.
    ///
    /// This check only observes [`Output::is_buffered`]. Standard-library
    /// [`std::io::BufWriter`] values use the blanket [`std::io::Write`]
    /// implementation and are therefore not detected.
    ///
    /// # Panics
    ///
    /// Panics while wrapping an unbuffered output if `O::Item::default()` or
    /// `O::Item::clone()` panics, or the default backing length exceeds
    /// [`Vec`]'s supported capacity.
    #[inline]
    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.
    ///
    /// # Type Parameters
    ///
    /// - `'a`: Lifetime required for the returned trait object.
    ///
    /// # Parameters
    ///
    /// - `output`: 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`].
    ///
    /// This check only observes [`Output::is_buffered`]. Standard-library
    /// [`std::io::BufWriter`] values use the blanket [`std::io::Write`]
    /// implementation and are therefore not detected.
    ///
    /// # Panics
    ///
    /// Panics while wrapping an unbuffered output if `O::Item::default()` or
    /// `O::Item::clone()` panics, or the default backing length exceeds
    /// [`Vec`]'s supported capacity.
    #[inline]
    #[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)]
    #[must_use]
    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. Writing through the returned reference can place
    /// new items before those pending items.
    ///
    /// # Returns
    ///
    /// Returns a mutable reference to the underlying writer.
    #[inline(always)]
    #[must_use]
    pub fn inner_mut(&mut self) -> &mut O {
        &mut self.inner
    }

    /// Consumes this buffered output without flushing pending items.
    ///
    /// This method does not call [`Self::flush`] and 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. Call [`Self::flush`] first for normal completion; a successful
    /// flush leaves that buffer empty. Calling this method first transfers
    /// responsibility for pending items to the caller.
    ///
    /// # Returns
    ///
    /// Returns the wrapped output and the buffer holding pending items in
    /// logical write order.
    #[inline]
    #[must_use = "the returned inner output and pending buffer must be handled"]
    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()
    }

    /// Tries to ensure that the internal buffer has at least `capacity` items.
    ///
    /// Pending items are preserved and this method performs no I/O.
    ///
    /// # Parameters
    ///
    /// - `capacity`: Required total buffer capacity.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after the requested capacity is available.
    ///
    /// # Errors
    ///
    /// Returns the original allocation error when the buffer cannot grow.
    ///
    /// # Panics
    ///
    /// Panics if growing the buffer requires `O::Item::default()` or
    /// `O::Item::clone()` and either operation panics.
    #[inline(always)]
    pub fn try_reserve_capacity(
        &mut self,
        capacity: usize,
    ) -> std::result::Result<(), std::collections::TryReserveError> {
        self.buffer.try_reserve_capacity(capacity)
    }

    /// Returns the unused capacity in the internal buffer.
    ///
    /// # Returns
    ///
    /// 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
    ///
    /// 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.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if `count > self.spare_capacity()`.
    ///
    /// # 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.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after at least `count` spare items are available.
    ///
    /// # 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.
    #[inline]
    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`: Source item slice.
    /// - `input_index`: Starting source index.
    /// - `count`: Maximum number of items to accept.
    ///
    /// # Returns
    ///
    /// Returns the number of items accepted. Buffered writes return `count`;
    /// 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.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested input range does not fit.
    ///
    /// # 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
    ///
    /// 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]
    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`: Source item slice.
    /// - `input_index`: Starting source index.
    /// - `count`: Number of items to write.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after all requested items 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.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested input range does not fit.
    ///
    /// # 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.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after every source item has been accepted.
    ///
    /// # 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
    ///
    /// 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]
    pub fn flush(&mut self) -> Result<()> {
        self.flush_buffer()
            .and_then(|()| Output::flush(&mut self.inner))
    }

    /// 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
    ///
    /// 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`.
    #[inline]
    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`: Target seek position in output items.
    ///
    /// # Returns
    ///
    /// 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]
    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)),
        }
    }

    /// 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
    ///
    /// 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(())
    }

    /// Writes items into the internal buffer without checking spare capacity.
    ///
    /// # Parameters
    ///
    /// - `input`: Source items.
    /// - `input_index`: Starting index in `input`.
    /// - `count`: Number of items to copy.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the input range does not fit or `count`
    /// exceeds the spare buffer capacity.
    ///
    /// # 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]
    unsafe fn write_to_buffer(
        &mut self,
        input: &[O::Item],
        input_index: usize,
        count: usize,
    ) {
        // SAFETY: The caller guarantees valid source and spare destination
        // ranges, and the buffer advances only after cloning succeeds.
        unsafe {
            self.buffer.copy_from(input, input_index, count);
        }
    }

    /// Writes items to the wrapped writer and validates the reported count.
    ///
    /// # Parameters
    ///
    /// - `input`: Source storage.
    /// - `input_index`: Starting index inside `input`.
    /// - `count`: Maximum number of items to write.
    ///
    /// # Returns
    ///
    /// 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]
    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`: Starting index inside `input`.
    /// - `count`: Number of items to write.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after every requested item is written.
    ///
    /// # 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`: Source item slice.
    /// - `input_index`: Starting source index.
    /// - `count`: Number of items to write after the fast path determined that
    ///   they do not fit comfortably in the current spare buffer capacity.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after all requested items 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.
    ///
    /// # Safety
    ///
    /// The range `input_index..input_index + count` must be valid for `input`.
    #[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`: Source item slice.
    /// - `input_index`: Starting source index.
    /// - `count`: Maximum number of items to accept after the fast path
    ///   determined that they do not fit comfortably in the current spare
    ///   buffer capacity.
    ///
    /// # Returns
    ///
    /// Returns the number of items accepted. Buffered writes return `count`;
    /// 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 range `input_index..input_index + count` must be valid for `input`.
    #[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: Clone + Default,
{
    /// Item type accepted by the buffered output.
    type Item = O::Item;

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

    /// Writes items through the internal buffer.
    ///
    /// # Parameters
    ///
    /// - `input`: Source item slice.
    /// - `input_index`: Starting source index.
    /// - `count`: Maximum number of items to accept.
    ///
    /// # Returns
    ///
    /// Returns the number of items accepted.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorKind::WriteZero`] if pending items cannot be drained or
    /// [`ErrorKind::InvalidData`] if the wrapped output reports an invalid
    /// count. Other errors are propagated from the wrapped output.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested input range does not fit.
    ///
    /// # Safety
    ///
    /// The range `input_index..input_index + count` must be valid for `input`.
    #[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.
    ///
    /// # Parameters
    ///
    /// - `input`: Source item slice.
    ///
    /// # Returns
    ///
    /// Returns the number of items accepted.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorKind::WriteZero`] if pending items cannot be drained or
    /// [`ErrorKind::InvalidData`] if the wrapped output reports an invalid
    /// count. Other errors are propagated from the wrapped output.
    #[inline(always)]
    fn write(&mut self, input: &[Self::Item]) -> Result<usize> {
        BufferedOutput::write(self, input)
    }

    /// Writes all items through the internal buffer.
    ///
    /// # Parameters
    ///
    /// - `input`: Source item slice.
    /// - `index`: Starting source index.
    /// - `count`: Number of items to write.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after every requested item is accepted.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorKind::WriteZero`] if the wrapped output cannot make
    /// progress or [`ErrorKind::InvalidData`] if it reports an invalid count.
    /// Other errors are propagated from the wrapped output.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested input range does not fit.
    ///
    /// # Safety
    ///
    /// The range `index..index + count` must be valid for `input`.
    #[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.
    ///
    /// # Parameters
    ///
    /// - `input`: Source item slice.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after every item is accepted.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorKind::WriteZero`] if the wrapped output cannot make
    /// progress or [`ErrorKind::InvalidData`] if it reports an invalid count.
    /// Other errors are propagated from the wrapped output.
    #[inline(always)]
    fn write_fully(&mut self, input: &[Self::Item]) -> Result<()> {
        BufferedOutput::write_fully(self, input)
    }

    /// Flushes pending items through the internal buffer.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` after pending items and the wrapped output are flushed.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorKind::WriteZero`] if pending items cannot be drained or
    /// [`ErrorKind::InvalidData`] if the wrapped output reports an invalid
    /// count. Other errors are propagated from the wrapped output.
    #[inline(always)]
    fn flush(&mut self) -> Result<()> {
        BufferedOutput::flush(self)
    }
}

impl<O> Seekable for BufferedOutput<O>
where
    O: SeekableOutput,
    <O as Output>::Item: Clone + Default,
{
    /// Item unit used for seek offsets.
    type Unit = <O as Output>::Item;

    /// Seeks the buffered output in item offsets.
    ///
    /// # Parameters
    ///
    /// - `position`: Target item offset.
    ///
    /// # Returns
    ///
    /// Returns the resulting absolute item position.
    ///
    /// # Errors
    ///
    /// Returns an error reported while draining pending items or seeking the
    /// wrapped output.
    #[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: Clone + Default,
{
    /// Attempts a best-effort drain unless another wrapped write is unwinding.
    ///
    /// Any drain error is ignored because destructors cannot report it.
    fn drop(&mut self) {
        if !self.panicked {
            drop(self.flush_buffer());
        }
    }
}