/rust/registry/src/index.crates.io-1949cf8c6b5b557f/byteorder-lite-0.1.0/src/io.rs
Line | Count | Source |
1 | | use std::io::{self, Result}; |
2 | | |
3 | | use crate::ByteOrder; |
4 | | |
5 | | /// Extends [`Read`] with methods for reading numbers. (For `std::io`.) |
6 | | /// |
7 | | /// Most of the methods defined here have an unconstrained type parameter that |
8 | | /// must be explicitly instantiated. Typically, it is instantiated with either |
9 | | /// the [`BigEndian`] or [`LittleEndian`] types defined in this crate. |
10 | | /// |
11 | | /// # Examples |
12 | | /// |
13 | | /// Read unsigned 16 bit big-endian integers from a [`Read`]: |
14 | | /// |
15 | | /// ```rust |
16 | | /// use std::io::Cursor; |
17 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
18 | | /// |
19 | | /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]); |
20 | | /// assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap()); |
21 | | /// assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap()); |
22 | | /// ``` |
23 | | /// |
24 | | /// [`BigEndian`]: enum.BigEndian.html |
25 | | /// [`LittleEndian`]: enum.LittleEndian.html |
26 | | /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html |
27 | | pub trait ReadBytesExt: io::Read { |
28 | | /// Reads an unsigned 8 bit integer from the underlying reader. |
29 | | /// |
30 | | /// Note that since this reads a single byte, no byte order conversions |
31 | | /// are used. It is included for completeness. |
32 | | /// |
33 | | /// # Errors |
34 | | /// |
35 | | /// This method returns the same errors as [`Read::read_exact`]. |
36 | | /// |
37 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
38 | | /// |
39 | | /// # Examples |
40 | | /// |
41 | | /// Read unsigned 8 bit integers from a `Read`: |
42 | | /// |
43 | | /// ```rust |
44 | | /// use std::io::Cursor; |
45 | | /// use byteorder_lite::ReadBytesExt; |
46 | | /// |
47 | | /// let mut rdr = Cursor::new(vec![2, 5]); |
48 | | /// assert_eq!(2, rdr.read_u8().unwrap()); |
49 | | /// assert_eq!(5, rdr.read_u8().unwrap()); |
50 | | /// ``` |
51 | | #[inline] |
52 | 16.7M | fn read_u8(&mut self) -> Result<u8> { |
53 | 16.7M | let mut buf = [0; 1]; |
54 | 16.7M | self.read_exact(&mut buf)?; |
55 | 16.7M | Ok(buf[0]) |
56 | 16.7M | } <std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u8 Line | Count | Source | 52 | 16.7M | fn read_u8(&mut self) -> Result<u8> { | 53 | 16.7M | let mut buf = [0; 1]; | 54 | 16.7M | self.read_exact(&mut buf)?; | 55 | 16.7M | Ok(buf[0]) | 56 | 16.7M | } |
<dyn std::io::Read as byteorder_lite::io::ReadBytesExt>::read_u8 Line | Count | Source | 52 | 4.56k | fn read_u8(&mut self) -> Result<u8> { | 53 | 4.56k | let mut buf = [0; 1]; | 54 | 4.56k | self.read_exact(&mut buf)?; | 55 | 4.56k | Ok(buf[0]) | 56 | 4.56k | } |
<std::io::Take<&mut std::io::cursor::Cursor<&[u8]>> as byteorder_lite::io::ReadBytesExt>::read_u8 Line | Count | Source | 52 | 854 | fn read_u8(&mut self) -> Result<u8> { | 53 | 854 | let mut buf = [0; 1]; | 54 | 854 | self.read_exact(&mut buf)?; | 55 | 849 | Ok(buf[0]) | 56 | 854 | } |
Unexecuted instantiation: <_ as byteorder_lite::io::ReadBytesExt>::read_u8 |
57 | | |
58 | | /// Reads a signed 8 bit integer from the underlying reader. |
59 | | /// |
60 | | /// Note that since this reads a single byte, no byte order conversions |
61 | | /// are used. It is included for completeness. |
62 | | /// |
63 | | /// # Errors |
64 | | /// |
65 | | /// This method returns the same errors as [`Read::read_exact`]. |
66 | | /// |
67 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
68 | | /// |
69 | | /// # Examples |
70 | | /// |
71 | | /// Read signed 8 bit integers from a `Read`: |
72 | | /// |
73 | | /// ```rust |
74 | | /// use std::io::Cursor; |
75 | | /// use byteorder_lite::ReadBytesExt; |
76 | | /// |
77 | | /// let mut rdr = Cursor::new(vec![0x02, 0xfb]); |
78 | | /// assert_eq!(2, rdr.read_i8().unwrap()); |
79 | | /// assert_eq!(-5, rdr.read_i8().unwrap()); |
80 | | /// ``` |
81 | | #[inline] |
82 | 0 | fn read_i8(&mut self) -> Result<i8> { |
83 | 0 | let mut buf = [0; 1]; |
84 | 0 | self.read_exact(&mut buf)?; |
85 | 0 | Ok(buf[0] as i8) |
86 | 0 | } |
87 | | |
88 | | /// Reads an unsigned 16 bit integer from the underlying reader. |
89 | | /// |
90 | | /// # Errors |
91 | | /// |
92 | | /// This method returns the same errors as [`Read::read_exact`]. |
93 | | /// |
94 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
95 | | /// |
96 | | /// # Examples |
97 | | /// |
98 | | /// Read unsigned 16 bit big-endian integers from a `Read`: |
99 | | /// |
100 | | /// ```rust |
101 | | /// use std::io::Cursor; |
102 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
103 | | /// |
104 | | /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]); |
105 | | /// assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap()); |
106 | | /// assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap()); |
107 | | /// ``` |
108 | | #[inline] |
109 | 1.96M | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { |
110 | 1.96M | let mut buf = [0; 2]; |
111 | 1.96M | self.read_exact(&mut buf)?; |
112 | 1.96M | Ok(T::read_u16(&buf)) |
113 | 1.96M | } <std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::ReadBytesExt>::read_u16::<byteorder_lite::LittleEndian> Line | Count | Source | 109 | 642 | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { | 110 | 642 | let mut buf = [0; 2]; | 111 | 642 | self.read_exact(&mut buf)?; | 112 | 642 | Ok(T::read_u16(&buf)) | 113 | 642 | } |
<std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u16::<byteorder_lite::LittleEndian> Line | Count | Source | 109 | 1.95M | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { | 110 | 1.95M | let mut buf = [0; 2]; | 111 | 1.95M | self.read_exact(&mut buf)?; | 112 | 1.95M | Ok(T::read_u16(&buf)) | 113 | 1.95M | } |
Unexecuted instantiation: <std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u16::<byteorder_lite::BigEndian> <dyn std::io::Read as byteorder_lite::io::ReadBytesExt>::read_u16::<byteorder_lite::LittleEndian> Line | Count | Source | 109 | 4.56k | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { | 110 | 4.56k | let mut buf = [0; 2]; | 111 | 4.56k | self.read_exact(&mut buf)?; | 112 | 4.56k | Ok(T::read_u16(&buf)) | 113 | 4.56k | } |
<std::io::Take<&mut std::io::cursor::Cursor<&[u8]>> as byteorder_lite::io::ReadBytesExt>::read_u16::<byteorder_lite::LittleEndian> Line | Count | Source | 109 | 2.69k | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { | 110 | 2.69k | let mut buf = [0; 2]; | 111 | 2.69k | self.read_exact(&mut buf)?; | 112 | 2.69k | Ok(T::read_u16(&buf)) | 113 | 2.69k | } |
Unexecuted instantiation: <_ as byteorder_lite::io::ReadBytesExt>::read_u16::<_> |
114 | | |
115 | | /// Reads a signed 16 bit integer from the underlying reader. |
116 | | /// |
117 | | /// # Errors |
118 | | /// |
119 | | /// This method returns the same errors as [`Read::read_exact`]. |
120 | | /// |
121 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
122 | | /// |
123 | | /// # Examples |
124 | | /// |
125 | | /// Read signed 16 bit big-endian integers from a `Read`: |
126 | | /// |
127 | | /// ```rust |
128 | | /// use std::io::Cursor; |
129 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
130 | | /// |
131 | | /// let mut rdr = Cursor::new(vec![0x00, 0xc1, 0xff, 0x7c]); |
132 | | /// assert_eq!(193, rdr.read_i16::<BigEndian>().unwrap()); |
133 | | /// assert_eq!(-132, rdr.read_i16::<BigEndian>().unwrap()); |
134 | | /// ``` |
135 | | #[inline] |
136 | 0 | fn read_i16<T: ByteOrder>(&mut self) -> Result<i16> { |
137 | 0 | let mut buf = [0; 2]; |
138 | 0 | self.read_exact(&mut buf)?; |
139 | 0 | Ok(T::read_i16(&buf)) |
140 | 0 | } |
141 | | |
142 | | /// Reads an unsigned 24 bit integer from the underlying reader. |
143 | | /// |
144 | | /// # Errors |
145 | | /// |
146 | | /// This method returns the same errors as [`Read::read_exact`]. |
147 | | /// |
148 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
149 | | /// |
150 | | /// # Examples |
151 | | /// |
152 | | /// Read unsigned 24 bit big-endian integers from a `Read`: |
153 | | /// |
154 | | /// ```rust |
155 | | /// use std::io::Cursor; |
156 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
157 | | /// |
158 | | /// let mut rdr = Cursor::new(vec![0x00, 0x01, 0x0b]); |
159 | | /// assert_eq!(267, rdr.read_u24::<BigEndian>().unwrap()); |
160 | | /// ``` |
161 | | #[inline] |
162 | 2.32k | fn read_u24<T: ByteOrder>(&mut self) -> Result<u32> { |
163 | 2.32k | let mut buf = [0; 3]; |
164 | 2.32k | self.read_exact(&mut buf)?; |
165 | 2.32k | Ok(T::read_u24(&buf)) |
166 | 2.32k | } <&[u8] as byteorder_lite::io::ReadBytesExt>::read_u24::<byteorder_lite::LittleEndian> Line | Count | Source | 162 | 138 | fn read_u24<T: ByteOrder>(&mut self) -> Result<u32> { | 163 | 138 | let mut buf = [0; 3]; | 164 | 138 | self.read_exact(&mut buf)?; | 165 | 138 | Ok(T::read_u24(&buf)) | 166 | 138 | } |
<std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u24::<byteorder_lite::LittleEndian> Line | Count | Source | 162 | 798 | fn read_u24<T: ByteOrder>(&mut self) -> Result<u32> { | 163 | 798 | let mut buf = [0; 3]; | 164 | 798 | self.read_exact(&mut buf)?; | 165 | 798 | Ok(T::read_u24(&buf)) | 166 | 798 | } |
<std::io::Take<&mut std::io::cursor::Cursor<&[u8]>> as byteorder_lite::io::ReadBytesExt>::read_u24::<byteorder_lite::LittleEndian> Line | Count | Source | 162 | 1.39k | fn read_u24<T: ByteOrder>(&mut self) -> Result<u32> { | 163 | 1.39k | let mut buf = [0; 3]; | 164 | 1.39k | self.read_exact(&mut buf)?; | 165 | 1.39k | Ok(T::read_u24(&buf)) | 166 | 1.39k | } |
Unexecuted instantiation: <_ as byteorder_lite::io::ReadBytesExt>::read_u24::<_> |
167 | | |
168 | | /// Reads a signed 24 bit integer from the underlying reader. |
169 | | /// |
170 | | /// # Errors |
171 | | /// |
172 | | /// This method returns the same errors as [`Read::read_exact`]. |
173 | | /// |
174 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
175 | | /// |
176 | | /// # Examples |
177 | | /// |
178 | | /// Read signed 24 bit big-endian integers from a `Read`: |
179 | | /// |
180 | | /// ```rust |
181 | | /// use std::io::Cursor; |
182 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
183 | | /// |
184 | | /// let mut rdr = Cursor::new(vec![0xff, 0x7a, 0x33]); |
185 | | /// assert_eq!(-34253, rdr.read_i24::<BigEndian>().unwrap()); |
186 | | /// ``` |
187 | | #[inline] |
188 | 0 | fn read_i24<T: ByteOrder>(&mut self) -> Result<i32> { |
189 | 0 | let mut buf = [0; 3]; |
190 | 0 | self.read_exact(&mut buf)?; |
191 | 0 | Ok(T::read_i24(&buf)) |
192 | 0 | } |
193 | | |
194 | | /// Reads an unsigned 32 bit integer from the underlying reader. |
195 | | /// |
196 | | /// # Errors |
197 | | /// |
198 | | /// This method returns the same errors as [`Read::read_exact`]. |
199 | | /// |
200 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
201 | | /// |
202 | | /// # Examples |
203 | | /// |
204 | | /// Read unsigned 32 bit big-endian integers from a `Read`: |
205 | | /// |
206 | | /// ```rust |
207 | | /// use std::io::Cursor; |
208 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
209 | | /// |
210 | | /// let mut rdr = Cursor::new(vec![0x00, 0x00, 0x01, 0x0b]); |
211 | | /// assert_eq!(267, rdr.read_u32::<BigEndian>().unwrap()); |
212 | | /// ``` |
213 | | #[inline] |
214 | 1.40M | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { |
215 | 1.40M | let mut buf = [0; 4]; |
216 | 1.40M | self.read_exact(&mut buf)?; |
217 | 1.40M | Ok(T::read_u32(&buf)) |
218 | 1.40M | } <std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u32::<byteorder_lite::LittleEndian> Line | Count | Source | 214 | 1.37M | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { | 215 | 1.37M | let mut buf = [0; 4]; | 216 | 1.37M | self.read_exact(&mut buf)?; | 217 | 1.37M | Ok(T::read_u32(&buf)) | 218 | 1.37M | } |
Unexecuted instantiation: <std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u32::<byteorder_lite::BigEndian> <&mut std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_u32::<byteorder_lite::LittleEndian> Line | Count | Source | 214 | 28.0k | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { | 215 | 28.0k | let mut buf = [0; 4]; | 216 | 28.0k | self.read_exact(&mut buf)?; | 217 | 27.9k | Ok(T::read_u32(&buf)) | 218 | 28.0k | } |
<dyn std::io::Read as byteorder_lite::io::ReadBytesExt>::read_u32::<byteorder_lite::LittleEndian> Line | Count | Source | 214 | 1.59k | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { | 215 | 1.59k | let mut buf = [0; 4]; | 216 | 1.59k | self.read_exact(&mut buf)?; | 217 | 1.59k | Ok(T::read_u32(&buf)) | 218 | 1.59k | } |
Unexecuted instantiation: <_ as byteorder_lite::io::ReadBytesExt>::read_u32::<_> |
219 | | |
220 | | /// Reads a signed 32 bit integer from the underlying reader. |
221 | | /// |
222 | | /// # Errors |
223 | | /// |
224 | | /// This method returns the same errors as [`Read::read_exact`]. |
225 | | /// |
226 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
227 | | /// |
228 | | /// # Examples |
229 | | /// |
230 | | /// Read signed 32 bit big-endian integers from a `Read`: |
231 | | /// |
232 | | /// ```rust |
233 | | /// use std::io::Cursor; |
234 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
235 | | /// |
236 | | /// let mut rdr = Cursor::new(vec![0xff, 0xff, 0x7a, 0x33]); |
237 | | /// assert_eq!(-34253, rdr.read_i32::<BigEndian>().unwrap()); |
238 | | /// ``` |
239 | | #[inline] |
240 | 6.62k | fn read_i32<T: ByteOrder>(&mut self) -> Result<i32> { |
241 | 6.62k | let mut buf = [0; 4]; |
242 | 6.62k | self.read_exact(&mut buf)?; |
243 | 6.60k | Ok(T::read_i32(&buf)) |
244 | 6.62k | } <std::io::cursor::Cursor<&[u8]> as byteorder_lite::io::ReadBytesExt>::read_i32::<byteorder_lite::LittleEndian> Line | Count | Source | 240 | 6.62k | fn read_i32<T: ByteOrder>(&mut self) -> Result<i32> { | 241 | 6.62k | let mut buf = [0; 4]; | 242 | 6.62k | self.read_exact(&mut buf)?; | 243 | 6.60k | Ok(T::read_i32(&buf)) | 244 | 6.62k | } |
Unexecuted instantiation: <_ as byteorder_lite::io::ReadBytesExt>::read_i32::<_> |
245 | | |
246 | | /// Reads an unsigned 48 bit integer from the underlying reader. |
247 | | /// |
248 | | /// # Errors |
249 | | /// |
250 | | /// This method returns the same errors as [`Read::read_exact`]. |
251 | | /// |
252 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
253 | | /// |
254 | | /// # Examples |
255 | | /// |
256 | | /// Read unsigned 48 bit big-endian integers from a `Read`: |
257 | | /// |
258 | | /// ```rust |
259 | | /// use std::io::Cursor; |
260 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
261 | | /// |
262 | | /// let mut rdr = Cursor::new(vec![0xb6, 0x71, 0x6b, 0xdc, 0x2b, 0x31]); |
263 | | /// assert_eq!(200598257150769, rdr.read_u48::<BigEndian>().unwrap()); |
264 | | /// ``` |
265 | | #[inline] |
266 | 0 | fn read_u48<T: ByteOrder>(&mut self) -> Result<u64> { |
267 | 0 | let mut buf = [0; 6]; |
268 | 0 | self.read_exact(&mut buf)?; |
269 | 0 | Ok(T::read_u48(&buf)) |
270 | 0 | } |
271 | | |
272 | | /// Reads a signed 48 bit integer from the underlying reader. |
273 | | /// |
274 | | /// # Errors |
275 | | /// |
276 | | /// This method returns the same errors as [`Read::read_exact`]. |
277 | | /// |
278 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
279 | | /// |
280 | | /// # Examples |
281 | | /// |
282 | | /// Read signed 48 bit big-endian integers from a `Read`: |
283 | | /// |
284 | | /// ```rust |
285 | | /// use std::io::Cursor; |
286 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
287 | | /// |
288 | | /// let mut rdr = Cursor::new(vec![0x9d, 0x71, 0xab, 0xe7, 0x97, 0x8f]); |
289 | | /// assert_eq!(-108363435763825, rdr.read_i48::<BigEndian>().unwrap()); |
290 | | /// ``` |
291 | | #[inline] |
292 | 0 | fn read_i48<T: ByteOrder>(&mut self) -> Result<i64> { |
293 | 0 | let mut buf = [0; 6]; |
294 | 0 | self.read_exact(&mut buf)?; |
295 | 0 | Ok(T::read_i48(&buf)) |
296 | 0 | } |
297 | | |
298 | | /// Reads an unsigned 64 bit integer from the underlying reader. |
299 | | /// |
300 | | /// # Errors |
301 | | /// |
302 | | /// This method returns the same errors as [`Read::read_exact`]. |
303 | | /// |
304 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
305 | | /// |
306 | | /// # Examples |
307 | | /// |
308 | | /// Read an unsigned 64 bit big-endian integer from a `Read`: |
309 | | /// |
310 | | /// ```rust |
311 | | /// use std::io::Cursor; |
312 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
313 | | /// |
314 | | /// let mut rdr = Cursor::new(vec![0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83]); |
315 | | /// assert_eq!(918733457491587, rdr.read_u64::<BigEndian>().unwrap()); |
316 | | /// ``` |
317 | | #[inline] |
318 | 0 | fn read_u64<T: ByteOrder>(&mut self) -> Result<u64> { |
319 | 0 | let mut buf = [0; 8]; |
320 | 0 | self.read_exact(&mut buf)?; |
321 | 0 | Ok(T::read_u64(&buf)) |
322 | 0 | } |
323 | | |
324 | | /// Reads a signed 64 bit integer from the underlying reader. |
325 | | /// |
326 | | /// # Errors |
327 | | /// |
328 | | /// This method returns the same errors as [`Read::read_exact`]. |
329 | | /// |
330 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
331 | | /// |
332 | | /// # Examples |
333 | | /// |
334 | | /// Read a signed 64 bit big-endian integer from a `Read`: |
335 | | /// |
336 | | /// ```rust |
337 | | /// use std::io::Cursor; |
338 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
339 | | /// |
340 | | /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0]); |
341 | | /// assert_eq!(i64::min_value(), rdr.read_i64::<BigEndian>().unwrap()); |
342 | | /// ``` |
343 | | #[inline] |
344 | 0 | fn read_i64<T: ByteOrder>(&mut self) -> Result<i64> { |
345 | 0 | let mut buf = [0; 8]; |
346 | 0 | self.read_exact(&mut buf)?; |
347 | 0 | Ok(T::read_i64(&buf)) |
348 | 0 | } |
349 | | |
350 | | /// Reads an unsigned 128 bit integer from the underlying reader. |
351 | | /// |
352 | | /// # Errors |
353 | | /// |
354 | | /// This method returns the same errors as [`Read::read_exact`]. |
355 | | /// |
356 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
357 | | /// |
358 | | /// # Examples |
359 | | /// |
360 | | /// Read an unsigned 128 bit big-endian integer from a `Read`: |
361 | | /// |
362 | | /// ```rust |
363 | | /// use std::io::Cursor; |
364 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
365 | | /// |
366 | | /// let mut rdr = Cursor::new(vec![ |
367 | | /// 0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83, |
368 | | /// 0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83 |
369 | | /// ]); |
370 | | /// assert_eq!(16947640962301618749969007319746179, rdr.read_u128::<BigEndian>().unwrap()); |
371 | | /// ``` |
372 | | #[inline] |
373 | 0 | fn read_u128<T: ByteOrder>(&mut self) -> Result<u128> { |
374 | 0 | let mut buf = [0; 16]; |
375 | 0 | self.read_exact(&mut buf)?; |
376 | 0 | Ok(T::read_u128(&buf)) |
377 | 0 | } |
378 | | |
379 | | /// Reads a signed 128 bit integer from the underlying reader. |
380 | | /// |
381 | | /// # Errors |
382 | | /// |
383 | | /// This method returns the same errors as [`Read::read_exact`]. |
384 | | /// |
385 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
386 | | /// |
387 | | /// # Examples |
388 | | /// |
389 | | /// Read a signed 128 bit big-endian integer from a `Read`: |
390 | | /// |
391 | | /// ```rust |
392 | | /// use std::io::Cursor; |
393 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
394 | | /// |
395 | | /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); |
396 | | /// assert_eq!(i128::min_value(), rdr.read_i128::<BigEndian>().unwrap()); |
397 | | /// ``` |
398 | | #[inline] |
399 | 0 | fn read_i128<T: ByteOrder>(&mut self) -> Result<i128> { |
400 | 0 | let mut buf = [0; 16]; |
401 | 0 | self.read_exact(&mut buf)?; |
402 | 0 | Ok(T::read_i128(&buf)) |
403 | 0 | } |
404 | | |
405 | | /// Reads an unsigned n-bytes integer from the underlying reader. |
406 | | /// |
407 | | /// # Errors |
408 | | /// |
409 | | /// This method returns the same errors as [`Read::read_exact`]. |
410 | | /// |
411 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
412 | | /// |
413 | | /// # Examples |
414 | | /// |
415 | | /// Read an unsigned n-byte big-endian integer from a `Read`: |
416 | | /// |
417 | | /// ```rust |
418 | | /// use std::io::Cursor; |
419 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
420 | | /// |
421 | | /// let mut rdr = Cursor::new(vec![0x80, 0x74, 0xfa]); |
422 | | /// assert_eq!(8418554, rdr.read_uint::<BigEndian>(3).unwrap()); |
423 | | #[inline] |
424 | 0 | fn read_uint<T: ByteOrder>(&mut self, nbytes: usize) -> Result<u64> { |
425 | 0 | let mut buf = [0; 8]; |
426 | 0 | self.read_exact(&mut buf[..nbytes])?; |
427 | 0 | Ok(T::read_uint(&buf[..nbytes], nbytes)) |
428 | 0 | } |
429 | | |
430 | | /// Reads a signed n-bytes integer from the underlying reader. |
431 | | /// |
432 | | /// # Errors |
433 | | /// |
434 | | /// This method returns the same errors as [`Read::read_exact`]. |
435 | | /// |
436 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
437 | | /// |
438 | | /// # Examples |
439 | | /// |
440 | | /// Read an unsigned n-byte big-endian integer from a `Read`: |
441 | | /// |
442 | | /// ```rust |
443 | | /// use std::io::Cursor; |
444 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
445 | | /// |
446 | | /// let mut rdr = Cursor::new(vec![0xc1, 0xff, 0x7c]); |
447 | | /// assert_eq!(-4063364, rdr.read_int::<BigEndian>(3).unwrap()); |
448 | | #[inline] |
449 | 0 | fn read_int<T: ByteOrder>(&mut self, nbytes: usize) -> Result<i64> { |
450 | 0 | let mut buf = [0; 8]; |
451 | 0 | self.read_exact(&mut buf[..nbytes])?; |
452 | 0 | Ok(T::read_int(&buf[..nbytes], nbytes)) |
453 | 0 | } |
454 | | |
455 | | /// Reads an unsigned n-bytes integer from the underlying reader. |
456 | | #[inline] |
457 | 0 | fn read_uint128<T: ByteOrder>(&mut self, nbytes: usize) -> Result<u128> { |
458 | 0 | let mut buf = [0; 16]; |
459 | 0 | self.read_exact(&mut buf[..nbytes])?; |
460 | 0 | Ok(T::read_uint128(&buf[..nbytes], nbytes)) |
461 | 0 | } |
462 | | |
463 | | /// Reads a signed n-bytes integer from the underlying reader. |
464 | | #[inline] |
465 | 0 | fn read_int128<T: ByteOrder>(&mut self, nbytes: usize) -> Result<i128> { |
466 | 0 | let mut buf = [0; 16]; |
467 | 0 | self.read_exact(&mut buf[..nbytes])?; |
468 | 0 | Ok(T::read_int128(&buf[..nbytes], nbytes)) |
469 | 0 | } |
470 | | |
471 | | /// Reads a IEEE754 single-precision (4 bytes) floating point number from |
472 | | /// the underlying reader. |
473 | | /// |
474 | | /// # Errors |
475 | | /// |
476 | | /// This method returns the same errors as [`Read::read_exact`]. |
477 | | /// |
478 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
479 | | /// |
480 | | /// # Examples |
481 | | /// |
482 | | /// Read a big-endian single-precision floating point number from a `Read`: |
483 | | /// |
484 | | /// ```rust |
485 | | /// use std::f32; |
486 | | /// use std::io::Cursor; |
487 | | /// |
488 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
489 | | /// |
490 | | /// let mut rdr = Cursor::new(vec![ |
491 | | /// 0x40, 0x49, 0x0f, 0xdb, |
492 | | /// ]); |
493 | | /// assert_eq!(f32::consts::PI, rdr.read_f32::<BigEndian>().unwrap()); |
494 | | /// ``` |
495 | | #[inline] |
496 | 0 | fn read_f32<T: ByteOrder>(&mut self) -> Result<f32> { |
497 | 0 | let mut buf = [0; 4]; |
498 | 0 | self.read_exact(&mut buf)?; |
499 | 0 | Ok(T::read_f32(&buf)) |
500 | 0 | } |
501 | | |
502 | | /// Reads a IEEE754 double-precision (8 bytes) floating point number from |
503 | | /// the underlying reader. |
504 | | /// |
505 | | /// # Errors |
506 | | /// |
507 | | /// This method returns the same errors as [`Read::read_exact`]. |
508 | | /// |
509 | | /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact |
510 | | /// |
511 | | /// # Examples |
512 | | /// |
513 | | /// Read a big-endian double-precision floating point number from a `Read`: |
514 | | /// |
515 | | /// ```rust |
516 | | /// use std::f64; |
517 | | /// use std::io::Cursor; |
518 | | /// |
519 | | /// use byteorder_lite::{BigEndian, ReadBytesExt}; |
520 | | /// |
521 | | /// let mut rdr = Cursor::new(vec![ |
522 | | /// 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18, |
523 | | /// ]); |
524 | | /// assert_eq!(f64::consts::PI, rdr.read_f64::<BigEndian>().unwrap()); |
525 | | /// ``` |
526 | | #[inline] |
527 | 0 | fn read_f64<T: ByteOrder>(&mut self) -> Result<f64> { |
528 | 0 | let mut buf = [0; 8]; |
529 | 0 | self.read_exact(&mut buf)?; |
530 | 0 | Ok(T::read_f64(&buf)) |
531 | 0 | } |
532 | | } |
533 | | |
534 | | /// All types that implement `Read` get methods defined in `ReadBytesExt` |
535 | | /// for free. |
536 | | impl<R: io::Read + ?Sized> ReadBytesExt for R {} |
537 | | |
538 | | /// Extends [`Write`] with methods for writing numbers. (For `std::io`.) |
539 | | /// |
540 | | /// Most of the methods defined here have an unconstrained type parameter that |
541 | | /// must be explicitly instantiated. Typically, it is instantiated with either |
542 | | /// the [`BigEndian`] or [`LittleEndian`] types defined in this crate. |
543 | | /// |
544 | | /// # Examples |
545 | | /// |
546 | | /// Write unsigned 16 bit big-endian integers to a [`Write`]: |
547 | | /// |
548 | | /// ```rust |
549 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
550 | | /// |
551 | | /// let mut wtr = vec![]; |
552 | | /// wtr.write_u16::<BigEndian>(517).unwrap(); |
553 | | /// wtr.write_u16::<BigEndian>(768).unwrap(); |
554 | | /// assert_eq!(wtr, vec![2, 5, 3, 0]); |
555 | | /// ``` |
556 | | /// |
557 | | /// [`BigEndian`]: enum.BigEndian.html |
558 | | /// [`LittleEndian`]: enum.LittleEndian.html |
559 | | /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html |
560 | | pub trait WriteBytesExt: io::Write { |
561 | | /// Writes an unsigned 8 bit integer to the underlying writer. |
562 | | /// |
563 | | /// Note that since this writes a single byte, no byte order conversions |
564 | | /// are used. It is included for completeness. |
565 | | /// |
566 | | /// # Errors |
567 | | /// |
568 | | /// This method returns the same errors as [`Write::write_all`]. |
569 | | /// |
570 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
571 | | /// |
572 | | /// # Examples |
573 | | /// |
574 | | /// Write unsigned 8 bit integers to a `Write`: |
575 | | /// |
576 | | /// ```rust |
577 | | /// use byteorder_lite::WriteBytesExt; |
578 | | /// |
579 | | /// let mut wtr = Vec::new(); |
580 | | /// wtr.write_u8(2).unwrap(); |
581 | | /// wtr.write_u8(5).unwrap(); |
582 | | /// assert_eq!(wtr, b"\x02\x05"); |
583 | | /// ``` |
584 | | #[inline] |
585 | 0 | fn write_u8(&mut self, n: u8) -> Result<()> { |
586 | 0 | self.write_all(&[n]) |
587 | 0 | } Unexecuted instantiation: <dyn std::io::Write as byteorder_lite::io::WriteBytesExt>::write_u8 Unexecuted instantiation: <_ as byteorder_lite::io::WriteBytesExt>::write_u8 Unexecuted instantiation: <std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_u8 Unexecuted instantiation: <&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_u8 |
588 | | |
589 | | /// Writes a signed 8 bit integer to the underlying writer. |
590 | | /// |
591 | | /// Note that since this writes a single byte, no byte order conversions |
592 | | /// are used. It is included for completeness. |
593 | | /// |
594 | | /// # Errors |
595 | | /// |
596 | | /// This method returns the same errors as [`Write::write_all`]. |
597 | | /// |
598 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
599 | | /// |
600 | | /// # Examples |
601 | | /// |
602 | | /// Write signed 8 bit integers to a `Write`: |
603 | | /// |
604 | | /// ```rust |
605 | | /// use byteorder_lite::WriteBytesExt; |
606 | | /// |
607 | | /// let mut wtr = Vec::new(); |
608 | | /// wtr.write_i8(2).unwrap(); |
609 | | /// wtr.write_i8(-5).unwrap(); |
610 | | /// assert_eq!(wtr, b"\x02\xfb"); |
611 | | /// ``` |
612 | | #[inline] |
613 | 0 | fn write_i8(&mut self, n: i8) -> Result<()> { |
614 | 0 | self.write_all(&[n as u8]) |
615 | 0 | } |
616 | | |
617 | | /// Writes an unsigned 16 bit integer to the underlying writer. |
618 | | /// |
619 | | /// # Errors |
620 | | /// |
621 | | /// This method returns the same errors as [`Write::write_all`]. |
622 | | /// |
623 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
624 | | /// |
625 | | /// # Examples |
626 | | /// |
627 | | /// Write unsigned 16 bit big-endian integers to a `Write`: |
628 | | /// |
629 | | /// ```rust |
630 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
631 | | /// |
632 | | /// let mut wtr = Vec::new(); |
633 | | /// wtr.write_u16::<BigEndian>(517).unwrap(); |
634 | | /// wtr.write_u16::<BigEndian>(768).unwrap(); |
635 | | /// assert_eq!(wtr, b"\x02\x05\x03\x00"); |
636 | | /// ``` |
637 | | #[inline] |
638 | 0 | fn write_u16<T: ByteOrder>(&mut self, n: u16) -> Result<()> { |
639 | 0 | let mut buf = [0; 2]; |
640 | 0 | T::write_u16(&mut buf, n); |
641 | 0 | self.write_all(&buf) |
642 | 0 | } Unexecuted instantiation: <std::io::cursor::Cursor<&mut [u8]> as byteorder_lite::io::WriteBytesExt>::write_u16::<byteorder_lite::LittleEndian> Unexecuted instantiation: <std::io::cursor::Cursor<&mut [u8]> as byteorder_lite::io::WriteBytesExt>::write_u16::<byteorder_lite::BigEndian> Unexecuted instantiation: <dyn std::io::Write as byteorder_lite::io::WriteBytesExt>::write_u16::<byteorder_lite::LittleEndian> Unexecuted instantiation: <dyn std::io::Write as byteorder_lite::io::WriteBytesExt>::write_u16::<byteorder_lite::BigEndian> Unexecuted instantiation: <_ as byteorder_lite::io::WriteBytesExt>::write_u16::<_> Unexecuted instantiation: <std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_u16::<byteorder_lite::LittleEndian> Unexecuted instantiation: <&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_u16::<byteorder_lite::LittleEndian> |
643 | | |
644 | | /// Writes a signed 16 bit integer to the underlying writer. |
645 | | /// |
646 | | /// # Errors |
647 | | /// |
648 | | /// This method returns the same errors as [`Write::write_all`]. |
649 | | /// |
650 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
651 | | /// |
652 | | /// # Examples |
653 | | /// |
654 | | /// Write signed 16 bit big-endian integers to a `Write`: |
655 | | /// |
656 | | /// ```rust |
657 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
658 | | /// |
659 | | /// let mut wtr = Vec::new(); |
660 | | /// wtr.write_i16::<BigEndian>(193).unwrap(); |
661 | | /// wtr.write_i16::<BigEndian>(-132).unwrap(); |
662 | | /// assert_eq!(wtr, b"\x00\xc1\xff\x7c"); |
663 | | /// ``` |
664 | | #[inline] |
665 | 0 | fn write_i16<T: ByteOrder>(&mut self, n: i16) -> Result<()> { |
666 | 0 | let mut buf = [0; 2]; |
667 | 0 | T::write_i16(&mut buf, n); |
668 | 0 | self.write_all(&buf) |
669 | 0 | } |
670 | | |
671 | | /// Writes an unsigned 24 bit integer to the underlying writer. |
672 | | /// |
673 | | /// # Errors |
674 | | /// |
675 | | /// This method returns the same errors as [`Write::write_all`]. |
676 | | /// |
677 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
678 | | /// |
679 | | /// # Examples |
680 | | /// |
681 | | /// Write unsigned 24 bit big-endian integers to a `Write`: |
682 | | /// |
683 | | /// ```rust |
684 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
685 | | /// |
686 | | /// let mut wtr = Vec::new(); |
687 | | /// wtr.write_u24::<BigEndian>(267).unwrap(); |
688 | | /// wtr.write_u24::<BigEndian>(120111).unwrap(); |
689 | | /// assert_eq!(wtr, b"\x00\x01\x0b\x01\xd5\x2f"); |
690 | | /// ``` |
691 | | #[inline] |
692 | 0 | fn write_u24<T: ByteOrder>(&mut self, n: u32) -> Result<()> { |
693 | 0 | let mut buf = [0; 3]; |
694 | 0 | T::write_u24(&mut buf, n); |
695 | 0 | self.write_all(&buf) |
696 | 0 | } |
697 | | |
698 | | /// Writes a signed 24 bit integer to the underlying writer. |
699 | | /// |
700 | | /// # Errors |
701 | | /// |
702 | | /// This method returns the same errors as [`Write::write_all`]. |
703 | | /// |
704 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
705 | | /// |
706 | | /// # Examples |
707 | | /// |
708 | | /// Write signed 24 bit big-endian integers to a `Write`: |
709 | | /// |
710 | | /// ```rust |
711 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
712 | | /// |
713 | | /// let mut wtr = Vec::new(); |
714 | | /// wtr.write_i24::<BigEndian>(-34253).unwrap(); |
715 | | /// wtr.write_i24::<BigEndian>(120111).unwrap(); |
716 | | /// assert_eq!(wtr, b"\xff\x7a\x33\x01\xd5\x2f"); |
717 | | /// ``` |
718 | | #[inline] |
719 | 0 | fn write_i24<T: ByteOrder>(&mut self, n: i32) -> Result<()> { |
720 | 0 | let mut buf = [0; 3]; |
721 | 0 | T::write_i24(&mut buf, n); |
722 | 0 | self.write_all(&buf) |
723 | 0 | } |
724 | | |
725 | | /// Writes an unsigned 32 bit integer to the underlying writer. |
726 | | /// |
727 | | /// # Errors |
728 | | /// |
729 | | /// This method returns the same errors as [`Write::write_all`]. |
730 | | /// |
731 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
732 | | /// |
733 | | /// # Examples |
734 | | /// |
735 | | /// Write unsigned 32 bit big-endian integers to a `Write`: |
736 | | /// |
737 | | /// ```rust |
738 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
739 | | /// |
740 | | /// let mut wtr = Vec::new(); |
741 | | /// wtr.write_u32::<BigEndian>(267).unwrap(); |
742 | | /// wtr.write_u32::<BigEndian>(1205419366).unwrap(); |
743 | | /// assert_eq!(wtr, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66"); |
744 | | /// ``` |
745 | | #[inline] |
746 | 0 | fn write_u32<T: ByteOrder>(&mut self, n: u32) -> Result<()> { |
747 | 0 | let mut buf = [0; 4]; |
748 | 0 | T::write_u32(&mut buf, n); |
749 | 0 | self.write_all(&buf) |
750 | 0 | } Unexecuted instantiation: <_ as byteorder_lite::io::WriteBytesExt>::write_u32::<_> Unexecuted instantiation: <std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_u32::<byteorder_lite::LittleEndian> Unexecuted instantiation: <&mut std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_u32::<byteorder_lite::LittleEndian> |
751 | | |
752 | | /// Writes a signed 32 bit integer to the underlying writer. |
753 | | /// |
754 | | /// # Errors |
755 | | /// |
756 | | /// This method returns the same errors as [`Write::write_all`]. |
757 | | /// |
758 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
759 | | /// |
760 | | /// # Examples |
761 | | /// |
762 | | /// Write signed 32 bit big-endian integers to a `Write`: |
763 | | /// |
764 | | /// ```rust |
765 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
766 | | /// |
767 | | /// let mut wtr = Vec::new(); |
768 | | /// wtr.write_i32::<BigEndian>(-34253).unwrap(); |
769 | | /// wtr.write_i32::<BigEndian>(1205419366).unwrap(); |
770 | | /// assert_eq!(wtr, b"\xff\xff\x7a\x33\x47\xd9\x3d\x66"); |
771 | | /// ``` |
772 | | #[inline] |
773 | 0 | fn write_i32<T: ByteOrder>(&mut self, n: i32) -> Result<()> { |
774 | 0 | let mut buf = [0; 4]; |
775 | 0 | T::write_i32(&mut buf, n); |
776 | 0 | self.write_all(&buf) |
777 | 0 | } Unexecuted instantiation: <_ as byteorder_lite::io::WriteBytesExt>::write_i32::<_> Unexecuted instantiation: <std::io::cursor::Cursor<alloc::vec::Vec<u8>> as byteorder_lite::io::WriteBytesExt>::write_i32::<byteorder_lite::LittleEndian> |
778 | | |
779 | | /// Writes an unsigned 48 bit integer to the underlying writer. |
780 | | /// |
781 | | /// # Errors |
782 | | /// |
783 | | /// This method returns the same errors as [`Write::write_all`]. |
784 | | /// |
785 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
786 | | /// |
787 | | /// # Examples |
788 | | /// |
789 | | /// Write unsigned 48 bit big-endian integers to a `Write`: |
790 | | /// |
791 | | /// ```rust |
792 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
793 | | /// |
794 | | /// let mut wtr = Vec::new(); |
795 | | /// wtr.write_u48::<BigEndian>(52360336390828).unwrap(); |
796 | | /// wtr.write_u48::<BigEndian>(541).unwrap(); |
797 | | /// assert_eq!(wtr, b"\x2f\x9f\x17\x40\x3a\xac\x00\x00\x00\x00\x02\x1d"); |
798 | | /// ``` |
799 | | #[inline] |
800 | 0 | fn write_u48<T: ByteOrder>(&mut self, n: u64) -> Result<()> { |
801 | 0 | let mut buf = [0; 6]; |
802 | 0 | T::write_u48(&mut buf, n); |
803 | 0 | self.write_all(&buf) |
804 | 0 | } |
805 | | |
806 | | /// Writes a signed 48 bit integer to the underlying writer. |
807 | | /// |
808 | | /// # Errors |
809 | | /// |
810 | | /// This method returns the same errors as [`Write::write_all`]. |
811 | | /// |
812 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
813 | | /// |
814 | | /// # Examples |
815 | | /// |
816 | | /// Write signed 48 bit big-endian integers to a `Write`: |
817 | | /// |
818 | | /// ```rust |
819 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
820 | | /// |
821 | | /// let mut wtr = Vec::new(); |
822 | | /// wtr.write_i48::<BigEndian>(-108363435763825).unwrap(); |
823 | | /// wtr.write_i48::<BigEndian>(77).unwrap(); |
824 | | /// assert_eq!(wtr, b"\x9d\x71\xab\xe7\x97\x8f\x00\x00\x00\x00\x00\x4d"); |
825 | | /// ``` |
826 | | #[inline] |
827 | 0 | fn write_i48<T: ByteOrder>(&mut self, n: i64) -> Result<()> { |
828 | 0 | let mut buf = [0; 6]; |
829 | 0 | T::write_i48(&mut buf, n); |
830 | 0 | self.write_all(&buf) |
831 | 0 | } |
832 | | |
833 | | /// Writes an unsigned 64 bit integer to the underlying writer. |
834 | | /// |
835 | | /// # Errors |
836 | | /// |
837 | | /// This method returns the same errors as [`Write::write_all`]. |
838 | | /// |
839 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
840 | | /// |
841 | | /// # Examples |
842 | | /// |
843 | | /// Write unsigned 64 bit big-endian integers to a `Write`: |
844 | | /// |
845 | | /// ```rust |
846 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
847 | | /// |
848 | | /// let mut wtr = Vec::new(); |
849 | | /// wtr.write_u64::<BigEndian>(918733457491587).unwrap(); |
850 | | /// wtr.write_u64::<BigEndian>(143).unwrap(); |
851 | | /// assert_eq!(wtr, b"\x00\x03\x43\x95\x4d\x60\x86\x83\x00\x00\x00\x00\x00\x00\x00\x8f"); |
852 | | /// ``` |
853 | | #[inline] |
854 | 0 | fn write_u64<T: ByteOrder>(&mut self, n: u64) -> Result<()> { |
855 | 0 | let mut buf = [0; 8]; |
856 | 0 | T::write_u64(&mut buf, n); |
857 | 0 | self.write_all(&buf) |
858 | 0 | } |
859 | | |
860 | | /// Writes a signed 64 bit integer to the underlying writer. |
861 | | /// |
862 | | /// # Errors |
863 | | /// |
864 | | /// This method returns the same errors as [`Write::write_all`]. |
865 | | /// |
866 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
867 | | /// |
868 | | /// # Examples |
869 | | /// |
870 | | /// Write signed 64 bit big-endian integers to a `Write`: |
871 | | /// |
872 | | /// ```rust |
873 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
874 | | /// |
875 | | /// let mut wtr = Vec::new(); |
876 | | /// wtr.write_i64::<BigEndian>(i64::min_value()).unwrap(); |
877 | | /// wtr.write_i64::<BigEndian>(i64::max_value()).unwrap(); |
878 | | /// assert_eq!(wtr, b"\x80\x00\x00\x00\x00\x00\x00\x00\x7f\xff\xff\xff\xff\xff\xff\xff"); |
879 | | /// ``` |
880 | | #[inline] |
881 | 0 | fn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<()> { |
882 | 0 | let mut buf = [0; 8]; |
883 | 0 | T::write_i64(&mut buf, n); |
884 | 0 | self.write_all(&buf) |
885 | 0 | } |
886 | | |
887 | | /// Writes an unsigned 128 bit integer to the underlying writer. |
888 | | #[inline] |
889 | 0 | fn write_u128<T: ByteOrder>(&mut self, n: u128) -> Result<()> { |
890 | 0 | let mut buf = [0; 16]; |
891 | 0 | T::write_u128(&mut buf, n); |
892 | 0 | self.write_all(&buf) |
893 | 0 | } |
894 | | |
895 | | /// Writes a signed 128 bit integer to the underlying writer. |
896 | | #[inline] |
897 | 0 | fn write_i128<T: ByteOrder>(&mut self, n: i128) -> Result<()> { |
898 | 0 | let mut buf = [0; 16]; |
899 | 0 | T::write_i128(&mut buf, n); |
900 | 0 | self.write_all(&buf) |
901 | 0 | } |
902 | | |
903 | | /// Writes an unsigned n-bytes integer to the underlying writer. |
904 | | /// |
905 | | /// # Errors |
906 | | /// |
907 | | /// This method returns the same errors as [`Write::write_all`]. |
908 | | /// |
909 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
910 | | /// |
911 | | /// # Panics |
912 | | /// |
913 | | /// If the given integer is not representable in the given number of bytes, |
914 | | /// this method panics. If `nbytes > 8`, this method panics. |
915 | | /// |
916 | | /// # Examples |
917 | | /// |
918 | | /// Write unsigned 40 bit big-endian integers to a `Write`: |
919 | | /// |
920 | | /// ```rust |
921 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
922 | | /// |
923 | | /// let mut wtr = Vec::new(); |
924 | | /// wtr.write_uint::<BigEndian>(312550384361, 5).unwrap(); |
925 | | /// wtr.write_uint::<BigEndian>(43, 5).unwrap(); |
926 | | /// assert_eq!(wtr, b"\x48\xc5\x74\x62\xe9\x00\x00\x00\x00\x2b"); |
927 | | /// ``` |
928 | | #[inline] |
929 | 0 | fn write_uint<T: ByteOrder>( |
930 | 0 | &mut self, |
931 | 0 | n: u64, |
932 | 0 | nbytes: usize, |
933 | 0 | ) -> Result<()> { |
934 | 0 | let mut buf = [0; 8]; |
935 | 0 | T::write_uint(&mut buf, n, nbytes); |
936 | 0 | self.write_all(&buf[0..nbytes]) |
937 | 0 | } |
938 | | |
939 | | /// Writes a signed n-bytes integer to the underlying writer. |
940 | | /// |
941 | | /// # Errors |
942 | | /// |
943 | | /// This method returns the same errors as [`Write::write_all`]. |
944 | | /// |
945 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
946 | | /// |
947 | | /// # Panics |
948 | | /// |
949 | | /// If the given integer is not representable in the given number of bytes, |
950 | | /// this method panics. If `nbytes > 8`, this method panics. |
951 | | /// |
952 | | /// # Examples |
953 | | /// |
954 | | /// Write signed 56 bit big-endian integers to a `Write`: |
955 | | /// |
956 | | /// ```rust |
957 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
958 | | /// |
959 | | /// let mut wtr = Vec::new(); |
960 | | /// wtr.write_int::<BigEndian>(-3548172039376767, 7).unwrap(); |
961 | | /// wtr.write_int::<BigEndian>(43, 7).unwrap(); |
962 | | /// assert_eq!(wtr, b"\xf3\x64\xf4\xd1\xfd\xb0\x81\x00\x00\x00\x00\x00\x00\x2b"); |
963 | | /// ``` |
964 | | #[inline] |
965 | 0 | fn write_int<T: ByteOrder>( |
966 | 0 | &mut self, |
967 | 0 | n: i64, |
968 | 0 | nbytes: usize, |
969 | 0 | ) -> Result<()> { |
970 | 0 | let mut buf = [0; 8]; |
971 | 0 | T::write_int(&mut buf, n, nbytes); |
972 | 0 | self.write_all(&buf[0..nbytes]) |
973 | 0 | } |
974 | | |
975 | | /// Writes an unsigned n-bytes integer to the underlying writer. |
976 | | /// |
977 | | /// If the given integer is not representable in the given number of bytes, |
978 | | /// this method panics. If `nbytes > 16`, this method panics. |
979 | | #[inline] |
980 | 0 | fn write_uint128<T: ByteOrder>( |
981 | 0 | &mut self, |
982 | 0 | n: u128, |
983 | 0 | nbytes: usize, |
984 | 0 | ) -> Result<()> { |
985 | 0 | let mut buf = [0; 16]; |
986 | 0 | T::write_uint128(&mut buf, n, nbytes); |
987 | 0 | self.write_all(&buf[0..nbytes]) |
988 | 0 | } |
989 | | |
990 | | /// Writes a signed n-bytes integer to the underlying writer. |
991 | | /// |
992 | | /// If the given integer is not representable in the given number of bytes, |
993 | | /// this method panics. If `nbytes > 16`, this method panics. |
994 | | #[inline] |
995 | 0 | fn write_int128<T: ByteOrder>( |
996 | 0 | &mut self, |
997 | 0 | n: i128, |
998 | 0 | nbytes: usize, |
999 | 0 | ) -> Result<()> { |
1000 | 0 | let mut buf = [0; 16]; |
1001 | 0 | T::write_int128(&mut buf, n, nbytes); |
1002 | 0 | self.write_all(&buf[0..nbytes]) |
1003 | 0 | } |
1004 | | |
1005 | | /// Writes a IEEE754 single-precision (4 bytes) floating point number to |
1006 | | /// the underlying writer. |
1007 | | /// |
1008 | | /// # Errors |
1009 | | /// |
1010 | | /// This method returns the same errors as [`Write::write_all`]. |
1011 | | /// |
1012 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
1013 | | /// |
1014 | | /// # Examples |
1015 | | /// |
1016 | | /// Write a big-endian single-precision floating point number to a `Write`: |
1017 | | /// |
1018 | | /// ```rust |
1019 | | /// use std::f32; |
1020 | | /// |
1021 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
1022 | | /// |
1023 | | /// let mut wtr = Vec::new(); |
1024 | | /// wtr.write_f32::<BigEndian>(f32::consts::PI).unwrap(); |
1025 | | /// assert_eq!(wtr, b"\x40\x49\x0f\xdb"); |
1026 | | /// ``` |
1027 | | #[inline] |
1028 | 0 | fn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<()> { |
1029 | 0 | let mut buf = [0; 4]; |
1030 | 0 | T::write_f32(&mut buf, n); |
1031 | 0 | self.write_all(&buf) |
1032 | 0 | } |
1033 | | |
1034 | | /// Writes a IEEE754 double-precision (8 bytes) floating point number to |
1035 | | /// the underlying writer. |
1036 | | /// |
1037 | | /// # Errors |
1038 | | /// |
1039 | | /// This method returns the same errors as [`Write::write_all`]. |
1040 | | /// |
1041 | | /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all |
1042 | | /// |
1043 | | /// # Examples |
1044 | | /// |
1045 | | /// Write a big-endian double-precision floating point number to a `Write`: |
1046 | | /// |
1047 | | /// ```rust |
1048 | | /// use std::f64; |
1049 | | /// |
1050 | | /// use byteorder_lite::{BigEndian, WriteBytesExt}; |
1051 | | /// |
1052 | | /// let mut wtr = Vec::new(); |
1053 | | /// wtr.write_f64::<BigEndian>(f64::consts::PI).unwrap(); |
1054 | | /// assert_eq!(wtr, b"\x40\x09\x21\xfb\x54\x44\x2d\x18"); |
1055 | | /// ``` |
1056 | | #[inline] |
1057 | 0 | fn write_f64<T: ByteOrder>(&mut self, n: f64) -> Result<()> { |
1058 | 0 | let mut buf = [0; 8]; |
1059 | 0 | T::write_f64(&mut buf, n); |
1060 | 0 | self.write_all(&buf) |
1061 | 0 | } |
1062 | | } |
1063 | | |
1064 | | /// All types that implement `Write` get methods defined in `WriteBytesExt` |
1065 | | /// for free. |
1066 | | impl<W: io::Write + ?Sized> WriteBytesExt for W {} |