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