From 306e14a20b5c327e1ffee1c51e4f9cd37050bda5 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Sun, 23 May 2021 19:02:05 -0700 Subject: [PATCH] deps: remove byteorder This takes #15 and shrinks its diff considerably. Namely, we match the byteorder API a bit more exactly, remove all changes to dense/sparse/state_id and keep the minimum things needed in lib.rs and Cargo.toml. While improvements are in general welcome, all of this machinery is gone in the rewrite. The only point of this change is to satisfy a request to remove a dependency, of which I am sympathetic to. Minimizing the diff is important for sanely rebasing my rewrite branch on top of this. Otherwise, removing this dependency probably becomes more annoying than it's worth. Closes #15 --- Cargo.toml | 1 - src/byteorder.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/byteorder.rs diff --git a/Cargo.toml b/Cargo.toml index 2f5eb41c..81588064 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,6 @@ std = ["regex-syntax"] transducer = ["std", "fst"] [dependencies] -byteorder = { version = "1.2.7", default-features = false } fst = { version = "0.4.0", optional = true } regex-syntax = { version = "0.6.16", optional = true } diff --git a/src/byteorder.rs b/src/byteorder.rs new file mode 100644 index 00000000..e909f93a --- /dev/null +++ b/src/byteorder.rs @@ -0,0 +1,76 @@ +use core::convert::TryInto; + +pub trait ByteOrder { + fn read_u16(buf: &[u8]) -> u16; + fn read_u32(buf: &[u8]) -> u32; + fn read_u64(buf: &[u8]) -> u64; + fn read_uint(buf: &[u8], nbytes: usize) -> u64; + fn write_u16(buf: &mut [u8], n: u16); + fn write_u32(buf: &mut [u8], n: u32); + fn write_u64(buf: &mut [u8], n: u64); + fn write_uint(buf: &mut [u8], n: u64, nbytes: usize); +} + +pub enum BigEndian {} +pub enum LittleEndian {} +pub enum NativeEndian {} + +macro_rules! impl_endian { + ($t:ty, $from_endian:ident, $to_endian:ident) => { + impl ByteOrder for $t { + #[inline] + fn read_u16(buf: &[u8]) -> u16 { + u16::$from_endian(buf[0..2].try_into().unwrap()) + } + + #[inline] + fn read_u32(buf: &[u8]) -> u32 { + u32::$from_endian(buf[0..4].try_into().unwrap()) + } + + #[inline] + fn read_u64(buf: &[u8]) -> u64 { + u64::$from_endian(buf[0..8].try_into().unwrap()) + } + + #[inline] + fn read_uint(buf: &[u8], nbytes: usize) -> u64 { + let mut dst = [0u8; 8]; + dst[..nbytes].copy_from_slice(&buf[..nbytes]); + u64::$from_endian(dst) + } + + #[inline] + fn write_u16(buf: &mut [u8], n: u16) { + buf[0..2].copy_from_slice(&n.$to_endian()[..]); + } + + #[inline] + fn write_u32(buf: &mut [u8], n: u32) { + buf[0..4].copy_from_slice(&n.$to_endian()[..]); + } + + #[inline] + fn write_u64(buf: &mut [u8], n: u64) { + buf[0..8].copy_from_slice(&n.$to_endian()[..]); + } + + #[inline] + fn write_uint(buf: &mut [u8], n: u64, nbytes: usize) { + buf[..nbytes].copy_from_slice(&n.$to_endian()[..nbytes]); + } + } + }; +} + +impl_endian! { + BigEndian, from_be_bytes, to_be_bytes +} + +impl_endian! { + LittleEndian, from_le_bytes, to_le_bytes +} + +impl_endian! { + NativeEndian, from_ne_bytes, to_ne_bytes +} diff --git a/src/lib.rs b/src/lib.rs index 4d3e9c1e..7894ecce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -290,7 +290,6 @@ extern crate core; #[cfg(all(test, feature = "transducer"))] extern crate bstr; -extern crate byteorder; #[cfg(feature = "transducer")] extern crate fst; #[cfg(feature = "std")] @@ -306,6 +305,7 @@ pub use regex::RegexBuilder; pub use sparse::SparseDFA; pub use state_id::StateID; +mod byteorder; mod classes; #[path = "dense.rs"] mod dense_imp;