From ac1c31b78656fa44d8b1a892ad514cbb7ad16fdf Mon Sep 17 00:00:00 2001 From: Will Stott Date: Tue, 23 Jul 2019 20:04:17 +0100 Subject: [PATCH] Use Rust's built-in unsigned 128 bit integer instead of the extprim crate --- Cargo.lock | 20 ------------------ core/Cargo.toml | 1 - core/src/lib.rs | 1 - core/src/spotify_id.rs | 47 ++++++++++++++---------------------------- 4 files changed, 15 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 602b6abb..f8fd3f88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -492,10 +492,7 @@ name = "env_logger" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -507,18 +504,6 @@ dependencies = [ "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "extprim" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", - "semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.97 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "failure" version = "0.1.5" @@ -890,7 +875,6 @@ dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", - "extprim 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)", "hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1743,9 +1727,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "serde" version = "1.0.97" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "serde_derive 1.0.97 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "serde_derive" @@ -2508,7 +2489,6 @@ dependencies = [ "checksum env_logger 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3ddf21e73e016298f5cb37d6ef8e8da8e39f91f9ec8b0df44b7deb16a9f8cd5b" "checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" "checksum error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" -"checksum extprim 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cfba1bd0c749760b3dad3e4d3926b2bf6186f48e244456bfe1ad3aecd55b4fb1" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" diff --git a/core/Cargo.toml b/core/Cargo.toml index 5afb90d2..162147de 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -12,7 +12,6 @@ base64 = "0.10" byteorder = "1.3" bytes = "0.4" error-chain = { version = "0.12", default_features = false } -extprim = "1.7" futures = "0.1" httparse = "1.3" hyper = "0.11" diff --git a/core/src/lib.rs b/core/src/lib.rs index 57be3b9d..e384d14b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -14,7 +14,6 @@ extern crate serde_derive; extern crate base64; extern crate byteorder; extern crate bytes; -extern crate extprim; extern crate httparse; extern crate hyper; extern crate hyper_proxy; diff --git a/core/src/spotify_id.rs b/core/src/spotify_id.rs index c71ea1d7..841c3225 100644 --- a/core/src/spotify_id.rs +++ b/core/src/spotify_id.rs @@ -1,5 +1,3 @@ -use byteorder::{BigEndian, ByteOrder}; -use extprim::u128::u128; use std; use std::fmt; @@ -16,14 +14,14 @@ impl SpotifyId { pub fn from_base16(id: &str) -> Result { let data = id.as_bytes(); - let mut n: u128 = u128::zero(); + let mut n = 0u128; for c in data { let d = match BASE16_DIGITS.iter().position(|e| e == c) { None => return Err(SpotifyIdError), - Some(x) => x as u64, + Some(x) => x as u128, }; - n = n * u128::new(16); - n = n + u128::new(d); + n = n * 16; + n = n + d; } Ok(SpotifyId(n)) @@ -32,14 +30,14 @@ impl SpotifyId { pub fn from_base62(id: &str) -> Result { let data = id.as_bytes(); - let mut n: u128 = u128::zero(); + let mut n = 0u128; for c in data { let d = match BASE62_DIGITS.iter().position(|e| e == c) { None => return Err(SpotifyIdError), - Some(x) => x as u64, + Some(x) => x as u128, }; - n = n * u128::new(62); - n = n + u128::new(d); + n = n * 62; + n = n + d; } Ok(SpotifyId(n)) @@ -50,45 +48,30 @@ impl SpotifyId { return Err(SpotifyIdError); }; - let high = BigEndian::read_u64(&data[0..8]); - let low = BigEndian::read_u64(&data[8..16]); + let mut arr: [u8; 16] = Default::default(); + arr.copy_from_slice(&data[0..16]); - Ok(SpotifyId(u128::from_parts(high, low))) + Ok(SpotifyId(u128::from_be_bytes(arr))) } pub fn to_base16(&self) -> String { - let &SpotifyId(ref n) = self; - - let mut data = [0u8; 32]; - for i in 0..32 { - data[31 - i] = BASE16_DIGITS[(n.wrapping_shr(4 * i as u32).low64() & 0xF) as usize]; - } - - std::str::from_utf8(&data).unwrap().to_owned() + format!("{:0>32}", format!("{:x}", self.0)) } pub fn to_base62(&self) -> String { let &SpotifyId(mut n) = self; let mut data = [0u8; 22]; - let sixty_two = u128::new(62); for i in 0..22 { - data[21 - i] = BASE62_DIGITS[(n % sixty_two).low64() as usize]; - n /= sixty_two; + data[21 - i] = BASE62_DIGITS[(n % 62) as usize]; + n /= 62; } std::str::from_utf8(&data).unwrap().to_owned() } pub fn to_raw(&self) -> [u8; 16] { - let &SpotifyId(ref n) = self; - - let mut data = [0u8; 16]; - - BigEndian::write_u64(&mut data[0..8], n.high64()); - BigEndian::write_u64(&mut data[8..16], n.low64()); - - data + self.0.to_be_bytes() } }