From 088fa24be8d6613d413be32d756e00c7ac259f62 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sun, 9 Apr 2023 13:36:38 +1000 Subject: [PATCH] hashes: Derive traits on the sha512::Hash type For all the other hash types we use derive, by way of the `hash_type!` macro, unless I'm missing something we can do the same for `sha512::Hash`. --- hashes/src/sha512.rs | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index 3064f45f..cb53c113 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -23,7 +23,7 @@ use core::convert::TryInto; use core::ops::Index; use core::slice::SliceIndex; -use core::{cmp, hash, str}; +use core::{cmp, str}; use crate::{Error, HashEngine as _}; @@ -80,6 +80,7 @@ impl crate::HashEngine for HashEngine { } /// Output of the SHA512 hash function. +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[repr(transparent)] pub struct Hash( @@ -96,38 +97,6 @@ impl Hash { fn internal_engine() -> HashEngine { Default::default() } } -impl Copy for Hash {} - -impl Clone for Hash { - fn clone(&self) -> Hash { - let mut ret = [0; 64]; - ret.copy_from_slice(&self.0); - Hash(ret) - } -} - -impl PartialEq for Hash { - fn eq(&self, other: &Hash) -> bool { self.0[..] == other.0[..] } -} - -impl Eq for Hash {} - -impl Default for Hash { - fn default() -> Hash { Hash([0; 64]) } -} - -impl PartialOrd for Hash { - fn partial_cmp(&self, other: &Hash) -> Option { self.0.partial_cmp(&other.0) } -} - -impl Ord for Hash { - fn cmp(&self, other: &Hash) -> cmp::Ordering { self.0.cmp(&other.0) } -} - -impl hash::Hash for Hash { - fn hash(&self, state: &mut H) { self.0.hash(state) } -} - #[cfg(not(fuzzing))] pub(crate) fn from_engine(mut e: HashEngine) -> Hash { // pad buffer with a single 1-bit then all 0s, until there are exactly 16 bytes remaining