Use Rust's built-in unsigned 128 bit integer instead of the extprim crate

This commit is contained in:
Will Stott 2019-07-23 20:04:17 +01:00
parent 551daadc44
commit ac1c31b786
4 changed files with 15 additions and 54 deletions

20
Cargo.lock generated
View file

@ -492,10 +492,7 @@ name = "env_logger"
version = "0.6.2" version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ 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)", "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)", "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)", "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]] [[package]]
name = "failure" name = "failure"
version = "0.1.5" version = "0.1.5"
@ -890,7 +875,6 @@ dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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)", "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)", "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)", "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)", "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" name = "serde"
version = "1.0.97" version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index" 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]] [[package]]
name = "serde_derive" 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.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 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 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 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 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" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"

View file

@ -12,7 +12,6 @@ base64 = "0.10"
byteorder = "1.3" byteorder = "1.3"
bytes = "0.4" bytes = "0.4"
error-chain = { version = "0.12", default_features = false } error-chain = { version = "0.12", default_features = false }
extprim = "1.7"
futures = "0.1" futures = "0.1"
httparse = "1.3" httparse = "1.3"
hyper = "0.11" hyper = "0.11"

View file

@ -14,7 +14,6 @@ extern crate serde_derive;
extern crate base64; extern crate base64;
extern crate byteorder; extern crate byteorder;
extern crate bytes; extern crate bytes;
extern crate extprim;
extern crate httparse; extern crate httparse;
extern crate hyper; extern crate hyper;
extern crate hyper_proxy; extern crate hyper_proxy;

View file

@ -1,5 +1,3 @@
use byteorder::{BigEndian, ByteOrder};
use extprim::u128::u128;
use std; use std;
use std::fmt; use std::fmt;
@ -16,14 +14,14 @@ impl SpotifyId {
pub fn from_base16(id: &str) -> Result<SpotifyId, SpotifyIdError> { pub fn from_base16(id: &str) -> Result<SpotifyId, SpotifyIdError> {
let data = id.as_bytes(); let data = id.as_bytes();
let mut n: u128 = u128::zero(); let mut n = 0u128;
for c in data { for c in data {
let d = match BASE16_DIGITS.iter().position(|e| e == c) { let d = match BASE16_DIGITS.iter().position(|e| e == c) {
None => return Err(SpotifyIdError), None => return Err(SpotifyIdError),
Some(x) => x as u64, Some(x) => x as u128,
}; };
n = n * u128::new(16); n = n * 16;
n = n + u128::new(d); n = n + d;
} }
Ok(SpotifyId(n)) Ok(SpotifyId(n))
@ -32,14 +30,14 @@ impl SpotifyId {
pub fn from_base62(id: &str) -> Result<SpotifyId, SpotifyIdError> { pub fn from_base62(id: &str) -> Result<SpotifyId, SpotifyIdError> {
let data = id.as_bytes(); let data = id.as_bytes();
let mut n: u128 = u128::zero(); let mut n = 0u128;
for c in data { for c in data {
let d = match BASE62_DIGITS.iter().position(|e| e == c) { let d = match BASE62_DIGITS.iter().position(|e| e == c) {
None => return Err(SpotifyIdError), None => return Err(SpotifyIdError),
Some(x) => x as u64, Some(x) => x as u128,
}; };
n = n * u128::new(62); n = n * 62;
n = n + u128::new(d); n = n + d;
} }
Ok(SpotifyId(n)) Ok(SpotifyId(n))
@ -50,45 +48,30 @@ impl SpotifyId {
return Err(SpotifyIdError); return Err(SpotifyIdError);
}; };
let high = BigEndian::read_u64(&data[0..8]); let mut arr: [u8; 16] = Default::default();
let low = BigEndian::read_u64(&data[8..16]); 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 { pub fn to_base16(&self) -> String {
let &SpotifyId(ref n) = self; format!("{:0>32}", format!("{:x}", self.0))
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()
} }
pub fn to_base62(&self) -> String { pub fn to_base62(&self) -> String {
let &SpotifyId(mut n) = self; let &SpotifyId(mut n) = self;
let mut data = [0u8; 22]; let mut data = [0u8; 22];
let sixty_two = u128::new(62);
for i in 0..22 { for i in 0..22 {
data[21 - i] = BASE62_DIGITS[(n % sixty_two).low64() as usize]; data[21 - i] = BASE62_DIGITS[(n % 62) as usize];
n /= sixty_two; n /= 62;
} }
std::str::from_utf8(&data).unwrap().to_owned() std::str::from_utf8(&data).unwrap().to_owned()
} }
pub fn to_raw(&self) -> [u8; 16] { pub fn to_raw(&self) -> [u8; 16] {
let &SpotifyId(ref n) = self; self.0.to_be_bytes()
let mut data = [0u8; 16];
BigEndian::write_u64(&mut data[0..8], n.high64());
BigEndian::write_u64(&mut data[8..16], n.low64());
data
} }
} }