| 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 | fn read_u8(&mut self) -> Result<u8> { |
| 53 | let mut buf = [0; 1]; |
| 54 | self.read_exact(&mut buf)?; |
| 55 | Ok(buf[0]) |
| 56 | } |
| 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 | fn read_i8(&mut self) -> Result<i8> { |
| 83 | let mut buf = [0; 1]; |
| 84 | self.read_exact(&mut buf)?; |
| 85 | Ok(buf[0] as i8) |
| 86 | } |
| 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 | fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> { |
| 110 | let mut buf = [0; 2]; |
| 111 | self.read_exact(&mut buf)?; |
| 112 | Ok(T::read_u16(&buf)) |
| 113 | } |
| 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 | fn read_i16<T: ByteOrder>(&mut self) -> Result<i16> { |
| 137 | let mut buf = [0; 2]; |
| 138 | self.read_exact(&mut buf)?; |
| 139 | Ok(T::read_i16(&buf)) |
| 140 | } |
| 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 | fn read_u24<T: ByteOrder>(&mut self) -> Result<u32> { |
| 163 | let mut buf = [0; 3]; |
| 164 | self.read_exact(&mut buf)?; |
| 165 | Ok(T::read_u24(&buf)) |
| 166 | } |
| 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 | fn read_i24<T: ByteOrder>(&mut self) -> Result<i32> { |
| 189 | let mut buf = [0; 3]; |
| 190 | self.read_exact(&mut buf)?; |
| 191 | Ok(T::read_i24(&buf)) |
| 192 | } |
| 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 | fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> { |
| 215 | let mut buf = [0; 4]; |
| 216 | self.read_exact(&mut buf)?; |
| 217 | Ok(T::read_u32(&buf)) |
| 218 | } |
| 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 | fn read_i32<T: ByteOrder>(&mut self) -> Result<i32> { |
| 241 | let mut buf = [0; 4]; |
| 242 | self.read_exact(&mut buf)?; |
| 243 | Ok(T::read_i32(&buf)) |
| 244 | } |
| 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 | fn read_u48<T: ByteOrder>(&mut self) -> Result<u64> { |
| 267 | let mut buf = [0; 6]; |
| 268 | self.read_exact(&mut buf)?; |
| 269 | Ok(T::read_u48(&buf)) |
| 270 | } |
| 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 | fn read_i48<T: ByteOrder>(&mut self) -> Result<i64> { |
| 293 | let mut buf = [0; 6]; |
| 294 | self.read_exact(&mut buf)?; |
| 295 | Ok(T::read_i48(&buf)) |
| 296 | } |
| 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 | fn read_u64<T: ByteOrder>(&mut self) -> Result<u64> { |
| 319 | let mut buf = [0; 8]; |
| 320 | self.read_exact(&mut buf)?; |
| 321 | Ok(T::read_u64(&buf)) |
| 322 | } |
| 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 | fn read_i64<T: ByteOrder>(&mut self) -> Result<i64> { |
| 345 | let mut buf = [0; 8]; |
| 346 | self.read_exact(&mut buf)?; |
| 347 | Ok(T::read_i64(&buf)) |
| 348 | } |
| 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 | fn read_u128<T: ByteOrder>(&mut self) -> Result<u128> { |
| 374 | let mut buf = [0; 16]; |
| 375 | self.read_exact(&mut buf)?; |
| 376 | Ok(T::read_u128(&buf)) |
| 377 | } |
| 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 | fn read_i128<T: ByteOrder>(&mut self) -> Result<i128> { |
| 400 | let mut buf = [0; 16]; |
| 401 | self.read_exact(&mut buf)?; |
| 402 | Ok(T::read_i128(&buf)) |
| 403 | } |
| 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 | fn read_uint<T: ByteOrder>(&mut self, nbytes: usize) -> Result<u64> { |
| 425 | let mut buf = [0; 8]; |
| 426 | self.read_exact(&mut buf[..nbytes])?; |
| 427 | Ok(T::read_uint(&buf[..nbytes], nbytes)) |
| 428 | } |
| 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 | fn read_int<T: ByteOrder>(&mut self, nbytes: usize) -> Result<i64> { |
| 450 | let mut buf = [0; 8]; |
| 451 | self.read_exact(&mut buf[..nbytes])?; |
| 452 | Ok(T::read_int(&buf[..nbytes], nbytes)) |
| 453 | } |
| 454 | |
| 455 | /// Reads an unsigned n-bytes integer from the underlying reader. |
| 456 | #[inline ] |
| 457 | fn read_uint128<T: ByteOrder>(&mut self, nbytes: usize) -> Result<u128> { |
| 458 | let mut buf = [0; 16]; |
| 459 | self.read_exact(&mut buf[..nbytes])?; |
| 460 | Ok(T::read_uint128(&buf[..nbytes], nbytes)) |
| 461 | } |
| 462 | |
| 463 | /// Reads a signed n-bytes integer from the underlying reader. |
| 464 | #[inline ] |
| 465 | fn read_int128<T: ByteOrder>(&mut self, nbytes: usize) -> Result<i128> { |
| 466 | let mut buf = [0; 16]; |
| 467 | self.read_exact(&mut buf[..nbytes])?; |
| 468 | Ok(T::read_int128(&buf[..nbytes], nbytes)) |
| 469 | } |
| 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 | fn read_f32<T: ByteOrder>(&mut self) -> Result<f32> { |
| 497 | let mut buf = [0; 4]; |
| 498 | self.read_exact(&mut buf)?; |
| 499 | Ok(T::read_f32(&buf)) |
| 500 | } |
| 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 | fn read_f64<T: ByteOrder>(&mut self) -> Result<f64> { |
| 528 | let mut buf = [0; 8]; |
| 529 | self.read_exact(&mut buf)?; |
| 530 | Ok(T::read_f64(&buf)) |
| 531 | } |
| 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 | fn write_u8(&mut self, n: u8) -> Result<()> { |
| 586 | self.write_all(&[n]) |
| 587 | } |
| 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 | fn write_i8(&mut self, n: i8) -> Result<()> { |
| 614 | self.write_all(&[n as u8]) |
| 615 | } |
| 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 | fn write_u16<T: ByteOrder>(&mut self, n: u16) -> Result<()> { |
| 639 | let mut buf = [0; 2]; |
| 640 | T::write_u16(&mut buf, n); |
| 641 | self.write_all(&buf) |
| 642 | } |
| 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 | fn write_i16<T: ByteOrder>(&mut self, n: i16) -> Result<()> { |
| 666 | let mut buf = [0; 2]; |
| 667 | T::write_i16(&mut buf, n); |
| 668 | self.write_all(&buf) |
| 669 | } |
| 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 | fn write_u24<T: ByteOrder>(&mut self, n: u32) -> Result<()> { |
| 693 | let mut buf = [0; 3]; |
| 694 | T::write_u24(&mut buf, n); |
| 695 | self.write_all(&buf) |
| 696 | } |
| 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 | fn write_i24<T: ByteOrder>(&mut self, n: i32) -> Result<()> { |
| 720 | let mut buf = [0; 3]; |
| 721 | T::write_i24(&mut buf, n); |
| 722 | self.write_all(&buf) |
| 723 | } |
| 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 | fn write_u32<T: ByteOrder>(&mut self, n: u32) -> Result<()> { |
| 747 | let mut buf = [0; 4]; |
| 748 | T::write_u32(&mut buf, n); |
| 749 | self.write_all(&buf) |
| 750 | } |
| 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 | fn write_i32<T: ByteOrder>(&mut self, n: i32) -> Result<()> { |
| 774 | let mut buf = [0; 4]; |
| 775 | T::write_i32(&mut buf, n); |
| 776 | self.write_all(&buf) |
| 777 | } |
| 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 | fn write_u48<T: ByteOrder>(&mut self, n: u64) -> Result<()> { |
| 801 | let mut buf = [0; 6]; |
| 802 | T::write_u48(&mut buf, n); |
| 803 | self.write_all(&buf) |
| 804 | } |
| 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 | fn write_i48<T: ByteOrder>(&mut self, n: i64) -> Result<()> { |
| 828 | let mut buf = [0; 6]; |
| 829 | T::write_i48(&mut buf, n); |
| 830 | self.write_all(&buf) |
| 831 | } |
| 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 | fn write_u64<T: ByteOrder>(&mut self, n: u64) -> Result<()> { |
| 855 | let mut buf = [0; 8]; |
| 856 | T::write_u64(&mut buf, n); |
| 857 | self.write_all(&buf) |
| 858 | } |
| 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 | fn write_i64<T: ByteOrder>(&mut self, n: i64) -> Result<()> { |
| 882 | let mut buf = [0; 8]; |
| 883 | T::write_i64(&mut buf, n); |
| 884 | self.write_all(&buf) |
| 885 | } |
| 886 | |
| 887 | /// Writes an unsigned 128 bit integer to the underlying writer. |
| 888 | #[inline ] |
| 889 | fn write_u128<T: ByteOrder>(&mut self, n: u128) -> Result<()> { |
| 890 | let mut buf = [0; 16]; |
| 891 | T::write_u128(&mut buf, n); |
| 892 | self.write_all(&buf) |
| 893 | } |
| 894 | |
| 895 | /// Writes a signed 128 bit integer to the underlying writer. |
| 896 | #[inline ] |
| 897 | fn write_i128<T: ByteOrder>(&mut self, n: i128) -> Result<()> { |
| 898 | let mut buf = [0; 16]; |
| 899 | T::write_i128(&mut buf, n); |
| 900 | self.write_all(&buf) |
| 901 | } |
| 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 | fn write_uint<T: ByteOrder>( |
| 930 | &mut self, |
| 931 | n: u64, |
| 932 | nbytes: usize, |
| 933 | ) -> Result<()> { |
| 934 | let mut buf = [0; 8]; |
| 935 | T::write_uint(&mut buf, n, nbytes); |
| 936 | self.write_all(&buf[0..nbytes]) |
| 937 | } |
| 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 | fn write_int<T: ByteOrder>( |
| 966 | &mut self, |
| 967 | n: i64, |
| 968 | nbytes: usize, |
| 969 | ) -> Result<()> { |
| 970 | let mut buf = [0; 8]; |
| 971 | T::write_int(&mut buf, n, nbytes); |
| 972 | self.write_all(&buf[0..nbytes]) |
| 973 | } |
| 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 | fn write_uint128<T: ByteOrder>( |
| 981 | &mut self, |
| 982 | n: u128, |
| 983 | nbytes: usize, |
| 984 | ) -> Result<()> { |
| 985 | let mut buf = [0; 16]; |
| 986 | T::write_uint128(&mut buf, n, nbytes); |
| 987 | self.write_all(&buf[0..nbytes]) |
| 988 | } |
| 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 | fn write_int128<T: ByteOrder>( |
| 996 | &mut self, |
| 997 | n: i128, |
| 998 | nbytes: usize, |
| 999 | ) -> Result<()> { |
| 1000 | let mut buf = [0; 16]; |
| 1001 | T::write_int128(&mut buf, n, nbytes); |
| 1002 | self.write_all(&buf[0..nbytes]) |
| 1003 | } |
| 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 | fn write_f32<T: ByteOrder>(&mut self, n: f32) -> Result<()> { |
| 1029 | let mut buf = [0; 4]; |
| 1030 | T::write_f32(&mut buf, n); |
| 1031 | self.write_all(&buf) |
| 1032 | } |
| 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 | fn write_f64<T: ByteOrder>(&mut self, n: f64) -> Result<()> { |
| 1058 | let mut buf = [0; 8]; |
| 1059 | T::write_f64(&mut buf, n); |
| 1060 | self.write_all(&buf) |
| 1061 | } |
| 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 {} |
| 1067 | |