Skip to main content

qubit_io_binary/stream/
methods.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//! Inherent forwarding methods for stream wrappers.
9
10use std::io::{
11    Result,
12    SeekFrom,
13};
14
15use qubit_io::{
16    Input,
17    Output,
18    Seekable,
19};
20
21use super::{
22    BinaryReader,
23    BinaryWriter,
24    BufferedBinaryReader,
25    BufferedBinaryWriter,
26    BufferedLeb128Reader,
27    BufferedLeb128Writer,
28    BufferedZigZagReader,
29    BufferedZigZagWriter,
30    Leb128Reader,
31    Leb128Writer,
32    ZigZagReader,
33    ZigZagWriter,
34};
35
36macro_rules! impl_input_methods {
37    ($type:ident<$($param:ident),+> where $($bound:tt)*) => {
38        impl<$($param),+> $type<$($param),+>
39        where
40            $($bound)*
41        {
42            /// Returns whether this reader buffers input.
43            #[inline(always)]
44            pub fn is_buffered(&self) -> bool {
45                Input::is_buffered(self)
46            }
47
48            /// Reads up to `count` bytes into an indexed output range.
49            ///
50            /// # Safety
51            ///
52            /// `index..index + count` must be a valid range within `output`.
53            #[inline(always)]
54            pub unsafe fn read_unchecked(
55                &mut self,
56                output: &mut [u8],
57                index: usize,
58                count: usize,
59            ) -> Result<usize> {
60                // SAFETY: The caller upholds the indexed range contract.
61                unsafe { Input::read_unchecked(self, output, index, count) }
62            }
63
64            /// Reads bytes into the full output slice.
65            #[inline(always)]
66            pub fn read(&mut self, output: &mut [u8]) -> Result<usize> {
67                Input::read(self, output)
68            }
69
70            /// Reads bytes into an indexed range until it is full or EOF.
71            ///
72            /// # Safety
73            ///
74            /// `index..index + count` must be a valid range within `output`.
75            #[inline(always)]
76            pub unsafe fn read_fully_unchecked(
77                &mut self,
78                output: &mut [u8],
79                index: usize,
80                count: usize,
81            ) -> Result<usize> {
82                // SAFETY: The caller upholds the indexed range contract.
83                unsafe { Input::read_fully_unchecked(self, output, index, count) }
84            }
85
86            /// Reads bytes into the full output slice until it is full or EOF.
87            #[inline(always)]
88            pub fn read_fully(&mut self, output: &mut [u8]) -> Result<usize> {
89                Input::read_fully(self, output)
90            }
91
92            /// Reads bytes until the full output slice is filled.
93            #[inline(always)]
94            pub fn read_exactly(&mut self, output: &mut [u8]) -> Result<()> {
95                Input::read_exactly(self, output)
96            }
97        }
98    };
99}
100
101macro_rules! impl_output_methods {
102    ($type:ident<$($param:ident),+> where $($bound:tt)*) => {
103        impl<$($param),+> $type<$($param),+>
104        where
105            $($bound)*
106        {
107            /// Returns whether this writer buffers output.
108            #[inline(always)]
109            pub fn is_buffered(&self) -> bool {
110                Output::is_buffered(self)
111            }
112
113            /// Writes up to `count` bytes from an indexed input range.
114            ///
115            /// # Safety
116            ///
117            /// `index..index + count` must be a valid range within `input`.
118            #[inline(always)]
119            pub unsafe fn write_unchecked(
120                &mut self,
121                input: &[u8],
122                index: usize,
123                count: usize,
124            ) -> Result<usize> {
125                // SAFETY: The caller upholds the indexed range contract.
126                unsafe { Output::write_unchecked(self, input, index, count) }
127            }
128
129            /// Writes bytes from the full input slice.
130            #[inline(always)]
131            pub fn write(&mut self, input: &[u8]) -> Result<usize> {
132                Output::write(self, input)
133            }
134
135            /// Writes all bytes from an indexed input range.
136            ///
137            /// # Safety
138            ///
139            /// `index..index + count` must be a valid range within `input`.
140            #[inline(always)]
141            pub unsafe fn write_fully_unchecked(
142                &mut self,
143                input: &[u8],
144                index: usize,
145                count: usize,
146            ) -> Result<()> {
147                // SAFETY: The caller upholds the indexed range contract.
148                unsafe { Output::write_fully_unchecked(self, input, index, count) }
149            }
150
151            /// Writes all bytes from the full input slice.
152            #[inline(always)]
153            pub fn write_fully(&mut self, input: &[u8]) -> Result<()> {
154                Output::write_fully(self, input)
155            }
156
157            /// Flushes buffered output.
158            #[inline(always)]
159            pub fn flush(&mut self) -> Result<()> {
160                Output::flush(self)
161            }
162        }
163    };
164}
165
166macro_rules! impl_seekable_methods {
167    ($type:ident<$($param:ident),+> where $($bound:tt)*) => {
168        impl<$($param),+> $type<$($param),+>
169        where
170            $($bound)*
171        {
172            /// Seeks the wrapped byte stream.
173            #[inline(always)]
174            pub fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
175                Seekable::seek_to(self, position)
176            }
177        }
178    };
179}
180
181impl_input_methods!(BinaryReader<R, O> where R: Input<Item = u8>);
182impl_input_methods!(BufferedBinaryReader<R, O> where R: Input<Item = u8>);
183impl_input_methods!(Leb128Reader<R, P> where R: Input<Item = u8>);
184impl_input_methods!(BufferedLeb128Reader<R, P> where R: Input<Item = u8>);
185impl_input_methods!(ZigZagReader<R, P> where R: Input<Item = u8>);
186impl_input_methods!(BufferedZigZagReader<R, P> where R: Input<Item = u8>);
187
188impl_output_methods!(BinaryWriter<W, O> where W: Output<Item = u8>);
189impl_output_methods!(BufferedBinaryWriter<W, O> where W: Output<Item = u8>);
190impl_output_methods!(Leb128Writer<W> where W: Output<Item = u8>);
191impl_output_methods!(BufferedLeb128Writer<W> where W: Output<Item = u8>);
192impl_output_methods!(ZigZagWriter<W> where W: Output<Item = u8>);
193impl_output_methods!(BufferedZigZagWriter<W> where W: Output<Item = u8>);
194
195impl_seekable_methods!(BinaryReader<R, O> where R: Seekable<Unit = u8>);
196impl_seekable_methods!(BufferedBinaryReader<R, O> where R: Input<Item = u8> + Seekable<Unit = u8>);
197impl_seekable_methods!(Leb128Reader<R, P> where R: Seekable<Unit = u8>);
198impl_seekable_methods!(BufferedLeb128Reader<R, P> where R: Input<Item = u8> + Seekable<Unit = u8>);
199impl_seekable_methods!(ZigZagReader<R, P> where R: Seekable<Unit = u8>);
200impl_seekable_methods!(BufferedZigZagReader<R, P> where R: Input<Item = u8> + Seekable<Unit = u8>);
201impl_seekable_methods!(BinaryWriter<W, O> where W: Seekable<Unit = u8>);
202impl_seekable_methods!(BufferedBinaryWriter<W, O> where W: Output<Item = u8> + Seekable<Unit = u8>);
203impl_seekable_methods!(Leb128Writer<W> where W: Seekable<Unit = u8>);
204impl_seekable_methods!(BufferedLeb128Writer<W> where W: Output<Item = u8> + Seekable<Unit = u8>);
205impl_seekable_methods!(ZigZagWriter<W> where W: Seekable<Unit = u8>);
206impl_seekable_methods!(BufferedZigZagWriter<W> where W: Output<Item = u8> + Seekable<Unit = u8>);