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
//! 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,
{
    fn new_default(inner: T) -> Self {
        ByteOrdered {
            inner: inner,
            endianness: Default::default(),
        }
    }
}

impl<T> ByteOrdered<T, StaticEndianness<LittleEndian>> {
    /// Obtains a new reader or writer that assumes data in _little endian_.
    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_.
    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).
    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_.
    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_.
    ///
    /// 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.
    ///
    /// [`new`]: struct.ByteOrdered.html#method.new
    /// [`le`]: struct.ByteOrdered.html#method.le
    /// [`be`]: struct.ByteOrdered.html#method.be
    pub fn runtime(inner: T, endianness: Endianness) -> Self {
        ByteOrdered::new(inner, endianness)
    }
}

impl<T, E> From<(T, E)> for ByteOrdered<T, E> {
    fn from((inner, endianness): (T, E)) -> Self {
        ByteOrdered {
            inner: inner,
            endianness: 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
    pub fn new(inner: T, endianness: E) -> Self {
        ByteOrdered {
            inner: inner,
            endianness: endianness,
        }
    }

    /// Recovers the inner reader or writer from this wrapper. Information
    /// about the assumed byte order is discarded.
    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.
    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.
    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.
    pub fn into_parts(self) -> (T, E) {
        (self.inner, self.endianness)
    }

    /// Changes the assumed byte order of the reader or writer.
    pub fn into_endianness<E2: Endian>(self, endianness: E2) -> ByteOrdered<T, E2> {
        ByteOrdered::new(self.inner, endianness)
    }

    /// Changes the assumed byte order of the reader or writer to
    /// little endian.
    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.
    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.
    pub fn into_native(self) -> ByteOrdered<T, StaticEndianness<NativeEndian>> {
        ByteOrdered::native(self.inner)
    }

    /// Converts the assumed endianness to the opposite of the current order.
    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.
    pub fn endianness(&self) -> E
    where
        E: Copy,
    {
        self.endianness
    }

    /// Checks whether the assumed endianness is the system's native byte
    /// order.
    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 std::io::Cursor;
    /// use byteordered::ByteOrdered;
    ///
    /// let mut rdr = ByteOrdered::native(Cursor::new(vec![2, 5]));
    /// assert_eq!(2, rdr.read_i8().unwrap());
    /// assert_eq!(5, rdr.read_i8().unwrap());
    /// ```
    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 std::io::Cursor;
    /// use byteordered::ByteOrdered;
    ///
    /// let mut rdr = ByteOrdered::native(Cursor::new(vec![2, 5]));
    /// assert_eq!(2, rdr.read_u8().unwrap());
    /// assert_eq!(5, rdr.read_u8().unwrap());
    /// ```
    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 std::io::Cursor;
    /// use byteordered::ByteOrdered;
    ///
    /// let mut rdr = ByteOrdered::be(Cursor::new(vec![0x00, 0xc1, 0xff, 0x7c]));
    /// assert_eq!(193, rdr.read_i16().unwrap());
    /// assert_eq!(-132, rdr.read_i16().unwrap());
    /// ```
    pub fn read_i16(&mut self) -> IoResult<i16> {
        self.endianness.read_i16(self.inner.by_ref())
    }

    /// 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
    pub fn read_u16(&mut self) -> IoResult<u16> {
        self.endianness.read_u16(self.inner.by_ref())
    }

    /// 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
    pub fn read_i32(&mut self) -> IoResult<i32> {
        self.endianness.read_i32(self.inner.by_ref())
    }

    /// 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
    pub fn read_u32(&mut self) -> IoResult<u32> {
        self.endianness.read_u32(self.inner.by_ref())
    }

    /// 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
    pub fn read_i64(&mut self) -> IoResult<i64> {
        self.endianness.read_i64(self.inner.by_ref())
    }

    /// 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
    pub fn read_u64(&mut self) -> IoResult<u64> {
        self.endianness.read_u64(self.inner.by_ref())
    }

    /// 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
    pub fn read_i128(&mut self) -> IoResult<i128> {
        self.endianness.read_i128(self.inner.by_ref())
    }

    /// 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
    pub fn read_u128(&mut self) -> IoResult<u128> {
        self.endianness.read_u128(self.inner.by_ref())
    }

    /// 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
    pub fn read_f32(&mut self) -> IoResult<f32> {
        self.endianness.read_f32(self.inner.by_ref())
    }

    /// 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
    pub fn read_f64(&mut self) -> IoResult<f64> {
        self.endianness.read_f64(self.inner.by_ref())
    }
}

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
    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
    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");
    /// ```
    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
    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
    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
    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
    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
    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
    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
    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
    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
    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,
{
    fn fill_buf(&mut self) -> IoResult<&[u8]> {
        self.inner.fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        self.inner.consume(amt)
    }

    fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> IoResult<usize> {
        self.inner.read_until(byte, buf)
    }

    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,
{
    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);
    }
}