Skip to content

Commit 234d814

Browse files
Doumancarllerche
authored andcommitted
Remove byteorder dependency (#280)
1 parent b5d4f87 commit 234d814

File tree

3 files changed

+73
-117
lines changed

3 files changed

+73
-117
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ edition = "2018"
2020
publish = false
2121

2222
[dependencies]
23-
byteorder = "1.3"
2423
serde = { version = "1.0", optional = true }
2524
either = { version = "1.5", default-features = false, optional = true }
2625

src/buf/buf.rs

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
use super::{IntoBuf, Take, Reader, FromBuf, Chain};
2-
use byteorder::{BigEndian, ByteOrder, LittleEndian};
32

4-
use std::{cmp, io::IoSlice, ptr};
3+
use std::{cmp, io::IoSlice, ptr, mem};
54

65
macro_rules! buf_get_impl {
7-
($this:ident, $size:expr, $conv:path) => ({
6+
($this:ident, $typ:tt::$conv:tt) => ({
7+
const SIZE: usize = mem::size_of::<$typ>();
88
// try to convert directly from the bytes
9-
let ret = {
10-
// this Option<ret> trick is to avoid keeping a borrow on self
11-
// when advance() is called (mut borrow) and to call bytes() only once
12-
if let Some(src) = $this.bytes().get(..($size)) {
13-
Some($conv(src))
14-
} else {
15-
None
16-
}
17-
};
9+
// this Option<ret> trick is to avoid keeping a borrow on self
10+
// when advance() is called (mut borrow) and to call bytes() only once
11+
let ret = $this.bytes().get(..SIZE).map(|src| unsafe {
12+
$typ::$conv(*(src as *const _ as *const [_; SIZE]))
13+
});
14+
1815
if let Some(ret) = ret {
1916
// if the direct conversion was possible, advance and return
20-
$this.advance($size);
17+
$this.advance(SIZE);
2118
return ret;
2219
} else {
2320
// if not we copy the bytes in a temp buffer then convert
24-
let mut buf = [0; ($size)];
21+
let mut buf = [0; SIZE];
2522
$this.copy_to_slice(&mut buf); // (do the advance)
26-
return $conv(&buf);
23+
return $typ::$conv(buf);
2724
}
2825
});
29-
($this:ident, $buf_size:expr, $conv:path, $len_to_read:expr) => ({
26+
(le => $this:ident, $typ:tt, $len_to_read:expr) => ({
27+
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
28+
3029
// The same trick as above does not improve the best case speed.
3130
// It seems to be linked to the way the method is optimised by the compiler
32-
let mut buf = [0; ($buf_size)];
31+
let mut buf = [0; (mem::size_of::<$typ>())];
3332
$this.copy_to_slice(&mut buf[..($len_to_read)]);
34-
return $conv(&buf[..($len_to_read)], $len_to_read);
33+
return $typ::from_le_bytes(buf);
3534
});
35+
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{
36+
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
37+
38+
let mut buf = [0; (mem::size_of::<$typ>())];
39+
$this.copy_to_slice(&mut buf[mem::size_of::<$typ>()-($len_to_read)..]);
40+
return $typ::from_be_bytes(buf);
41+
}};
3642
}
3743

3844
/// Read bytes from a buffer.
@@ -314,7 +320,7 @@ pub trait Buf {
314320
///
315321
/// This function panics if there is not enough remaining data in `self`.
316322
fn get_u16(&mut self) -> u16 {
317-
buf_get_impl!(self, 2, BigEndian::read_u16);
323+
buf_get_impl!(self, u16::from_be_bytes);
318324
}
319325

320326
/// Gets an unsigned 16 bit integer from `self` in little-endian byte order.
@@ -334,7 +340,7 @@ pub trait Buf {
334340
///
335341
/// This function panics if there is not enough remaining data in `self`.
336342
fn get_u16_le(&mut self) -> u16 {
337-
buf_get_impl!(self, 2, LittleEndian::read_u16);
343+
buf_get_impl!(self, u16::from_le_bytes);
338344
}
339345

340346
/// Gets a signed 16 bit integer from `self` in big-endian byte order.
@@ -354,7 +360,7 @@ pub trait Buf {
354360
///
355361
/// This function panics if there is not enough remaining data in `self`.
356362
fn get_i16(&mut self) -> i16 {
357-
buf_get_impl!(self, 2, BigEndian::read_i16);
363+
buf_get_impl!(self, i16::from_be_bytes);
358364
}
359365

360366
/// Gets a signed 16 bit integer from `self` in little-endian byte order.
@@ -374,7 +380,7 @@ pub trait Buf {
374380
///
375381
/// This function panics if there is not enough remaining data in `self`.
376382
fn get_i16_le(&mut self) -> i16 {
377-
buf_get_impl!(self, 2, LittleEndian::read_i16);
383+
buf_get_impl!(self, i16::from_le_bytes);
378384
}
379385

380386
/// Gets an unsigned 32 bit integer from `self` in the big-endian byte order.
@@ -394,7 +400,7 @@ pub trait Buf {
394400
///
395401
/// This function panics if there is not enough remaining data in `self`.
396402
fn get_u32(&mut self) -> u32 {
397-
buf_get_impl!(self, 4, BigEndian::read_u32);
403+
buf_get_impl!(self, u32::from_be_bytes);
398404
}
399405

400406
/// Gets an unsigned 32 bit integer from `self` in the little-endian byte order.
@@ -414,7 +420,7 @@ pub trait Buf {
414420
///
415421
/// This function panics if there is not enough remaining data in `self`.
416422
fn get_u32_le(&mut self) -> u32 {
417-
buf_get_impl!(self, 4, LittleEndian::read_u32);
423+
buf_get_impl!(self, u32::from_le_bytes);
418424
}
419425

420426
/// Gets a signed 32 bit integer from `self` in big-endian byte order.
@@ -434,7 +440,7 @@ pub trait Buf {
434440
///
435441
/// This function panics if there is not enough remaining data in `self`.
436442
fn get_i32(&mut self) -> i32 {
437-
buf_get_impl!(self, 4, BigEndian::read_i32);
443+
buf_get_impl!(self, i32::from_be_bytes);
438444
}
439445

440446
/// Gets a signed 32 bit integer from `self` in little-endian byte order.
@@ -454,7 +460,7 @@ pub trait Buf {
454460
///
455461
/// This function panics if there is not enough remaining data in `self`.
456462
fn get_i32_le(&mut self) -> i32 {
457-
buf_get_impl!(self, 4, LittleEndian::read_i32);
463+
buf_get_impl!(self, i32::from_le_bytes);
458464
}
459465

460466
/// Gets an unsigned 64 bit integer from `self` in big-endian byte order.
@@ -474,7 +480,7 @@ pub trait Buf {
474480
///
475481
/// This function panics if there is not enough remaining data in `self`.
476482
fn get_u64(&mut self) -> u64 {
477-
buf_get_impl!(self, 8, BigEndian::read_u64);
483+
buf_get_impl!(self, u64::from_be_bytes);
478484
}
479485

480486
/// Gets an unsigned 64 bit integer from `self` in little-endian byte order.
@@ -494,7 +500,7 @@ pub trait Buf {
494500
///
495501
/// This function panics if there is not enough remaining data in `self`.
496502
fn get_u64_le(&mut self) -> u64 {
497-
buf_get_impl!(self, 8, LittleEndian::read_u64);
503+
buf_get_impl!(self, u64::from_le_bytes);
498504
}
499505

500506
/// Gets a signed 64 bit integer from `self` in big-endian byte order.
@@ -514,7 +520,7 @@ pub trait Buf {
514520
///
515521
/// This function panics if there is not enough remaining data in `self`.
516522
fn get_i64(&mut self) -> i64 {
517-
buf_get_impl!(self, 8, BigEndian::read_i64);
523+
buf_get_impl!(self, i64::from_be_bytes);
518524
}
519525

520526
/// Gets a signed 64 bit integer from `self` in little-endian byte order.
@@ -534,7 +540,7 @@ pub trait Buf {
534540
///
535541
/// This function panics if there is not enough remaining data in `self`.
536542
fn get_i64_le(&mut self) -> i64 {
537-
buf_get_impl!(self, 8, LittleEndian::read_i64);
543+
buf_get_impl!(self, i64::from_le_bytes);
538544
}
539545

540546
/// Gets an unsigned 128 bit integer from `self` in big-endian byte order.
@@ -554,7 +560,7 @@ pub trait Buf {
554560
///
555561
/// This function panics if there is not enough remaining data in `self`.
556562
fn get_u128(&mut self) -> u128 {
557-
buf_get_impl!(self, 16, BigEndian::read_u128);
563+
buf_get_impl!(self, u128::from_be_bytes);
558564
}
559565

560566
/// Gets an unsigned 128 bit integer from `self` in little-endian byte order.
@@ -574,7 +580,7 @@ pub trait Buf {
574580
///
575581
/// This function panics if there is not enough remaining data in `self`.
576582
fn get_u128_le(&mut self) -> u128 {
577-
buf_get_impl!(self, 16, LittleEndian::read_u128);
583+
buf_get_impl!(self, u128::from_le_bytes);
578584
}
579585

580586
/// Gets a signed 128 bit integer from `self` in big-endian byte order.
@@ -594,7 +600,7 @@ pub trait Buf {
594600
///
595601
/// This function panics if there is not enough remaining data in `self`.
596602
fn get_i128(&mut self) -> i128 {
597-
buf_get_impl!(self, 16, BigEndian::read_i128);
603+
buf_get_impl!(self, i128::from_be_bytes);
598604
}
599605

600606
/// Gets a signed 128 bit integer from `self` in little-endian byte order.
@@ -614,7 +620,7 @@ pub trait Buf {
614620
///
615621
/// This function panics if there is not enough remaining data in `self`.
616622
fn get_i128_le(&mut self) -> i128 {
617-
buf_get_impl!(self, 16, LittleEndian::read_i128);
623+
buf_get_impl!(self, i128::from_le_bytes);
618624
}
619625

620626
/// Gets an unsigned n-byte integer from `self` in big-endian byte order.
@@ -634,7 +640,7 @@ pub trait Buf {
634640
///
635641
/// This function panics if there is not enough remaining data in `self`.
636642
fn get_uint(&mut self, nbytes: usize) -> u64 {
637-
buf_get_impl!(self, 8, BigEndian::read_uint, nbytes);
643+
buf_get_impl!(be => self, u64, nbytes);
638644
}
639645

640646
/// Gets an unsigned n-byte integer from `self` in little-endian byte order.
@@ -654,7 +660,7 @@ pub trait Buf {
654660
///
655661
/// This function panics if there is not enough remaining data in `self`.
656662
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
657-
buf_get_impl!(self, 8, LittleEndian::read_uint, nbytes);
663+
buf_get_impl!(le => self, u64, nbytes);
658664
}
659665

660666
/// Gets a signed n-byte integer from `self` in big-endian byte order.
@@ -674,7 +680,7 @@ pub trait Buf {
674680
///
675681
/// This function panics if there is not enough remaining data in `self`.
676682
fn get_int(&mut self, nbytes: usize) -> i64 {
677-
buf_get_impl!(self, 8, BigEndian::read_int, nbytes);
683+
buf_get_impl!(be => self, i64, nbytes);
678684
}
679685

680686
/// Gets a signed n-byte integer from `self` in little-endian byte order.
@@ -694,7 +700,7 @@ pub trait Buf {
694700
///
695701
/// This function panics if there is not enough remaining data in `self`.
696702
fn get_int_le(&mut self, nbytes: usize) -> i64 {
697-
buf_get_impl!(self, 8, LittleEndian::read_int, nbytes);
703+
buf_get_impl!(le => self, i64, nbytes);
698704
}
699705

700706
/// Gets an IEEE754 single-precision (4 bytes) floating point number from
@@ -715,7 +721,7 @@ pub trait Buf {
715721
///
716722
/// This function panics if there is not enough remaining data in `self`.
717723
fn get_f32(&mut self) -> f32 {
718-
buf_get_impl!(self, 4, BigEndian::read_f32);
724+
f32::from_bits(Self::get_u32(self))
719725
}
720726

721727
/// Gets an IEEE754 single-precision (4 bytes) floating point number from
@@ -736,7 +742,7 @@ pub trait Buf {
736742
///
737743
/// This function panics if there is not enough remaining data in `self`.
738744
fn get_f32_le(&mut self) -> f32 {
739-
buf_get_impl!(self, 4, LittleEndian::read_f32);
745+
f32::from_bits(Self::get_u32_le(self))
740746
}
741747

742748
/// Gets an IEEE754 double-precision (8 bytes) floating point number from
@@ -757,7 +763,7 @@ pub trait Buf {
757763
///
758764
/// This function panics if there is not enough remaining data in `self`.
759765
fn get_f64(&mut self) -> f64 {
760-
buf_get_impl!(self, 8, BigEndian::read_f64);
766+
f64::from_bits(Self::get_u64(self))
761767
}
762768

763769
/// Gets an IEEE754 double-precision (8 bytes) floating point number from
@@ -778,7 +784,7 @@ pub trait Buf {
778784
///
779785
/// This function panics if there is not enough remaining data in `self`.
780786
fn get_f64_le(&mut self) -> f64 {
781-
buf_get_impl!(self, 8, LittleEndian::read_f64);
787+
f64::from_bits(Self::get_u64_le(self))
782788
}
783789

784790
/// Transforms a `Buf` into a concrete buffer.

0 commit comments

Comments
 (0)