qubit_io/std_io/ext/write_ext.rs
1// =============================================================================
2// Copyright (c) 2026 Haixing Hu.
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6// Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! Extension operations for standard byte writers.
10
11use std::io::Result;
12use std::io::Write;
13
14use crate::util::UncheckedSlice;
15
16/// Extension methods for [`Write`] values.
17///
18/// `WriteExt` provides small method-style helpers for byte writers. The
19/// methods are implemented for every type that implements [`Write`], including
20/// `dyn Write` trait objects.
21pub trait WriteExt: Write {
22 /// Writes bytes from a range of `buffer` without checking the range bounds
23 /// in release builds.
24 ///
25 /// This method delegates to [`Write::write`] after creating the source
26 /// slice with raw pointer arithmetic. It performs at most one write
27 /// operation and returns the number of bytes written, keeping the same
28 /// short-write and error behavior as [`Write::write`].
29 ///
30 /// # Parameters
31 /// - `buffer`: Source buffer.
32 /// - `start_index`: Start offset inside `buffer`.
33 /// - `count`: Maximum number of bytes to write.
34 ///
35 /// # Returns
36 /// The number of bytes written from `buffer[start_index..start_index +
37 /// count]`. The value is in `0..=count`.
38 ///
39 /// # Errors
40 /// Returns the error reported by [`Write::write`].
41 ///
42 /// # Panics
43 /// Panics in debug builds if the requested source range does not fit.
44 ///
45 /// # Safety
46 /// The caller must guarantee that `start_index..start_index + count` is a
47 /// valid range within `buffer` and that `start_index + count` does not
48 /// overflow `usize`.
49 #[inline(always)]
50 unsafe fn write_unchecked(&mut self, buffer: &[u8], start_index: usize, count: usize) -> Result<usize> {
51 // SAFETY: The caller guarantees that the computed pointer and length
52 // form a valid subslice of `buffer`.
53 let source = unsafe { UncheckedSlice::subslice(buffer, start_index, count) };
54 self.write(source)
55 }
56
57 /// Writes exactly `count` bytes from a range of `buffer` without checking
58 /// the range bounds in release builds.
59 ///
60 /// This method delegates to [`Write::write_all`] after creating the source
61 /// slice with raw pointer arithmetic. It keeps the same short-write,
62 /// [`std::io::ErrorKind::Interrupted`], and
63 /// [`std::io::ErrorKind::WriteZero`] behavior as [`Write::write_all`].
64 ///
65 /// # Parameters
66 /// - `buffer`: Source buffer.
67 /// - `start_index`: Start offset inside `buffer`.
68 /// - `count`: Number of bytes to write.
69 ///
70 /// # Returns
71 /// `Ok(())` after the requested range has been written.
72 ///
73 /// # Errors
74 /// Returns the error reported by [`Write::write_all`].
75 ///
76 /// # Panics
77 /// Panics in debug builds if the requested source range does not fit.
78 ///
79 /// # Safety
80 /// The caller must guarantee that `start_index..start_index + count` is a
81 /// valid range within `buffer` and that `start_index + count` does not
82 /// overflow `usize`.
83 #[inline(always)]
84 unsafe fn write_all_unchecked(&mut self, buffer: &[u8], start_index: usize, count: usize) -> Result<()> {
85 // SAFETY: The caller guarantees that the computed pointer and length
86 // form a valid subslice of `buffer`.
87 let source = unsafe { UncheckedSlice::subslice(buffer, start_index, count) };
88 self.write_all(source)
89 }
90}
91
92impl<T> WriteExt for T where T: Write + ?Sized {}