]>
git.proxmox.com Git - rustc.git/blob - vendor/byteorder/src/io.rs
8 /// Extends [`Read`] with methods for reading numbers. (For `std::io`.)
10 /// Most of the methods defined here have an unconstrained type parameter that
11 /// must be explicitly instantiated. Typically, it is instantiated with either
12 /// the [`BigEndian`] or [`LittleEndian`] types defined in this crate.
16 /// Read unsigned 16 bit big-endian integers from a [`Read`]:
19 /// use std::io::Cursor;
20 /// use byteorder::{BigEndian, ReadBytesExt};
22 /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
23 /// assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
24 /// assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());
27 /// [`BigEndian`]: enum.BigEndian.html
28 /// [`LittleEndian`]: enum.LittleEndian.html
29 /// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
30 pub trait ReadBytesExt
: io
::Read
{
31 /// Reads an unsigned 8 bit integer from the underlying reader.
33 /// Note that since this reads a single byte, no byte order conversions
34 /// are used. It is included for completeness.
38 /// This method returns the same errors as [`Read::read_exact`].
40 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
44 /// Read unsigned 8 bit integers from a `Read`:
47 /// use std::io::Cursor;
48 /// use byteorder::ReadBytesExt;
50 /// let mut rdr = Cursor::new(vec![2, 5]);
51 /// assert_eq!(2, rdr.read_u8().unwrap());
52 /// assert_eq!(5, rdr.read_u8().unwrap());
55 fn read_u8(&mut self) -> Result
<u8> {
57 self.read_exact(&mut buf
)?
;
61 /// Reads a signed 8 bit integer from the underlying reader.
63 /// Note that since this reads a single byte, no byte order conversions
64 /// are used. It is included for completeness.
68 /// This method returns the same errors as [`Read::read_exact`].
70 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
74 /// Read signed 8 bit integers from a `Read`:
77 /// use std::io::Cursor;
78 /// use byteorder::ReadBytesExt;
80 /// let mut rdr = Cursor::new(vec![0x02, 0xfb]);
81 /// assert_eq!(2, rdr.read_i8().unwrap());
82 /// assert_eq!(-5, rdr.read_i8().unwrap());
85 fn read_i8(&mut self) -> Result
<i8> {
87 self.read_exact(&mut buf
)?
;
91 /// Reads an unsigned 16 bit integer from the underlying reader.
95 /// This method returns the same errors as [`Read::read_exact`].
97 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
101 /// Read unsigned 16 bit big-endian integers from a `Read`:
104 /// use std::io::Cursor;
105 /// use byteorder::{BigEndian, ReadBytesExt};
107 /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
108 /// assert_eq!(517, rdr.read_u16::<BigEndian>().unwrap());
109 /// assert_eq!(768, rdr.read_u16::<BigEndian>().unwrap());
112 fn read_u16
<T
: ByteOrder
>(&mut self) -> Result
<u16> {
113 let mut buf
= [0; 2];
114 self.read_exact(&mut buf
)?
;
115 Ok(T
::read_u16(&buf
))
118 /// Reads a signed 16 bit integer from the underlying reader.
122 /// This method returns the same errors as [`Read::read_exact`].
124 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
128 /// Read signed 16 bit big-endian integers from a `Read`:
131 /// use std::io::Cursor;
132 /// use byteorder::{BigEndian, ReadBytesExt};
134 /// let mut rdr = Cursor::new(vec![0x00, 0xc1, 0xff, 0x7c]);
135 /// assert_eq!(193, rdr.read_i16::<BigEndian>().unwrap());
136 /// assert_eq!(-132, rdr.read_i16::<BigEndian>().unwrap());
139 fn read_i16
<T
: ByteOrder
>(&mut self) -> Result
<i16> {
140 let mut buf
= [0; 2];
141 self.read_exact(&mut buf
)?
;
142 Ok(T
::read_i16(&buf
))
145 /// Reads an unsigned 24 bit integer from the underlying reader.
149 /// This method returns the same errors as [`Read::read_exact`].
151 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
155 /// Read unsigned 24 bit big-endian integers from a `Read`:
158 /// use std::io::Cursor;
159 /// use byteorder::{BigEndian, ReadBytesExt};
161 /// let mut rdr = Cursor::new(vec![0x00, 0x01, 0x0b]);
162 /// assert_eq!(267, rdr.read_u24::<BigEndian>().unwrap());
165 fn read_u24
<T
: ByteOrder
>(&mut self) -> Result
<u32> {
166 let mut buf
= [0; 3];
167 self.read_exact(&mut buf
)?
;
168 Ok(T
::read_u24(&buf
))
171 /// Reads a signed 24 bit integer from the underlying reader.
175 /// This method returns the same errors as [`Read::read_exact`].
177 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
181 /// Read signed 24 bit big-endian integers from a `Read`:
184 /// use std::io::Cursor;
185 /// use byteorder::{BigEndian, ReadBytesExt};
187 /// let mut rdr = Cursor::new(vec![0xff, 0x7a, 0x33]);
188 /// assert_eq!(-34253, rdr.read_i24::<BigEndian>().unwrap());
191 fn read_i24
<T
: ByteOrder
>(&mut self) -> Result
<i32> {
192 let mut buf
= [0; 3];
193 self.read_exact(&mut buf
)?
;
194 Ok(T
::read_i24(&buf
))
197 /// Reads an unsigned 32 bit integer from the underlying reader.
201 /// This method returns the same errors as [`Read::read_exact`].
203 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
207 /// Read unsigned 32 bit big-endian integers from a `Read`:
210 /// use std::io::Cursor;
211 /// use byteorder::{BigEndian, ReadBytesExt};
213 /// let mut rdr = Cursor::new(vec![0x00, 0x00, 0x01, 0x0b]);
214 /// assert_eq!(267, rdr.read_u32::<BigEndian>().unwrap());
217 fn read_u32
<T
: ByteOrder
>(&mut self) -> Result
<u32> {
218 let mut buf
= [0; 4];
219 self.read_exact(&mut buf
)?
;
220 Ok(T
::read_u32(&buf
))
223 /// Reads a signed 32 bit integer from the underlying reader.
227 /// This method returns the same errors as [`Read::read_exact`].
229 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
233 /// Read signed 32 bit big-endian integers from a `Read`:
236 /// use std::io::Cursor;
237 /// use byteorder::{BigEndian, ReadBytesExt};
239 /// let mut rdr = Cursor::new(vec![0xff, 0xff, 0x7a, 0x33]);
240 /// assert_eq!(-34253, rdr.read_i32::<BigEndian>().unwrap());
243 fn read_i32
<T
: ByteOrder
>(&mut self) -> Result
<i32> {
244 let mut buf
= [0; 4];
245 self.read_exact(&mut buf
)?
;
246 Ok(T
::read_i32(&buf
))
249 /// Reads an unsigned 48 bit integer from the underlying reader.
253 /// This method returns the same errors as [`Read::read_exact`].
255 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
259 /// Read unsigned 48 bit big-endian integers from a `Read`:
262 /// use std::io::Cursor;
263 /// use byteorder::{BigEndian, ReadBytesExt};
265 /// let mut rdr = Cursor::new(vec![0xb6, 0x71, 0x6b, 0xdc, 0x2b, 0x31]);
266 /// assert_eq!(200598257150769, rdr.read_u48::<BigEndian>().unwrap());
269 fn read_u48
<T
: ByteOrder
>(&mut self) -> Result
<u64> {
270 let mut buf
= [0; 6];
271 self.read_exact(&mut buf
)?
;
272 Ok(T
::read_u48(&buf
))
275 /// Reads a signed 48 bit integer from the underlying reader.
279 /// This method returns the same errors as [`Read::read_exact`].
281 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
285 /// Read signed 48 bit big-endian integers from a `Read`:
288 /// use std::io::Cursor;
289 /// use byteorder::{BigEndian, ReadBytesExt};
291 /// let mut rdr = Cursor::new(vec![0x9d, 0x71, 0xab, 0xe7, 0x97, 0x8f]);
292 /// assert_eq!(-108363435763825, rdr.read_i48::<BigEndian>().unwrap());
295 fn read_i48
<T
: ByteOrder
>(&mut self) -> Result
<i64> {
296 let mut buf
= [0; 6];
297 self.read_exact(&mut buf
)?
;
298 Ok(T
::read_i48(&buf
))
301 /// Reads an unsigned 64 bit integer from the underlying reader.
305 /// This method returns the same errors as [`Read::read_exact`].
307 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
311 /// Read an unsigned 64 bit big-endian integer from a `Read`:
314 /// use std::io::Cursor;
315 /// use byteorder::{BigEndian, ReadBytesExt};
317 /// let mut rdr = Cursor::new(vec![0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83]);
318 /// assert_eq!(918733457491587, rdr.read_u64::<BigEndian>().unwrap());
321 fn read_u64
<T
: ByteOrder
>(&mut self) -> Result
<u64> {
322 let mut buf
= [0; 8];
323 self.read_exact(&mut buf
)?
;
324 Ok(T
::read_u64(&buf
))
327 /// Reads a signed 64 bit integer from the underlying reader.
331 /// This method returns the same errors as [`Read::read_exact`].
333 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
337 /// Read a signed 64 bit big-endian integer from a `Read`:
340 /// use std::io::Cursor;
341 /// use byteorder::{BigEndian, ReadBytesExt};
343 /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0]);
344 /// assert_eq!(i64::min_value(), rdr.read_i64::<BigEndian>().unwrap());
347 fn read_i64
<T
: ByteOrder
>(&mut self) -> Result
<i64> {
348 let mut buf
= [0; 8];
349 self.read_exact(&mut buf
)?
;
350 Ok(T
::read_i64(&buf
))
353 /// Reads an unsigned 128 bit integer from the underlying reader.
357 /// This method returns the same errors as [`Read::read_exact`].
359 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
363 /// Read an unsigned 128 bit big-endian integer from a `Read`:
366 /// use std::io::Cursor;
367 /// use byteorder::{BigEndian, ReadBytesExt};
369 /// let mut rdr = Cursor::new(vec![
370 /// 0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83,
371 /// 0x00, 0x03, 0x43, 0x95, 0x4d, 0x60, 0x86, 0x83
373 /// assert_eq!(16947640962301618749969007319746179, rdr.read_u128::<BigEndian>().unwrap());
376 fn read_u128
<T
: ByteOrder
>(&mut self) -> Result
<u128
> {
377 let mut buf
= [0; 16];
378 self.read_exact(&mut buf
)?
;
379 Ok(T
::read_u128(&buf
))
382 /// Reads a signed 128 bit integer from the underlying reader.
386 /// This method returns the same errors as [`Read::read_exact`].
388 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
392 /// Read a signed 128 bit big-endian integer from a `Read`:
395 /// use std::io::Cursor;
396 /// use byteorder::{BigEndian, ReadBytesExt};
398 /// let mut rdr = Cursor::new(vec![0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
399 /// assert_eq!(i128::min_value(), rdr.read_i128::<BigEndian>().unwrap());
402 fn read_i128
<T
: ByteOrder
>(&mut self) -> Result
<i128
> {
403 let mut buf
= [0; 16];
404 self.read_exact(&mut buf
)?
;
405 Ok(T
::read_i128(&buf
))
408 /// Reads an unsigned n-bytes integer from the underlying reader.
412 /// This method returns the same errors as [`Read::read_exact`].
414 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
418 /// Read an unsigned n-byte big-endian integer from a `Read`:
421 /// use std::io::Cursor;
422 /// use byteorder::{BigEndian, ReadBytesExt};
424 /// let mut rdr = Cursor::new(vec![0x80, 0x74, 0xfa]);
425 /// assert_eq!(8418554, rdr.read_uint::<BigEndian>(3).unwrap());
427 fn read_uint
<T
: ByteOrder
>(&mut self, nbytes
: usize) -> Result
<u64> {
428 let mut buf
= [0; 8];
429 self.read_exact(&mut buf
[..nbytes
])?
;
430 Ok(T
::read_uint(&buf
[..nbytes
], nbytes
))
433 /// Reads a signed n-bytes integer from the underlying reader.
437 /// This method returns the same errors as [`Read::read_exact`].
439 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
443 /// Read an unsigned n-byte big-endian integer from a `Read`:
446 /// use std::io::Cursor;
447 /// use byteorder::{BigEndian, ReadBytesExt};
449 /// let mut rdr = Cursor::new(vec![0xc1, 0xff, 0x7c]);
450 /// assert_eq!(-4063364, rdr.read_int::<BigEndian>(3).unwrap());
452 fn read_int
<T
: ByteOrder
>(&mut self, nbytes
: usize) -> Result
<i64> {
453 let mut buf
= [0; 8];
454 self.read_exact(&mut buf
[..nbytes
])?
;
455 Ok(T
::read_int(&buf
[..nbytes
], nbytes
))
458 /// Reads an unsigned n-bytes integer from the underlying reader.
460 fn read_uint128
<T
: ByteOrder
>(&mut self, nbytes
: usize) -> Result
<u128
> {
461 let mut buf
= [0; 16];
462 self.read_exact(&mut buf
[..nbytes
])?
;
463 Ok(T
::read_uint128(&buf
[..nbytes
], nbytes
))
466 /// Reads a signed n-bytes integer from the underlying reader.
468 fn read_int128
<T
: ByteOrder
>(&mut self, nbytes
: usize) -> Result
<i128
> {
469 let mut buf
= [0; 16];
470 self.read_exact(&mut buf
[..nbytes
])?
;
471 Ok(T
::read_int128(&buf
[..nbytes
], nbytes
))
474 /// Reads a IEEE754 single-precision (4 bytes) floating point number from
475 /// the underlying reader.
479 /// This method returns the same errors as [`Read::read_exact`].
481 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
485 /// Read a big-endian single-precision floating point number from a `Read`:
489 /// use std::io::Cursor;
491 /// use byteorder::{BigEndian, ReadBytesExt};
493 /// let mut rdr = Cursor::new(vec![
494 /// 0x40, 0x49, 0x0f, 0xdb,
496 /// assert_eq!(f32::consts::PI, rdr.read_f32::<BigEndian>().unwrap());
499 fn read_f32
<T
: ByteOrder
>(&mut self) -> Result
<f32> {
500 let mut buf
= [0; 4];
501 self.read_exact(&mut buf
)?
;
502 Ok(T
::read_f32(&buf
))
505 /// Reads a IEEE754 double-precision (8 bytes) floating point number from
506 /// the underlying reader.
510 /// This method returns the same errors as [`Read::read_exact`].
512 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
516 /// Read a big-endian double-precision floating point number from a `Read`:
520 /// use std::io::Cursor;
522 /// use byteorder::{BigEndian, ReadBytesExt};
524 /// let mut rdr = Cursor::new(vec![
525 /// 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
527 /// assert_eq!(f64::consts::PI, rdr.read_f64::<BigEndian>().unwrap());
530 fn read_f64
<T
: ByteOrder
>(&mut self) -> Result
<f64> {
531 let mut buf
= [0; 8];
532 self.read_exact(&mut buf
)?
;
533 Ok(T
::read_f64(&buf
))
536 /// Reads a sequence of unsigned 16 bit integers from the underlying
539 /// The given buffer is either filled completely or an error is returned.
540 /// If an error is returned, the contents of `dst` are unspecified.
544 /// This method returns the same errors as [`Read::read_exact`].
546 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
550 /// Read a sequence of unsigned 16 bit big-endian integers from a `Read`:
553 /// use std::io::Cursor;
554 /// use byteorder::{BigEndian, ReadBytesExt};
556 /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
557 /// let mut dst = [0; 2];
558 /// rdr.read_u16_into::<BigEndian>(&mut dst).unwrap();
559 /// assert_eq!([517, 768], dst);
562 fn read_u16_into
<T
: ByteOrder
>(&mut self, dst
: &mut [u16]) -> Result
<()> {
564 let buf
= unsafe { slice_to_u8_mut(dst) }
;
565 self.read_exact(buf
)?
;
567 T
::from_slice_u16(dst
);
571 /// Reads a sequence of unsigned 32 bit integers from the underlying
574 /// The given buffer is either filled completely or an error is returned.
575 /// If an error is returned, the contents of `dst` are unspecified.
579 /// This method returns the same errors as [`Read::read_exact`].
581 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
585 /// Read a sequence of unsigned 32 bit big-endian integers from a `Read`:
588 /// use std::io::Cursor;
589 /// use byteorder::{BigEndian, ReadBytesExt};
591 /// let mut rdr = Cursor::new(vec![0, 0, 2, 5, 0, 0, 3, 0]);
592 /// let mut dst = [0; 2];
593 /// rdr.read_u32_into::<BigEndian>(&mut dst).unwrap();
594 /// assert_eq!([517, 768], dst);
597 fn read_u32_into
<T
: ByteOrder
>(&mut self, dst
: &mut [u32]) -> Result
<()> {
599 let buf
= unsafe { slice_to_u8_mut(dst) }
;
600 self.read_exact(buf
)?
;
602 T
::from_slice_u32(dst
);
606 /// Reads a sequence of unsigned 64 bit integers from the underlying
609 /// The given buffer is either filled completely or an error is returned.
610 /// If an error is returned, the contents of `dst` are unspecified.
614 /// This method returns the same errors as [`Read::read_exact`].
616 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
620 /// Read a sequence of unsigned 64 bit big-endian integers from a `Read`:
623 /// use std::io::Cursor;
624 /// use byteorder::{BigEndian, ReadBytesExt};
626 /// let mut rdr = Cursor::new(vec![
627 /// 0, 0, 0, 0, 0, 0, 2, 5,
628 /// 0, 0, 0, 0, 0, 0, 3, 0,
630 /// let mut dst = [0; 2];
631 /// rdr.read_u64_into::<BigEndian>(&mut dst).unwrap();
632 /// assert_eq!([517, 768], dst);
635 fn read_u64_into
<T
: ByteOrder
>(&mut self, dst
: &mut [u64]) -> Result
<()> {
637 let buf
= unsafe { slice_to_u8_mut(dst) }
;
638 self.read_exact(buf
)?
;
640 T
::from_slice_u64(dst
);
644 /// Reads a sequence of unsigned 128 bit integers from the underlying
647 /// The given buffer is either filled completely or an error is returned.
648 /// If an error is returned, the contents of `dst` are unspecified.
652 /// This method returns the same errors as [`Read::read_exact`].
654 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
658 /// Read a sequence of unsigned 128 bit big-endian integers from a `Read`:
661 /// use std::io::Cursor;
662 /// use byteorder::{BigEndian, ReadBytesExt};
664 /// let mut rdr = Cursor::new(vec![
665 /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5,
666 /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
668 /// let mut dst = [0; 2];
669 /// rdr.read_u128_into::<BigEndian>(&mut dst).unwrap();
670 /// assert_eq!([517, 768], dst);
673 fn read_u128_into
<T
: ByteOrder
>(
678 let buf
= unsafe { slice_to_u8_mut(dst) }
;
679 self.read_exact(buf
)?
;
681 T
::from_slice_u128(dst
);
685 /// Reads a sequence of signed 8 bit integers from the underlying reader.
687 /// The given buffer is either filled completely or an error is returned.
688 /// If an error is returned, the contents of `dst` are unspecified.
690 /// Note that since each `i8` is a single byte, no byte order conversions
691 /// are used. This method is included because it provides a safe, simple
692 /// way for the caller to read into a `&mut [i8]` buffer. (Without this
693 /// method, the caller would have to either use `unsafe` code or convert
694 /// each byte to `i8` individually.)
698 /// This method returns the same errors as [`Read::read_exact`].
700 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
704 /// Read a sequence of signed 8 bit integers from a `Read`:
707 /// use std::io::Cursor;
708 /// use byteorder::{BigEndian, ReadBytesExt};
710 /// let mut rdr = Cursor::new(vec![2, 251, 3]);
711 /// let mut dst = [0; 3];
712 /// rdr.read_i8_into(&mut dst).unwrap();
713 /// assert_eq!([2, -5, 3], dst);
716 fn read_i8_into(&mut self, dst
: &mut [i8]) -> Result
<()> {
717 let buf
= unsafe { slice_to_u8_mut(dst) }
;
721 /// Reads a sequence of signed 16 bit integers from the underlying
724 /// The given buffer is either filled completely or an error is returned.
725 /// If an error is returned, the contents of `dst` are unspecified.
729 /// This method returns the same errors as [`Read::read_exact`].
731 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
735 /// Read a sequence of signed 16 bit big-endian integers from a `Read`:
738 /// use std::io::Cursor;
739 /// use byteorder::{BigEndian, ReadBytesExt};
741 /// let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
742 /// let mut dst = [0; 2];
743 /// rdr.read_i16_into::<BigEndian>(&mut dst).unwrap();
744 /// assert_eq!([517, 768], dst);
747 fn read_i16_into
<T
: ByteOrder
>(&mut self, dst
: &mut [i16]) -> Result
<()> {
749 let buf
= unsafe { slice_to_u8_mut(dst) }
;
750 self.read_exact(buf
)?
;
752 T
::from_slice_i16(dst
);
756 /// Reads a sequence of signed 32 bit integers from the underlying
759 /// The given buffer is either filled completely or an error is returned.
760 /// If an error is returned, the contents of `dst` are unspecified.
764 /// This method returns the same errors as [`Read::read_exact`].
766 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
770 /// Read a sequence of signed 32 bit big-endian integers from a `Read`:
773 /// use std::io::Cursor;
774 /// use byteorder::{BigEndian, ReadBytesExt};
776 /// let mut rdr = Cursor::new(vec![0, 0, 2, 5, 0, 0, 3, 0]);
777 /// let mut dst = [0; 2];
778 /// rdr.read_i32_into::<BigEndian>(&mut dst).unwrap();
779 /// assert_eq!([517, 768], dst);
782 fn read_i32_into
<T
: ByteOrder
>(&mut self, dst
: &mut [i32]) -> Result
<()> {
784 let buf
= unsafe { slice_to_u8_mut(dst) }
;
785 self.read_exact(buf
)?
;
787 T
::from_slice_i32(dst
);
791 /// Reads a sequence of signed 64 bit integers from the underlying
794 /// The given buffer is either filled completely or an error is returned.
795 /// If an error is returned, the contents of `dst` are unspecified.
799 /// This method returns the same errors as [`Read::read_exact`].
801 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
805 /// Read a sequence of signed 64 bit big-endian integers from a `Read`:
808 /// use std::io::Cursor;
809 /// use byteorder::{BigEndian, ReadBytesExt};
811 /// let mut rdr = Cursor::new(vec![
812 /// 0, 0, 0, 0, 0, 0, 2, 5,
813 /// 0, 0, 0, 0, 0, 0, 3, 0,
815 /// let mut dst = [0; 2];
816 /// rdr.read_i64_into::<BigEndian>(&mut dst).unwrap();
817 /// assert_eq!([517, 768], dst);
820 fn read_i64_into
<T
: ByteOrder
>(&mut self, dst
: &mut [i64]) -> Result
<()> {
822 let buf
= unsafe { slice_to_u8_mut(dst) }
;
823 self.read_exact(buf
)?
;
825 T
::from_slice_i64(dst
);
829 /// Reads a sequence of signed 128 bit integers from the underlying
832 /// The given buffer is either filled completely or an error is returned.
833 /// If an error is returned, the contents of `dst` are unspecified.
837 /// This method returns the same errors as [`Read::read_exact`].
839 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
843 /// Read a sequence of signed 128 bit big-endian integers from a `Read`:
846 /// use std::io::Cursor;
847 /// use byteorder::{BigEndian, ReadBytesExt};
849 /// let mut rdr = Cursor::new(vec![
850 /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 5,
851 /// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0,
853 /// let mut dst = [0; 2];
854 /// rdr.read_i128_into::<BigEndian>(&mut dst).unwrap();
855 /// assert_eq!([517, 768], dst);
858 fn read_i128_into
<T
: ByteOrder
>(
863 let buf
= unsafe { slice_to_u8_mut(dst) }
;
864 self.read_exact(buf
)?
;
866 T
::from_slice_i128(dst
);
870 /// Reads a sequence of IEEE754 single-precision (4 bytes) floating
871 /// point numbers from the underlying reader.
873 /// The given buffer is either filled completely or an error is returned.
874 /// If an error is returned, the contents of `dst` are unspecified.
878 /// This method returns the same errors as [`Read::read_exact`].
880 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
884 /// Read a sequence of big-endian single-precision floating point number
889 /// use std::io::Cursor;
891 /// use byteorder::{BigEndian, ReadBytesExt};
893 /// let mut rdr = Cursor::new(vec![
894 /// 0x40, 0x49, 0x0f, 0xdb,
895 /// 0x3f, 0x80, 0x00, 0x00,
897 /// let mut dst = [0.0; 2];
898 /// rdr.read_f32_into::<BigEndian>(&mut dst).unwrap();
899 /// assert_eq!([f32::consts::PI, 1.0], dst);
902 fn read_f32_into
<T
: ByteOrder
>(&mut self, dst
: &mut [f32]) -> Result
<()> {
904 let buf
= unsafe { slice_to_u8_mut(dst) }
;
905 self.read_exact(buf
)?
;
907 T
::from_slice_f32(dst
);
913 /// This method is deprecated. Use `read_f32_into` instead.
915 /// Reads a sequence of IEEE754 single-precision (4 bytes) floating
916 /// point numbers from the underlying reader.
918 /// The given buffer is either filled completely or an error is returned.
919 /// If an error is returned, the contents of `dst` are unspecified.
923 /// This method returns the same errors as [`Read::read_exact`].
925 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
929 /// Read a sequence of big-endian single-precision floating point number
934 /// use std::io::Cursor;
936 /// use byteorder::{BigEndian, ReadBytesExt};
938 /// let mut rdr = Cursor::new(vec![
939 /// 0x40, 0x49, 0x0f, 0xdb,
940 /// 0x3f, 0x80, 0x00, 0x00,
942 /// let mut dst = [0.0; 2];
943 /// rdr.read_f32_into_unchecked::<BigEndian>(&mut dst).unwrap();
944 /// assert_eq!([f32::consts::PI, 1.0], dst);
947 #[deprecated(since = "1.2.0", note = "please use `read_f32_into` instead")]
948 fn read_f32_into_unchecked
<T
: ByteOrder
>(
952 self.read_f32_into
::<T
>(dst
)
955 /// Reads a sequence of IEEE754 double-precision (8 bytes) floating
956 /// point numbers from the underlying reader.
958 /// The given buffer is either filled completely or an error is returned.
959 /// If an error is returned, the contents of `dst` are unspecified.
963 /// This method returns the same errors as [`Read::read_exact`].
965 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
969 /// Read a sequence of big-endian single-precision floating point number
974 /// use std::io::Cursor;
976 /// use byteorder::{BigEndian, ReadBytesExt};
978 /// let mut rdr = Cursor::new(vec![
979 /// 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
980 /// 0x3f, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
982 /// let mut dst = [0.0; 2];
983 /// rdr.read_f64_into::<BigEndian>(&mut dst).unwrap();
984 /// assert_eq!([f64::consts::PI, 1.0], dst);
987 fn read_f64_into
<T
: ByteOrder
>(&mut self, dst
: &mut [f64]) -> Result
<()> {
989 let buf
= unsafe { slice_to_u8_mut(dst) }
;
990 self.read_exact(buf
)?
;
992 T
::from_slice_f64(dst
);
998 /// This method is deprecated. Use `read_f64_into` instead.
1000 /// Reads a sequence of IEEE754 double-precision (8 bytes) floating
1001 /// point numbers from the underlying reader.
1003 /// The given buffer is either filled completely or an error is returned.
1004 /// If an error is returned, the contents of `dst` are unspecified.
1008 /// This method is unsafe because there are no guarantees made about the
1009 /// floating point values. In particular, this method does not check for
1010 /// signaling NaNs, which may result in undefined behavior.
1014 /// This method returns the same errors as [`Read::read_exact`].
1016 /// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
1020 /// Read a sequence of big-endian single-precision floating point number
1025 /// use std::io::Cursor;
1027 /// use byteorder::{BigEndian, ReadBytesExt};
1029 /// let mut rdr = Cursor::new(vec![
1030 /// 0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18,
1031 /// 0x3f, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1033 /// let mut dst = [0.0; 2];
1034 /// rdr.read_f64_into_unchecked::<BigEndian>(&mut dst).unwrap();
1035 /// assert_eq!([f64::consts::PI, 1.0], dst);
1038 #[deprecated(since = "1.2.0", note = "please use `read_f64_into` instead")]
1039 fn read_f64_into_unchecked
<T
: ByteOrder
>(
1043 self.read_f64_into
::<T
>(dst
)
1047 /// All types that implement `Read` get methods defined in `ReadBytesExt`
1049 impl<R
: io
::Read
+ ?Sized
> ReadBytesExt
for R {}
1051 /// Extends [`Write`] with methods for writing numbers. (For `std::io`.)
1053 /// Most of the methods defined here have an unconstrained type parameter that
1054 /// must be explicitly instantiated. Typically, it is instantiated with either
1055 /// the [`BigEndian`] or [`LittleEndian`] types defined in this crate.
1059 /// Write unsigned 16 bit big-endian integers to a [`Write`]:
1062 /// use byteorder::{BigEndian, WriteBytesExt};
1064 /// let mut wtr = vec![];
1065 /// wtr.write_u16::<BigEndian>(517).unwrap();
1066 /// wtr.write_u16::<BigEndian>(768).unwrap();
1067 /// assert_eq!(wtr, vec![2, 5, 3, 0]);
1070 /// [`BigEndian`]: enum.BigEndian.html
1071 /// [`LittleEndian`]: enum.LittleEndian.html
1072 /// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
1073 pub trait WriteBytesExt
: io
::Write
{
1074 /// Writes an unsigned 8 bit integer to the underlying writer.
1076 /// Note that since this writes a single byte, no byte order conversions
1077 /// are used. It is included for completeness.
1081 /// This method returns the same errors as [`Write::write_all`].
1083 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1087 /// Write unsigned 8 bit integers to a `Write`:
1090 /// use byteorder::WriteBytesExt;
1092 /// let mut wtr = Vec::new();
1093 /// wtr.write_u8(2).unwrap();
1094 /// wtr.write_u8(5).unwrap();
1095 /// assert_eq!(wtr, b"\x02\x05");
1098 fn write_u8(&mut self, n
: u8) -> Result
<()> {
1099 self.write_all(&[n
])
1102 /// Writes a signed 8 bit integer to the underlying writer.
1104 /// Note that since this writes a single byte, no byte order conversions
1105 /// are used. It is included for completeness.
1109 /// This method returns the same errors as [`Write::write_all`].
1111 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1115 /// Write signed 8 bit integers to a `Write`:
1118 /// use byteorder::WriteBytesExt;
1120 /// let mut wtr = Vec::new();
1121 /// wtr.write_i8(2).unwrap();
1122 /// wtr.write_i8(-5).unwrap();
1123 /// assert_eq!(wtr, b"\x02\xfb");
1126 fn write_i8(&mut self, n
: i8) -> Result
<()> {
1127 self.write_all(&[n
as u8])
1130 /// Writes an unsigned 16 bit integer to the underlying writer.
1134 /// This method returns the same errors as [`Write::write_all`].
1136 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1140 /// Write unsigned 16 bit big-endian integers to a `Write`:
1143 /// use byteorder::{BigEndian, WriteBytesExt};
1145 /// let mut wtr = Vec::new();
1146 /// wtr.write_u16::<BigEndian>(517).unwrap();
1147 /// wtr.write_u16::<BigEndian>(768).unwrap();
1148 /// assert_eq!(wtr, b"\x02\x05\x03\x00");
1151 fn write_u16
<T
: ByteOrder
>(&mut self, n
: u16) -> Result
<()> {
1152 let mut buf
= [0; 2];
1153 T
::write_u16(&mut buf
, n
);
1154 self.write_all(&buf
)
1157 /// Writes a signed 16 bit integer to the underlying writer.
1161 /// This method returns the same errors as [`Write::write_all`].
1163 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1167 /// Write signed 16 bit big-endian integers to a `Write`:
1170 /// use byteorder::{BigEndian, WriteBytesExt};
1172 /// let mut wtr = Vec::new();
1173 /// wtr.write_i16::<BigEndian>(193).unwrap();
1174 /// wtr.write_i16::<BigEndian>(-132).unwrap();
1175 /// assert_eq!(wtr, b"\x00\xc1\xff\x7c");
1178 fn write_i16
<T
: ByteOrder
>(&mut self, n
: i16) -> Result
<()> {
1179 let mut buf
= [0; 2];
1180 T
::write_i16(&mut buf
, n
);
1181 self.write_all(&buf
)
1184 /// Writes an unsigned 24 bit integer to the underlying writer.
1188 /// This method returns the same errors as [`Write::write_all`].
1190 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1194 /// Write unsigned 24 bit big-endian integers to a `Write`:
1197 /// use byteorder::{BigEndian, WriteBytesExt};
1199 /// let mut wtr = Vec::new();
1200 /// wtr.write_u24::<BigEndian>(267).unwrap();
1201 /// wtr.write_u24::<BigEndian>(120111).unwrap();
1202 /// assert_eq!(wtr, b"\x00\x01\x0b\x01\xd5\x2f");
1205 fn write_u24
<T
: ByteOrder
>(&mut self, n
: u32) -> Result
<()> {
1206 let mut buf
= [0; 3];
1207 T
::write_u24(&mut buf
, n
);
1208 self.write_all(&buf
)
1211 /// Writes a signed 24 bit integer to the underlying writer.
1215 /// This method returns the same errors as [`Write::write_all`].
1217 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1221 /// Write signed 24 bit big-endian integers to a `Write`:
1224 /// use byteorder::{BigEndian, WriteBytesExt};
1226 /// let mut wtr = Vec::new();
1227 /// wtr.write_i24::<BigEndian>(-34253).unwrap();
1228 /// wtr.write_i24::<BigEndian>(120111).unwrap();
1229 /// assert_eq!(wtr, b"\xff\x7a\x33\x01\xd5\x2f");
1232 fn write_i24
<T
: ByteOrder
>(&mut self, n
: i32) -> Result
<()> {
1233 let mut buf
= [0; 3];
1234 T
::write_i24(&mut buf
, n
);
1235 self.write_all(&buf
)
1238 /// Writes an unsigned 32 bit integer to the underlying writer.
1242 /// This method returns the same errors as [`Write::write_all`].
1244 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1248 /// Write unsigned 32 bit big-endian integers to a `Write`:
1251 /// use byteorder::{BigEndian, WriteBytesExt};
1253 /// let mut wtr = Vec::new();
1254 /// wtr.write_u32::<BigEndian>(267).unwrap();
1255 /// wtr.write_u32::<BigEndian>(1205419366).unwrap();
1256 /// assert_eq!(wtr, b"\x00\x00\x01\x0b\x47\xd9\x3d\x66");
1259 fn write_u32
<T
: ByteOrder
>(&mut self, n
: u32) -> Result
<()> {
1260 let mut buf
= [0; 4];
1261 T
::write_u32(&mut buf
, n
);
1262 self.write_all(&buf
)
1265 /// Writes a signed 32 bit integer to the underlying writer.
1269 /// This method returns the same errors as [`Write::write_all`].
1271 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1275 /// Write signed 32 bit big-endian integers to a `Write`:
1278 /// use byteorder::{BigEndian, WriteBytesExt};
1280 /// let mut wtr = Vec::new();
1281 /// wtr.write_i32::<BigEndian>(-34253).unwrap();
1282 /// wtr.write_i32::<BigEndian>(1205419366).unwrap();
1283 /// assert_eq!(wtr, b"\xff\xff\x7a\x33\x47\xd9\x3d\x66");
1286 fn write_i32
<T
: ByteOrder
>(&mut self, n
: i32) -> Result
<()> {
1287 let mut buf
= [0; 4];
1288 T
::write_i32(&mut buf
, n
);
1289 self.write_all(&buf
)
1292 /// Writes an unsigned 48 bit integer to the underlying writer.
1296 /// This method returns the same errors as [`Write::write_all`].
1298 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1302 /// Write unsigned 48 bit big-endian integers to a `Write`:
1305 /// use byteorder::{BigEndian, WriteBytesExt};
1307 /// let mut wtr = Vec::new();
1308 /// wtr.write_u48::<BigEndian>(52360336390828).unwrap();
1309 /// wtr.write_u48::<BigEndian>(541).unwrap();
1310 /// assert_eq!(wtr, b"\x2f\x9f\x17\x40\x3a\xac\x00\x00\x00\x00\x02\x1d");
1313 fn write_u48
<T
: ByteOrder
>(&mut self, n
: u64) -> Result
<()> {
1314 let mut buf
= [0; 6];
1315 T
::write_u48(&mut buf
, n
);
1316 self.write_all(&buf
)
1319 /// Writes a signed 48 bit integer to the underlying writer.
1323 /// This method returns the same errors as [`Write::write_all`].
1325 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1329 /// Write signed 48 bit big-endian integers to a `Write`:
1332 /// use byteorder::{BigEndian, WriteBytesExt};
1334 /// let mut wtr = Vec::new();
1335 /// wtr.write_i48::<BigEndian>(-108363435763825).unwrap();
1336 /// wtr.write_i48::<BigEndian>(77).unwrap();
1337 /// assert_eq!(wtr, b"\x9d\x71\xab\xe7\x97\x8f\x00\x00\x00\x00\x00\x4d");
1340 fn write_i48
<T
: ByteOrder
>(&mut self, n
: i64) -> Result
<()> {
1341 let mut buf
= [0; 6];
1342 T
::write_i48(&mut buf
, n
);
1343 self.write_all(&buf
)
1346 /// Writes an unsigned 64 bit integer to the underlying writer.
1350 /// This method returns the same errors as [`Write::write_all`].
1352 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1356 /// Write unsigned 64 bit big-endian integers to a `Write`:
1359 /// use byteorder::{BigEndian, WriteBytesExt};
1361 /// let mut wtr = Vec::new();
1362 /// wtr.write_u64::<BigEndian>(918733457491587).unwrap();
1363 /// wtr.write_u64::<BigEndian>(143).unwrap();
1364 /// assert_eq!(wtr, b"\x00\x03\x43\x95\x4d\x60\x86\x83\x00\x00\x00\x00\x00\x00\x00\x8f");
1367 fn write_u64
<T
: ByteOrder
>(&mut self, n
: u64) -> Result
<()> {
1368 let mut buf
= [0; 8];
1369 T
::write_u64(&mut buf
, n
);
1370 self.write_all(&buf
)
1373 /// Writes a signed 64 bit integer to the underlying writer.
1377 /// This method returns the same errors as [`Write::write_all`].
1379 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1383 /// Write signed 64 bit big-endian integers to a `Write`:
1386 /// use byteorder::{BigEndian, WriteBytesExt};
1388 /// let mut wtr = Vec::new();
1389 /// wtr.write_i64::<BigEndian>(i64::min_value()).unwrap();
1390 /// wtr.write_i64::<BigEndian>(i64::max_value()).unwrap();
1391 /// assert_eq!(wtr, b"\x80\x00\x00\x00\x00\x00\x00\x00\x7f\xff\xff\xff\xff\xff\xff\xff");
1394 fn write_i64
<T
: ByteOrder
>(&mut self, n
: i64) -> Result
<()> {
1395 let mut buf
= [0; 8];
1396 T
::write_i64(&mut buf
, n
);
1397 self.write_all(&buf
)
1400 /// Writes an unsigned 128 bit integer to the underlying writer.
1402 fn write_u128
<T
: ByteOrder
>(&mut self, n
: u128
) -> Result
<()> {
1403 let mut buf
= [0; 16];
1404 T
::write_u128(&mut buf
, n
);
1405 self.write_all(&buf
)
1408 /// Writes a signed 128 bit integer to the underlying writer.
1410 fn write_i128
<T
: ByteOrder
>(&mut self, n
: i128
) -> Result
<()> {
1411 let mut buf
= [0; 16];
1412 T
::write_i128(&mut buf
, n
);
1413 self.write_all(&buf
)
1416 /// Writes an unsigned n-bytes integer to the underlying writer.
1420 /// This method returns the same errors as [`Write::write_all`].
1422 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1426 /// If the given integer is not representable in the given number of bytes,
1427 /// this method panics. If `nbytes > 8`, this method panics.
1431 /// Write unsigned 40 bit big-endian integers to a `Write`:
1434 /// use byteorder::{BigEndian, WriteBytesExt};
1436 /// let mut wtr = Vec::new();
1437 /// wtr.write_uint::<BigEndian>(312550384361, 5).unwrap();
1438 /// wtr.write_uint::<BigEndian>(43, 5).unwrap();
1439 /// assert_eq!(wtr, b"\x48\xc5\x74\x62\xe9\x00\x00\x00\x00\x2b");
1442 fn write_uint
<T
: ByteOrder
>(
1447 let mut buf
= [0; 8];
1448 T
::write_uint(&mut buf
, n
, nbytes
);
1449 self.write_all(&buf
[0..nbytes
])
1452 /// Writes a signed n-bytes integer to the underlying writer.
1456 /// This method returns the same errors as [`Write::write_all`].
1458 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1462 /// If the given integer is not representable in the given number of bytes,
1463 /// this method panics. If `nbytes > 8`, this method panics.
1467 /// Write signed 56 bit big-endian integers to a `Write`:
1470 /// use byteorder::{BigEndian, WriteBytesExt};
1472 /// let mut wtr = Vec::new();
1473 /// wtr.write_int::<BigEndian>(-3548172039376767, 7).unwrap();
1474 /// wtr.write_int::<BigEndian>(43, 7).unwrap();
1475 /// assert_eq!(wtr, b"\xf3\x64\xf4\xd1\xfd\xb0\x81\x00\x00\x00\x00\x00\x00\x2b");
1478 fn write_int
<T
: ByteOrder
>(
1483 let mut buf
= [0; 8];
1484 T
::write_int(&mut buf
, n
, nbytes
);
1485 self.write_all(&buf
[0..nbytes
])
1488 /// Writes an unsigned n-bytes integer to the underlying writer.
1490 /// If the given integer is not representable in the given number of bytes,
1491 /// this method panics. If `nbytes > 16`, this method panics.
1493 fn write_uint128
<T
: ByteOrder
>(
1498 let mut buf
= [0; 16];
1499 T
::write_uint128(&mut buf
, n
, nbytes
);
1500 self.write_all(&buf
[0..nbytes
])
1503 /// Writes a signed n-bytes integer to the underlying writer.
1505 /// If the given integer is not representable in the given number of bytes,
1506 /// this method panics. If `nbytes > 16`, this method panics.
1508 fn write_int128
<T
: ByteOrder
>(
1513 let mut buf
= [0; 16];
1514 T
::write_int128(&mut buf
, n
, nbytes
);
1515 self.write_all(&buf
[0..nbytes
])
1518 /// Writes a IEEE754 single-precision (4 bytes) floating point number to
1519 /// the underlying writer.
1523 /// This method returns the same errors as [`Write::write_all`].
1525 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1529 /// Write a big-endian single-precision floating point number to a `Write`:
1534 /// use byteorder::{BigEndian, WriteBytesExt};
1536 /// let mut wtr = Vec::new();
1537 /// wtr.write_f32::<BigEndian>(f32::consts::PI).unwrap();
1538 /// assert_eq!(wtr, b"\x40\x49\x0f\xdb");
1541 fn write_f32
<T
: ByteOrder
>(&mut self, n
: f32) -> Result
<()> {
1542 let mut buf
= [0; 4];
1543 T
::write_f32(&mut buf
, n
);
1544 self.write_all(&buf
)
1547 /// Writes a IEEE754 double-precision (8 bytes) floating point number to
1548 /// the underlying writer.
1552 /// This method returns the same errors as [`Write::write_all`].
1554 /// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
1558 /// Write a big-endian double-precision floating point number to a `Write`:
1563 /// use byteorder::{BigEndian, WriteBytesExt};
1565 /// let mut wtr = Vec::new();
1566 /// wtr.write_f64::<BigEndian>(f64::consts::PI).unwrap();
1567 /// assert_eq!(wtr, b"\x40\x09\x21\xfb\x54\x44\x2d\x18");
1570 fn write_f64
<T
: ByteOrder
>(&mut self, n
: f64) -> Result
<()> {
1571 let mut buf
= [0; 8];
1572 T
::write_f64(&mut buf
, n
);
1573 self.write_all(&buf
)
1577 /// All types that implement `Write` get methods defined in `WriteBytesExt`
1579 impl<W
: io
::Write
+ ?Sized
> WriteBytesExt
for W {}
1581 /// Convert a slice of T (where T is plain old data) to its mutable binary
1584 /// This function is wildly unsafe because it permits arbitrary modification of
1585 /// the binary representation of any `Copy` type. Use with care. It's intended
1586 /// to be called only where `T` is a numeric type.
1587 unsafe fn slice_to_u8_mut
<T
: Copy
>(slice
: &mut [T
]) -> &mut [u8] {
1588 use std
::mem
::size_of
;
1590 let len
= size_of
::<T
>() * slice
.len();
1591 slice
::from_raw_parts_mut(slice
.as_mut_ptr() as *mut u8, len
)