Browse Source

Merge pull request #341 from willstott101/u128

Drop extprim crate (use built-in u128) and reduce usage of byteorder crate (Rust 1.32+)
Sasha Hilton 4 years ago
parent
commit
cbba63f60b
4 changed files with 15 additions and 51 deletions
  1. 0 17
      Cargo.lock
  2. 0 1
      core/Cargo.toml
  3. 0 1
      core/src/lib.rs
  4. 15 32
      core/src/spotify_id.rs

+ 0 - 17
Cargo.lock

@@ -507,18 +507,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 +878,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 +1730,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 +2492,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"

+ 0 - 1
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"

+ 0 - 1
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;

+ 15 - 32
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<SpotifyId, SpotifyIdError> {
         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<SpotifyId, SpotifyIdError> {
         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!("{:032x}", 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()
     }
 }