qubit_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
9use std::io::{
10 Result,
11 Write,
12};
13
14/// Extension methods for [`Write`] values.
15///
16/// `WriteExt` provides small method-style helpers for byte writers. The
17/// methods are implemented for every type that implements [`Write`], including
18/// `dyn Write` trait objects.
19pub trait WriteExt: Write {
20 /// Writes bytes from a range of `buffer` without checking the range bounds
21 /// in release builds.
22 ///
23 /// This method delegates to [`Write::write`] after creating the source
24 /// slice with raw pointer arithmetic. It performs at most one write
25 /// operation and returns the number of bytes written, keeping the same
26 /// short-write and error behavior as [`Write::write`].
27 ///
28 /// # Parameters
29 /// - `buffer`: Source buffer.
30 /// - `start_index`: Start offset inside `buffer`.
31 /// - `count`: Maximum number of bytes to write.
32 ///
33 /// # Returns
34 /// The number of bytes written from `buffer[start_index..start_index +
35 /// count]`. The value is in `0..=count`.
36 ///
37 /// # Errors
38 /// Returns the error reported by [`Write::write`].
39 ///
40 /// # Safety
41 /// The caller must guarantee that `start_index..start_index + count` is a
42 /// valid range within `buffer` and that `start_index + count` does not
43 /// overflow `usize`.
44 unsafe fn write_unchecked(
45 &mut self,
46 buffer: &[u8],
47 start_index: usize,
48 count: usize,
49 ) -> Result<usize> {
50 debug_assert!(
51 start_index
52 .checked_add(count)
53 .is_some_and(|end_index| end_index <= buffer.len()),
54 "unchecked write range exceeds buffer"
55 );
56 // SAFETY: The caller guarantees that the computed pointer and length
57 // form a valid subslice of `buffer`.
58 let source = unsafe {
59 core::slice::from_raw_parts(buffer.as_ptr().add(start_index), count)
60 };
61 self.write(source)
62 }
63
64 /// Writes exactly `count` bytes from a range of `buffer` without checking
65 /// the range bounds in release builds.
66 ///
67 /// This method delegates to [`Write::write_all`] after creating the source
68 /// slice with raw pointer arithmetic. It keeps the same short-write,
69 /// [`std::io::ErrorKind::Interrupted`], and
70 /// [`std::io::ErrorKind::WriteZero`] behavior as [`Write::write_all`].
71 ///
72 /// # Parameters
73 /// - `buffer`: Source buffer.
74 /// - `start_index`: Start offset inside `buffer`.
75 /// - `count`: Number of bytes to write.
76 ///
77 /// # Errors
78 /// Returns the error reported by [`Write::write_all`].
79 ///
80 /// # Safety
81 /// The caller must guarantee that `start_index..start_index + count` is a
82 /// valid range within `buffer` and that `start_index + count` does not
83 /// overflow `usize`.
84 unsafe fn write_all_unchecked(
85 &mut self,
86 buffer: &[u8],
87 start_index: usize,
88 count: usize,
89 ) -> Result<()> {
90 debug_assert!(
91 start_index
92 .checked_add(count)
93 .is_some_and(|end_index| end_index <= buffer.len()),
94 "unchecked write range exceeds buffer"
95 );
96 // SAFETY: The caller guarantees that the computed pointer and length
97 // form a valid subslice of `buffer`.
98 let source = unsafe {
99 core::slice::from_raw_parts(buffer.as_ptr().add(start_index), count)
100 };
101 self.write_all(source)
102 }
103}
104
105impl<T> WriteExt for T where T: Write + ?Sized {}