Skip to content

Commit

Permalink
Remove dependency on byteorder
Browse files Browse the repository at this point in the history
This commit remove `gimli`'s dependency on the `byteorder` crate. While
this crate is quite small and general not expensive as a dependency, I'm
curious to eventually enable the `gimli-symbolize` feature of the
`backtrace` crate in the standard library. The standard library has
odd technical restrictions about what crates can be used in the standard
library, namely that nonstandard `Cargo.toml` configuration is required
to get the build working correctly. In order to inflict this oddness on
as few crates as possible, and to minimize the amount of auditing the
standard library needs to do, I'm hoping to remove most dependencies
from the `gimli-symbolize` dependency tree of the `backtrace` crate.

The usage of `byteorder` in `gimli` was quite light and pretty easy to
remove, especially now that `{to,from}_{le,be}_bytes` methods have been
stable on integers from the standard library for some time now!
  • Loading branch information
alexcrichton committed May 7, 2020
1 parent f346d6f commit 4b6a09d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ coveralls = { repository = "gimli-rs/gimli" }

[dependencies]
arrayvec = { version = "0.5.0", default-features = false, optional = true }
byteorder = { version = "1.0", default-features = false }
fallible-iterator = { version = "0.2.0", default-features = false, optional = true }
indexmap = { version = "1.0.2", optional = true }
smallvec = { version = "1.1.0", default-features = false, optional = true }
Expand Down
51 changes: 29 additions & 22 deletions src/endianity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Types for compile-time and run-time endianity.

use byteorder;
use byteorder::ByteOrder;
use core::convert::TryInto;
use core::fmt::Debug;

/// A trait describing the endianity of some buffer.
Expand All @@ -22,10 +21,11 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 2`.
#[inline]
fn read_u16(self, buf: &[u8]) -> u16 {
let bytes: &[u8; 2] = buf[..2].try_into().unwrap();
if self.is_big_endian() {
byteorder::BigEndian::read_u16(buf)
u16::from_be_bytes(*bytes)
} else {
byteorder::LittleEndian::read_u16(buf)
u16::from_le_bytes(*bytes)
}
}

Expand All @@ -36,10 +36,11 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 4`.
#[inline]
fn read_u32(self, buf: &[u8]) -> u32 {
let bytes: &[u8; 4] = buf[..4].try_into().unwrap();
if self.is_big_endian() {
byteorder::BigEndian::read_u32(buf)
u32::from_be_bytes(*bytes)
} else {
byteorder::LittleEndian::read_u32(buf)
u32::from_le_bytes(*bytes)
}
}

Expand All @@ -50,10 +51,11 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 8`.
#[inline]
fn read_u64(self, buf: &[u8]) -> u64 {
let bytes: &[u8; 8] = buf[..8].try_into().unwrap();
if self.is_big_endian() {
byteorder::BigEndian::read_u64(buf)
u64::from_be_bytes(*bytes)
} else {
byteorder::LittleEndian::read_u64(buf)
u64::from_le_bytes(*bytes)
}
}

Expand All @@ -64,11 +66,13 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 1` or `buf.len() > 8`.
#[inline]
fn read_uint(&mut self, buf: &[u8]) -> u64 {
let mut tmp = [0; 8];
if self.is_big_endian() {
byteorder::BigEndian::read_uint(buf, buf.len())
tmp[8 - buf.len()..].copy_from_slice(buf);
} else {
byteorder::LittleEndian::read_uint(buf, buf.len())
tmp[..buf.len()].copy_from_slice(buf);
}
self.read_u64(&tmp)
}

/// Reads a signed 16 bit integer from `buf`.
Expand Down Expand Up @@ -128,11 +132,12 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 2`.
#[inline]
fn write_u16(self, buf: &mut [u8], n: u16) {
if self.is_big_endian() {
byteorder::BigEndian::write_u16(buf, n)
let bytes = if self.is_big_endian() {
n.to_be_bytes()
} else {
byteorder::LittleEndian::write_u16(buf, n)
}
n.to_le_bytes()
};
buf[..2].copy_from_slice(&bytes);
}

/// Writes an unsigned 32 bit integer `n` to `buf`.
Expand All @@ -142,11 +147,12 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 4`.
#[inline]
fn write_u32(self, buf: &mut [u8], n: u32) {
if self.is_big_endian() {
byteorder::BigEndian::write_u32(buf, n)
let bytes = if self.is_big_endian() {
n.to_be_bytes()
} else {
byteorder::LittleEndian::write_u32(buf, n)
}
n.to_le_bytes()
};
buf[..4].copy_from_slice(&bytes);
}

/// Writes an unsigned 64 bit integer `n` to `buf`.
Expand All @@ -156,11 +162,12 @@ pub trait Endianity: Debug + Default + Clone + Copy + PartialEq + Eq {
/// Panics when `buf.len() < 8`.
#[inline]
fn write_u64(self, buf: &mut [u8], n: u64) {
if self.is_big_endian() {
byteorder::BigEndian::write_u64(buf, n)
let bytes = if self.is_big_endian() {
n.to_be_bytes()
} else {
byteorder::LittleEndian::write_u64(buf, n)
}
n.to_le_bytes()
};
buf[..8].copy_from_slice(&bytes);
}
}

Expand Down

0 comments on commit 4b6a09d

Please sign in to comment.