From 4d8e4788feb273f0f39385aa76c1abff14e224e4 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 12 Dec 2020 15:30:00 +0100 Subject: [PATCH 1/4] Test and simplify Hash::into_array --- lib/cache/src/hash.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/cache/src/hash.rs b/lib/cache/src/hash.rs index b9a69fd7965..def877a59af 100644 --- a/lib/cache/src/hash.rs +++ b/lib/cache/src/hash.rs @@ -21,9 +21,7 @@ impl Hash { } pub(crate) fn into_array(self) -> [u8; 32] { - let mut total = [0u8; 32]; - total[0..32].copy_from_slice(&self.0); - total + self.0 } } @@ -56,3 +54,19 @@ impl FromStr for Hash { })?)) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hash_into_array_works() { + let original = [ + 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, + 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, + 0xEE, 0xFF, 0x12, 0x65, + ]; + let hash = Hash::new(original); + assert_eq!(hash.into_array(), original); + } +} From 830f6a2d1d1efa78fa76bb516c78e058b5cdccf5 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 12 Dec 2020 15:30:14 +0100 Subject: [PATCH 2/4] Avoid unnecessary cast --- lib/cache/src/hash.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cache/src/hash.rs b/lib/cache/src/hash.rs index def877a59af..0ad04300dd5 100644 --- a/lib/cache/src/hash.rs +++ b/lib/cache/src/hash.rs @@ -29,7 +29,7 @@ impl ToString for Hash { /// Create the hexadecimal representation of the /// stored hash. fn to_string(&self) -> String { - hex::encode(&self.into_array() as &[u8]) + hex::encode(&self.into_array()) } } From 0ef103901868381741c4413616ce042120de9562 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 12 Dec 2020 15:32:15 +0100 Subject: [PATCH 3/4] Change Hash::into_array -> to_array --- lib/cache/src/hash.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cache/src/hash.rs b/lib/cache/src/hash.rs index 0ad04300dd5..6228c061ef5 100644 --- a/lib/cache/src/hash.rs +++ b/lib/cache/src/hash.rs @@ -20,7 +20,7 @@ impl Hash { Self::new(hash.into()) } - pub(crate) fn into_array(self) -> [u8; 32] { + pub(crate) fn to_array(&self) -> [u8; 32] { self.0 } } @@ -29,7 +29,7 @@ impl ToString for Hash { /// Create the hexadecimal representation of the /// stored hash. fn to_string(&self) -> String { - hex::encode(&self.into_array()) + hex::encode(&self.to_array()) } } @@ -60,13 +60,13 @@ mod tests { use super::*; #[test] - fn hash_into_array_works() { + fn hash_to_array_works() { let original = [ 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x12, 0x65, ]; let hash = Hash::new(original); - assert_eq!(hash.into_array(), original); + assert_eq!(hash.to_array(), original); } } From fcf5de178e6672c76a828b5f71717a51ea5eca87 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 12 Dec 2020 15:36:44 +0100 Subject: [PATCH 4/4] Fix description of Hash::new --- lib/cache/src/hash.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/cache/src/hash.rs b/lib/cache/src/hash.rs index 6228c061ef5..8504413bf06 100644 --- a/lib/cache/src/hash.rs +++ b/lib/cache/src/hash.rs @@ -9,7 +9,9 @@ use std::string::ToString; pub struct Hash([u8; 32]); impl Hash { - /// Creates a new hash. Has to be encodable as a hex format. + /// Creates a new instance from 32 raw bytes. + /// Does not perform any hashing. In order to create a hash from data, + /// use `Hash::generate`. pub fn new(bytes: [u8; 32]) -> Self { Self(bytes) }