Skip to content

Commit

Permalink
Merge #1923
Browse files Browse the repository at this point in the history
1923: Hash 💅 r=syrusakbary a=webmaster128

# Description

Minor codestyle improvements a ran into by accident.

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Simon Warta <simon@warta.it>
  • Loading branch information
bors[bot] and webmaster128 authored Dec 12, 2020
2 parents a69c9b3 + fcf5de1 commit 3709c9d
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/cache/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -20,18 +22,16 @@ impl Hash {
Self::new(hash.into())
}

pub(crate) fn into_array(self) -> [u8; 32] {
let mut total = [0u8; 32];
total[0..32].copy_from_slice(&self.0);
total
pub(crate) fn to_array(&self) -> [u8; 32] {
self.0
}
}

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.to_array())
}
}

Expand All @@ -56,3 +56,19 @@ impl FromStr for Hash {
})?))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
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.to_array(), original);
}
}

0 comments on commit 3709c9d

Please sign in to comment.