1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Wrapper types providing byte order awareness.
use byteorder::{
BigEndian, LittleEndian, NativeEndian, NetworkEndian, ReadBytesExt, WriteBytesExt,
};
use std::fmt::Arguments;
use std::io::{BufRead, Read, Result as IoResult, Seek, SeekFrom, Write};
use {Endian, Endianness, StaticEndianness};
/// Wrapper type for a reader or writer with an assumed byte order.
///
/// More details can be found at the [crate level documentation][1].
///
/// [1]: index.html
#[derive(Debug, Clone)]
pub struct ByteOrdered<T, E> {
inner: T,
endianness: E,
}
impl<T, E> ByteOrdered<T, E>
where
E: Default,
{
#[inline]
fn new_default(inner: T) -> Self {
ByteOrdered {
inner,
endianness: Default::default(),
}
}
}
impl<T> ByteOrdered<T, StaticEndianness<LittleEndian>> {
/// Obtains a new reader or writer that assumes data in _little endian_.
#[inline]
pub fn le(inner: T) -> Self {
ByteOrdered::new_default(inner)
}
}
impl<T> ByteOrdered<T, StaticEndianness<BigEndian>> {
/// Obtains a new reader or writer that assumes data in _big endian_.
#[inline]
pub fn be(inner: T) -> Self {
ByteOrdered::new_default(inner)
}
}
impl<T> ByteOrdered<T, StaticEndianness<NativeEndian>> {
/// Obtains a new reader or writer that assumes data in the system's
/// _native endianness_. While this method might sounds a bit pointless,
/// it enables easier byte order changes through method chaining).
#[inline]
pub fn native(inner: T) -> Self {
ByteOrdered::new_default(inner)
}
}
impl<T> ByteOrdered<T, StaticEndianness<NetworkEndian>> {
/// Obtains a new reader or writer that assumes _network order_.
#[inline]
pub fn network(inner: T) -> Self {
ByteOrdered::new_default(inner)
}
}
impl<T> ByteOrdered<T, Endianness> {
/// Creates a new reader or writer that assumes data in the given byte
/// order known at _run-time_.
/// That is, the type representing the byte order
/// is the enum type [`Endianness`].
///
/// Although it is equivalent to [`ByteOrdered::new`][`new`], this function
/// leaves a code signal that subsequent calls depend on conditions
/// resolved at run-time. If you know the data's endianness in compile
/// time, the other constructors are preferred (e.g. [`new`], [`le`] or
/// [`be`]), so as to avoid the overhead of dynamic dispatching.
///
/// [`Endianness`]: ../base/struct.Endianness.html
/// [`new`]: struct.ByteOrdered.html#method.new
/// [`le`]: struct.ByteOrdered.html#method.le
/// [`be`]: struct.ByteOrdered.html#method.be
#[inline]
pub fn runtime(inner: T, endianness: Endianness) -> Self {
ByteOrdered::new(inner, endianness)
}
}
impl<T, E> From<(T, E)> for ByteOrdered<T, E> {
#[inline]
fn from((inner, endianness): (T, E)) -> Self {
ByteOrdered { inner, endianness }
}
}
impl<T, E> ByteOrdered<T, E>
where
E: Endian,
{
/// Creates a new reader or writer that assumes data in the given byte
/// order. This flexible constructor admits any kind of byte order (static
/// and dynamic).
///
/// **Note:** The other constructors ([`le`], [`be`], [`native`], and
/// [`runtime`]) are more recommended because they are easier to use and
/// leave a better signal of whether the endianness is known at compile
/// time or at run time. For example, if you pass a value literal of type
/// `Endianness` (such as `Endianness::Little`), the program will perform
/// dynamic dispatching in spite of the fixed byte order. The use of this
/// method is more appropriate when constructing functions which are
/// generic over the endianness type.
///
/// [`le`]: struct.ByteOrdered.html#method.le
/// [`be`]: struct.ByteOrdered.html#method.be
/// [`native`]: struct.ByteOrdered.html#method.native
/// [`runtime`]: struct.ByteOrdered.html#method.runtime
#[inline]
pub fn new(inner: T, endianness: E) -> Self {
ByteOrdered { inner, endianness }
}
/// Recovers the inner reader or writer from this wrapper. Information
/// about the assumed byte order is discarded.
#[inline]
pub fn into_inner(self) -> T {
self.inner
}
/// Obtains an exclusive mutable reference to the inner reader or writer in
/// this wrapper. Information about the assumed byte order is ignored until
/// the reference is dropped.
#[inline]
pub fn inner_mut(&mut self) -> &mut T {
&mut self.inner
}
/// Converts from `ByteOrdered<T, E>` to `ByteOrdered<&mut T, E>`,
/// copying the endianness information.
#[inline]
pub fn as_mut(&mut self) -> ByteOrdered<&mut T, E>
where
E: Copy,
{
let e = self.endianness;
ByteOrdered::new(self.inner_mut(), e)
}
/// Disbands a `ByteOrder` into its parts.
#[inline]
pub fn into_parts(self) -> (T, E) {
(self.inner, self.endianness)
}
/// Maps a `ByteOrdered<T, E>` into a `ByteOrdered<O, E>` by applying the
/// given function to the inner reader or writer.
#[inline]
pub fn map<F, U>(self, f: F) -> ByteOrdered<U, E>
where
F: FnOnce(T) -> U,
{
let (src, e) = self.into_parts();
ByteOrdered::new(f(src), e)
}
/// Changes the assumed byte order of the reader or writer.
#[inline]
pub fn into_endianness<E2: Endian>(self, endianness: E2) -> ByteOrdered<T, E2> {
ByteOrdered::new(self.inner, endianness)
}
/// Modifies the assumed byte order of the reader or writer
/// inline with the value.
/// Since the endianness type needs to be the same,
/// this function is only relevant when
/// `E` is a run-time defined byte order
/// (see [`Endianness`]).
///
/// [`Endianness`]: ../base/struct.Endianness.html
#[inline]
pub fn set_endianness(&mut self, endianness: E) {
self.endianness = endianness;
}
/// Changes the assumed byte order of the reader or writer to
/// little endian.
#[inline]
pub fn into_le(self) -> ByteOrdered<T, StaticEndianness<LittleEndian>> {
ByteOrdered::le(self.inner)
}
/// Changes the assumed byte order of the reader or writer to
/// little endian.
#[inline]
pub fn into_be(self) -> ByteOrdered<T, StaticEndianness<BigEndian>> {
ByteOrdered::be(self.inner)
}
/// Changes the assumed byte order of the reader or writer to
/// the system's native endianness.
#[inline]
pub fn into_native(self) -> ByteOrdered<T, StaticEndianness<NativeEndian>> {
ByteOrdered::native(self.inner)
}
/// Converts the assumed endianness to the opposite of the current order.
#[inline]
pub fn into_opposite(self) -> ByteOrdered<T, E::Opposite>
where
E: Endian,
{
let e = self.endianness.into_opposite();
ByteOrdered {
inner: self.inner,
endianness: e,
}
}
/// Retrieves the byte order assumed by this wrapper.
#[inline]
pub fn endianness(&self) -> E
where
E: Copy,
{
self.endianness
}
/// Checks whether the assumed endianness is the system's native byte
/// order.
#[inline]
pub fn is_native(&self) -> bool
where
E: Endian,
{
self.endianness.is_native()
}
}
impl<R, E> Read for ByteOrdered<R, E>
where
R: Read,
{
#[inline]
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
self.inner.read(buf)
}
#[inline]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> IoResult<usize> {
self.inner.read_to_end(buf)
}
#[inline]
fn read_to_string(&mut self, buf: &mut String) -> IoResult<usize> {
self.inner.read_to_string(buf)
}
#[inline]
fn read_exact(&mut self, buf: &mut [u8]) -> IoResult<()> {
self.inner.read_exact(buf)
}
}
impl<W, E> Write for ByteOrdered<W, E>
where
W: Write,
{
#[inline]
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
self.inner.write(buf)
}
#[inline]
fn flush(&mut self) -> IoResult<()> {
self.inner.flush()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> IoResult<()> {
self.inner.write_all(buf)
}
#[inline]
fn write_fmt(&mut self, fmt: Arguments) -> IoResult<()> {
self.inner.write_fmt(fmt)
}
}
impl<R, E> ByteOrdered<R, E>
where
R: ReadBytesExt,
E: Endian,
{
/// Reads a signed 8 bit integer from the underlying reader.
///
/// This method does exactly the same thing as `read_i8` in
/// `byteorder::ReadBytesExt`. It is included so that users do not have to
/// import the former trait.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
///
/// Read unsigned 8 bit integers from a `Read`:
///
/// ```rust
/// use byteordered::ByteOrdered;
///
/// # fn run() -> std::io::Result<()> {
/// let mut rdr = ByteOrdered::native(&[2, 5][..]);
/// assert_eq!(2, rdr.read_i8()?);
/// assert_eq!(5, rdr.read_i8()?);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
#[inline]
pub fn read_i8(&mut self) -> IoResult<i8> {
ReadBytesExt::read_i8(self)
}
/// Reads an unsigned 8 bit integer from the underlying reader.
///
/// This method does exactly the same thing as `read_u8` in
/// `byteorder::ReadBytesExt`. It is included so that users do not have to
/// import the former trait.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
///
/// Read unsigned 8 bit integers from a `Read`:
///
/// ```rust
/// use byteordered::ByteOrdered;
///
/// # fn run() -> std::io::Result<()> {
/// let mut rdr = ByteOrdered::native(&[2, 5][..]);
/// assert_eq!(2, rdr.read_u8()?);
/// assert_eq!(5, rdr.read_u8()?);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
#[inline]
pub fn read_u8(&mut self) -> IoResult<u8> {
ReadBytesExt::read_u8(self)
}
/// Reads a signed 16 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
///
/// Read signed 16 bit big-endian integers from a `Read`:
///
/// ```rust
/// use byteordered::ByteOrdered;
///
/// # fn run() -> std::io::Result<()> {
/// let mut rdr = ByteOrdered::be(&[0x00, 0xc1, 0xff, 0x7c][..]);
/// assert_eq!(193, rdr.read_i16()?);
/// assert_eq!(-132, rdr.read_i16()?);
/// # Ok(())
/// # }
/// # run().unwrap();
/// ```
#[inline]
pub fn read_i16(&mut self) -> IoResult<i16> {
self.endianness.read_i16(self.inner.by_ref())
}
/// Reads a sequence of signed 16 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
///
/// # Examples
///
/// Read two signed 16 bit big-endian integers from a `Read`:
///
/// ```rust
/// # use byteordered::ByteOrdered;
/// # fn run() -> std::io::Result<()> {
/// let mut out = [0; 2];
/// let mut rdr = ByteOrdered::be(&[0x00, 0xc1, 0xff, 0x7c][..]);
/// rdr.read_i16_into(&mut out)?;
/// assert_eq!(out, [193, -132]);
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn read_i16_into(&mut self, dst: &mut [i16]) -> IoResult<()> {
self.endianness.read_i16_into(self.inner.by_ref(), dst)
}
/// Reads an unsigned 16 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u16(&mut self) -> IoResult<u16> {
self.endianness.read_u16(self.inner.by_ref())
}
/// Reads a sequence of unsigned 16 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u16_into(&mut self, dst: &mut [u16]) -> IoResult<()> {
self.endianness.read_u16_into(self.inner.by_ref(), dst)
}
/// Reads a signed 32 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_i32(&mut self) -> IoResult<i32> {
self.endianness.read_i32(self.inner.by_ref())
}
/// Reads a sequence of signed 32 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_i32_into(&mut self, dst: &mut [i32]) -> IoResult<()> {
self.endianness.read_i32_into(self.inner.by_ref(), dst)
}
/// Reads an unsigned 32 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u32(&mut self) -> IoResult<u32> {
self.endianness.read_u32(self.inner.by_ref())
}
/// Reads a sequence of unsigned 32 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u32_into(&mut self, dst: &mut [u32]) -> IoResult<()> {
self.endianness.read_u32_into(self.inner.by_ref(), dst)
}
/// Reads a signed 64 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_i64(&mut self) -> IoResult<i64> {
self.endianness.read_i64(self.inner.by_ref())
}
/// Reads a sequence of signed 64 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_i64_into(&mut self, dst: &mut [i64]) -> IoResult<()> {
self.endianness.read_i64_into(self.inner.by_ref(), dst)
}
/// Reads an unsigned 16 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u64(&mut self) -> IoResult<u64> {
self.endianness.read_u64(self.inner.by_ref())
}
/// Reads a sequence of unsigned 64 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u64_into(&mut self, dst: &mut [u64]) -> IoResult<()> {
self.endianness.read_u64_into(self.inner.by_ref(), dst)
}
/// Reads a signed 128 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_i128(&mut self) -> IoResult<i128> {
self.endianness.read_i128(self.inner.by_ref())
}
/// Reads a sequence of signed 128 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_i128_into(&mut self, dst: &mut [i128]) -> IoResult<()> {
self.endianness.read_i128_into(self.inner.by_ref(), dst)
}
/// Reads an unsigned 16 bit integer from the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u128(&mut self) -> IoResult<u128> {
self.endianness.read_u128(self.inner.by_ref())
}
/// Reads a sequence of unsigned 128 bit integers from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_u128_into(&mut self, dst: &mut [u128]) -> IoResult<()> {
self.endianness.read_u128_into(self.inner.by_ref(), dst)
}
/// Reads a IEEE754 single-precision (4 bytes) floating point number from
/// the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_f32(&mut self) -> IoResult<f32> {
self.endianness.read_f32(self.inner.by_ref())
}
/// Reads a sequence of IEEE754 single-precision (4 bytes) floating point numbers
/// from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_f32_into(&mut self, dst: &mut [f32]) -> IoResult<()> {
self.endianness.read_f32_into(self.inner.by_ref(), dst)
}
/// Reads a IEEE754 double-precision (8 bytes) floating point number from
/// the underlying reader.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_f64(&mut self) -> IoResult<f64> {
self.endianness.read_f64(self.inner.by_ref())
}
/// Reads a sequence of IEEE754 double-precision (8 bytes) floating point numbers
/// from the underlying reader.
///
/// The given buffer is either filled completely or an error is returned.
/// If an error is returned,
/// the contents of `dst` are unspecified.
///
/// # Errors
///
/// This method returns the same errors as [`Read::read_exact`].
///
/// [`Read::read_exact`]: https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact
#[inline]
pub fn read_f64_into(&mut self, dst: &mut [f64]) -> IoResult<()> {
self.endianness.read_f64_into(self.inner.by_ref(), dst)
}
}
impl<W, E> ByteOrdered<W, E>
where
W: WriteBytesExt,
E: Endian,
{
/// Writes a signed 8 bit integer to the underlying writer.
///
/// Note that since this writes a single byte, no byte order conversions
/// are used. It is included for completeness.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_i8(&mut self, x: i8) -> IoResult<()> {
self.inner.write_i8(x)
}
/// Writes an unsigned 8 bit integer to the underlying writer.
///
/// Note that since this writes a single byte, no byte order conversions
/// are used. It is included for completeness.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_u8(&mut self, x: u8) -> IoResult<()> {
self.inner.write_u8(x)
}
/// Writes a signed 16 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
///
/// # Examples
///
/// Write signed 16 bit big-endian integers to a `Write`:
///
/// ```rust
/// use byteordered::ByteOrdered;
///
/// let mut wtr = ByteOrdered::be(Vec::new());
/// wtr.write_i16(193).unwrap();
/// wtr.write_i16(-132).unwrap();
/// assert_eq!(wtr.into_inner(), b"\x00\xc1\xff\x7c");
/// ```
#[inline]
pub fn write_i16(&mut self, x: i16) -> IoResult<()> {
self.endianness.write_i16(self.inner.by_ref(), x)
}
/// Writes an unsigned 16 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_u16(&mut self, x: u16) -> IoResult<()> {
self.endianness.write_u16(self.inner.by_ref(), x)
}
/// Writes a signed 32 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_i32(&mut self, x: i32) -> IoResult<()> {
self.endianness.write_i32(self.inner.by_ref(), x)
}
/// Writes an unsigned 32 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_u32(&mut self, x: u32) -> IoResult<()> {
self.endianness.write_u32(self.inner.by_ref(), x)
}
/// Writes a signed 64 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_i64(&mut self, x: i64) -> IoResult<()> {
self.endianness.write_i64(self.inner.by_ref(), x)
}
/// Writes an unsigned 64 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_u64(&mut self, x: u64) -> IoResult<()> {
self.endianness.write_u64(self.inner.by_ref(), x)
}
/// Writes a signed 128 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_i128(&mut self, x: i128) -> IoResult<()> {
self.endianness.write_i128(self.inner.by_ref(), x)
}
/// Writes an unsigned 128 bit integer to the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_u128(&mut self, x: u128) -> IoResult<()> {
self.endianness.write_u128(self.inner.by_ref(), x)
}
/// Writes a IEEE754 single-precision (4 bytes) floating point number to
/// the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_f32(&mut self, x: f32) -> IoResult<()> {
self.endianness.write_f32(self.inner.by_ref(), x)
}
/// Writes a IEEE754 double-precision (8 bytes) floating point number to
/// the underlying writer.
///
/// # Errors
///
/// This method returns the same errors as [`Write::write_all`].
///
/// [`Write::write_all`]: https://doc.rust-lang.org/std/io/trait.Write.html#method.write_all
#[inline]
pub fn write_f64(&mut self, x: f64) -> IoResult<()> {
self.endianness.write_f64(self.inner.by_ref(), x)
}
}
impl<T, E> BufRead for ByteOrdered<T, E>
where
T: BufRead,
{
#[inline]
fn fill_buf(&mut self) -> IoResult<&[u8]> {
self.inner.fill_buf()
}
#[inline]
fn consume(&mut self, amt: usize) {
self.inner.consume(amt)
}
#[inline]
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> IoResult<usize> {
self.inner.read_until(byte, buf)
}
#[inline]
fn read_line(&mut self, buf: &mut String) -> IoResult<usize> {
self.inner.read_line(buf)
}
}
impl<T, E> Seek for ByteOrdered<T, E>
where
T: Seek,
{
#[inline]
fn seek(&mut self, pos: SeekFrom) -> IoResult<u64> {
self.inner.seek(pos)
}
}
#[cfg(test)]
mod tests {
// TODO test moar
use super::ByteOrdered;
use base::Endianness;
static TEST_BYTES: &'static [u8] = &[0x12, 0x34, 0x56, 0x78, 0x21, 0x43, 0x65, 0x87];
static TEST_U64DATA_LE: &'static [u64] = &[0x87654321_78563412];
static TEST_U64DATA_BE: &'static [u64] = &[0x12345678_21436587];
#[test]
fn test_read_u64() {
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::le(&mut data);
let words = [reader.read_u64().unwrap()];
assert_eq!(words, TEST_U64DATA_LE);
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::be(&mut data);
let words = [reader.read_u64().unwrap()];
assert_eq!(words, TEST_U64DATA_BE);
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::runtime(&mut data, Endianness::Little);
let words = [reader.read_u64().unwrap()];
assert_eq!(words, TEST_U64DATA_LE);
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::runtime(&mut data, Endianness::Big);
let words = [reader.read_u64().unwrap()];
assert_eq!(words, TEST_U64DATA_BE);
}
#[test]
fn test_write_u64() {
let mut writer = ByteOrdered::le(Vec::new());
for v in TEST_U64DATA_LE {
writer.write_u64(*v).unwrap();
}
assert_eq!(&*writer.into_inner(), TEST_BYTES);
let mut writer = ByteOrdered::be(Vec::new());
for v in TEST_U64DATA_BE {
writer.write_u64(*v).unwrap();
}
assert_eq!(&*writer.into_inner(), TEST_BYTES);
let mut writer = ByteOrdered::runtime(Vec::new(), Endianness::Little);
for v in TEST_U64DATA_LE {
writer.write_u64(*v).unwrap();
}
assert_eq!(&*writer.into_inner(), TEST_BYTES);
let mut writer = ByteOrdered::runtime(Vec::new(), Endianness::Big);
for v in TEST_U64DATA_BE {
writer.write_u64(*v).unwrap();
}
assert_eq!(&*writer.into_inner(), TEST_BYTES);
}
/// the test bytes as two u32s in little endian
static TEST_U32DATA_LE: &'static [u32] = &[0x7856_3412, 0x8765_4321];
/// the test bytes as two u32s in big endian
static TEST_U32DATA_BE: &'static [u32] = &[0x1234_5678, 0x2143_6587];
#[test]
fn test_read_u32_into() {
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::le(&mut data);
let mut words = [0; 2];
reader.read_u32_into(&mut words).unwrap();
assert_eq!(words, TEST_U32DATA_LE);
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::be(&mut data);
let mut words = [0; 2];
reader.read_u32_into(&mut words).unwrap();
assert_eq!(words, TEST_U32DATA_BE);
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::runtime(&mut data, Endianness::Little);
let mut words = [0; 2];
reader.read_u32_into(&mut words).unwrap();
assert_eq!(words, TEST_U32DATA_LE);
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::runtime(&mut data, Endianness::Big);
let mut words = [0; 2];
reader.read_u32_into(&mut words).unwrap();
assert_eq!(words, TEST_U32DATA_BE);
}
#[test]
fn test_read_u32_and_set_endianness() {
let mut data = TEST_BYTES;
let mut reader = ByteOrdered::runtime(&mut data, Endianness::Little);
let v1 = reader.read_u32().unwrap();
assert_eq!(v1, TEST_U32DATA_LE[0]);
// change to big endian
reader.set_endianness(Endianness::Big);
let v2 = reader.read_u32().unwrap();
assert_eq!(v2, TEST_U32DATA_BE[1]);
}
}