From 5d23126d229b475002f45c7ea0e3322dc62f787e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 9 Aug 2022 11:08:31 +1000 Subject: [PATCH] Remove Index implementations The `Index` trait is not required if we provide the `AsRef` trait. Users can just call `as_ref()[0]` to get access to the 0th element of a `Hash`. For times when we take a reference to the whole slice `[..]` using `as_ref()` is capable and arguably more ergonomic. From the `Hash` trait remove the trait bound on `Index` and its associated traits, instead use the trait bound `AsRef` (we already have a trait bound of `Borrow`). Fix all uses of `foo[..]` -> `foo.as_ref()`. --- fuzz/fuzz_targets/ripemd160.rs | 2 +- fuzz/fuzz_targets/sha1.rs | 2 +- fuzz/fuzz_targets/sha256.rs | 2 +- fuzz/fuzz_targets/sha512.rs | 2 +- src/cmp.rs | 16 +++++------ src/hash160.rs | 21 ++++---------- src/hmac.rs | 51 ++++++++-------------------------- src/lib.rs | 15 ++++------ src/ripemd160.rs | 13 +-------- src/serde_macros.rs | 7 ++--- src/sha1.rs | 13 +-------- src/sha256.rs | 24 ++-------------- src/sha256d.rs | 17 ++---------- src/sha256t.rs | 13 +-------- src/sha512.rs | 13 +-------- src/siphash24.rs | 11 -------- src/util.rs | 13 ++------- 17 files changed, 47 insertions(+), 188 deletions(-) diff --git a/fuzz/fuzz_targets/ripemd160.rs b/fuzz/fuzz_targets/ripemd160.rs index ed7ba86..a95d7c4 100644 --- a/fuzz/fuzz_targets/ripemd160.rs +++ b/fuzz/fuzz_targets/ripemd160.rs @@ -15,7 +15,7 @@ fn do_test(data: &[u8]) { rc_engine.input(data); rc_engine.result(&mut rc_hash); - assert_eq!(&our_hash[..], &rc_hash[..]); + assert_eq!(our_hash.as_ref(), rc_hash.as_ref()); } #[cfg(feature = "honggfuzz")] diff --git a/fuzz/fuzz_targets/sha1.rs b/fuzz/fuzz_targets/sha1.rs index f2648db..68f75ea 100644 --- a/fuzz/fuzz_targets/sha1.rs +++ b/fuzz/fuzz_targets/sha1.rs @@ -15,7 +15,7 @@ fn do_test(data: &[u8]) { rc_sha1.input(data); rc_sha1.result(&mut rc_hash); - assert_eq!(&our_hash[..], &rc_hash[..]); + assert_eq!(our_hash.as_ref(), rc_hash.as_ref()); } #[cfg(feature = "honggfuzz")] diff --git a/fuzz/fuzz_targets/sha256.rs b/fuzz/fuzz_targets/sha256.rs index 898002b..485af58 100644 --- a/fuzz/fuzz_targets/sha256.rs +++ b/fuzz/fuzz_targets/sha256.rs @@ -15,7 +15,7 @@ fn do_test(data: &[u8]) { rc_engine.input(data); rc_engine.result(&mut rc_hash); - assert_eq!(&our_hash[..], &rc_hash[..]); + assert_eq!(our_hash.as_ref(), rc_hash.as_ref()); } #[cfg(feature = "honggfuzz")] diff --git a/fuzz/fuzz_targets/sha512.rs b/fuzz/fuzz_targets/sha512.rs index 88a0b29..d388120 100644 --- a/fuzz/fuzz_targets/sha512.rs +++ b/fuzz/fuzz_targets/sha512.rs @@ -15,7 +15,7 @@ fn do_test(data: &[u8]) { rc_engine.input(data); rc_engine.result(&mut rc_hash); - assert_eq!(&our_hash[..], &rc_hash[..]); + assert_eq!(our_hash.as_ref(), rc_hash.as_ref()); } #[cfg(feature = "honggfuzz")] diff --git a/src/cmp.rs b/src/cmp.rs index 3fe1df7..9ddbdee 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -91,7 +91,7 @@ mod benches { let hash_a = sha256::Hash::hash(&[0; 1]); let hash_b = sha256::Hash::hash(&[1; 1]); bh.iter(|| { - fixed_time_eq(&hash_a[..], &hash_b[..]) + fixed_time_eq(hash_a.as_ref(), hash_b.as_ref()) }) } @@ -100,7 +100,7 @@ mod benches { let hash_a = sha256::Hash::hash(&[0; 1]); let hash_b = sha256::Hash::hash(&[1; 1]); bh.iter(|| { - &hash_a[..] == &hash_b[..] + hash_a.as_ref() == hash_b.as_ref() }) } @@ -109,7 +109,7 @@ mod benches { let hash_a = sha256::Hash::hash(&[0; 1]); let hash_b = sha256::Hash::hash(&[0; 1]); bh.iter(|| { - fixed_time_eq(&hash_a[..], &hash_b[..]) + fixed_time_eq(hash_a.as_ref(), hash_b.as_ref()) }) } @@ -118,7 +118,7 @@ mod benches { let hash_a = sha256::Hash::hash(&[0; 1]); let hash_b = sha256::Hash::hash(&[0; 1]); bh.iter(|| { - &hash_a[..] == &hash_b[..] + hash_a.as_ref() == hash_b.as_ref() }) } @@ -127,7 +127,7 @@ mod benches { let hash_a = sha512::Hash::hash(&[0; 1]); let hash_b = sha512::Hash::hash(&[1; 1]); bh.iter(|| { - fixed_time_eq(&hash_a[..], &hash_b[..]) + fixed_time_eq(hash_a.as_ref(), hash_b.as_ref()) }) } @@ -136,7 +136,7 @@ mod benches { let hash_a = sha512::Hash::hash(&[0; 1]); let hash_b = sha512::Hash::hash(&[1; 1]); bh.iter(|| { - &hash_a[..] == &hash_b[..] + hash_a.as_ref() == hash_b.as_ref() }) } @@ -145,7 +145,7 @@ mod benches { let hash_a = sha512::Hash::hash(&[0; 1]); let hash_b = sha512::Hash::hash(&[0; 1]); bh.iter(|| { - fixed_time_eq(&hash_a[..], &hash_b[..]) + fixed_time_eq(hash_a.as_ref(), hash_b.as_ref()) }) } @@ -154,7 +154,7 @@ mod benches { let hash_a = sha512::Hash::hash(&[0; 1]); let hash_b = sha512::Hash::hash(&[0; 1]); bh.iter(|| { - &hash_a[..] == &hash_b[..] + hash_a.as_ref() == hash_b.as_ref() }) } } diff --git a/src/hash160.rs b/src/hash160.rs index 605da69..720c172 100644 --- a/src/hash160.rs +++ b/src/hash160.rs @@ -21,8 +21,6 @@ //! use core::str; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, hex, ripemd160, sha256}; @@ -41,15 +39,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 20); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl str::FromStr for Hash { type Err = hex::Error; fn from_str(s: &str) -> Result { @@ -67,10 +56,10 @@ impl crate::Hash for Hash { fn from_engine(e: sha256::HashEngine) -> Hash { let sha2 = sha256::Hash::from_engine(e); - let rmd = ripemd160::Hash::hash(&sha2[..]); + let rmd = ripemd160::Hash::hash(sha2.as_ref()); let mut ret = [0; 20]; - ret.copy_from_slice(&rmd[..]); + ret.copy_from_slice(rmd.as_ref()); Hash(ret) } @@ -143,9 +132,9 @@ mod tests { for test in tests { // Hash through high-level API, check hex encoding/decoding - let hash = hash160::Hash::hash(&test.input[..]); + let hash = hash160::Hash::hash(test.input.as_ref()); assert_eq!(hash, hash160::Hash::from_hex(test.output_str).expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(&hash.to_hex(), &test.output_str); // Hash through engine, checking that we can input byte by byte @@ -155,7 +144,7 @@ mod tests { } let manual_hash = Hash::from_engine(engine); assert_eq!(hash, manual_hash); - assert_eq!(hash.into_inner()[..].as_ref(), test.output.as_slice()); + assert_eq!(hash.into_inner().as_ref(), test.output.as_slice()); } } diff --git a/src/hmac.rs b/src/hmac.rs index 01bdc78..cbc5be6 100644 --- a/src/hmac.rs +++ b/src/hmac.rs @@ -20,7 +20,7 @@ //! Hash-based Message Authentication Code (HMAC). //! -use core::{borrow, fmt, ops, str}; +use core::{borrow, convert, fmt, str}; #[cfg(feature = "serde")] use serde::{Serialize, Serializer, Deserialize, Deserializer}; @@ -81,10 +81,10 @@ impl HmacEngine { if key.len() > T::Engine::BLOCK_SIZE { let hash = ::hash(key); - for (b_i, b_h) in ipad.iter_mut().zip(&hash[..]) { + for (b_i, b_h) in ipad.iter_mut().zip(hash.as_ref()) { *b_i ^= *b_h; } - for (b_o, b_h) in opad.iter_mut().zip(&hash[..]) { + for (b_o, b_h) in opad.iter_mut().zip(hash.as_ref()) { *b_o ^= *b_h; } } else { @@ -149,44 +149,15 @@ impl fmt::LowerHex for Hmac { } } -impl ops::Index for Hmac { - type Output = u8; - fn index(&self, index: usize) -> &u8 { - &self.0[index] - } -} - -impl ops::Index> for Hmac { - type Output = [u8]; - fn index(&self, index: ops::Range) -> &[u8] { - &self.0[index] - } -} - -impl ops::Index> for Hmac { - type Output = [u8]; - fn index(&self, index: ops::RangeFrom) -> &[u8] { - &self.0[index] - } -} - -impl ops::Index> for Hmac { - type Output = [u8]; - fn index(&self, index: ops::RangeTo) -> &[u8] { - &self.0[index] - } -} - -impl ops::Index for Hmac { - type Output = [u8]; - fn index(&self, index: ops::RangeFull) -> &[u8] { - &self.0[index] +impl borrow::Borrow<[u8]> for Hmac { + fn borrow(&self) -> &[u8] { + self.0.borrow() } } -impl borrow::Borrow<[u8]> for Hmac { - fn borrow(&self) -> &[u8] { - &self[..] +impl convert::AsRef<[u8]> for Hmac { + fn as_ref(&self) -> &[u8] { + self.0.as_ref() } } @@ -196,7 +167,7 @@ impl Hash for Hmac { fn from_engine(mut e: HmacEngine) -> Hmac { let ihash = T::from_engine(e.iengine); - e.oengine.input(&ihash[..]); + e.oengine.input(ihash.as_ref()); let ohash = T::from_engine(e.oengine); Hmac(ohash) } @@ -364,7 +335,7 @@ mod tests { let mut engine = HmacEngine::::new(&test.key); engine.input(&test.input); let hash = Hmac::::from_engine(engine); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(hash.into_inner()[..].as_ref(), test.output.as_slice()); } } diff --git a/src/lib.rs b/src/lib.rs index 14208ad..cbe67fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ pub mod siphash24; pub mod sha512; pub mod cmp; -use core::{borrow, fmt, hash, ops}; +use core::{borrow, convert, fmt, hash}; pub use hmac::{Hmac, HmacEngine}; pub use error::Error; @@ -98,14 +98,9 @@ pub trait HashEngine: Clone + Default { } /// Trait which applies to hashes of all types. -pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord + - hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex + - ops::Index + - ops::Index, Output = [u8]> + - ops::Index, Output = [u8]> + - ops::Index, Output = [u8]> + - ops::Index + - borrow::Borrow<[u8]> +pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord + + hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex + + borrow::Borrow<[u8]> + convert::AsRef<[u8]> { /// A hashing engine which bytes can be serialized into. It is expected /// to implement the `io::Write` trait, and to never return errors under @@ -169,7 +164,7 @@ mod tests { fn convert_newtypes() { let h1 = TestNewtype::hash(&[]); let h2: TestNewtype2 = h1.as_hash().into(); - assert_eq!(&h1[..], &h2[..]); + assert_eq!(h1.as_ref(), h2.as_ref()); let h = sha256d::Hash::hash(&[]); let h2: TestNewtype = h.to_string().parse().unwrap(); diff --git a/src/ripemd160.rs b/src/ripemd160.rs index f1035c2..f235d6b 100644 --- a/src/ripemd160.rs +++ b/src/ripemd160.rs @@ -21,8 +21,6 @@ //! use core::{cmp, str}; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, HashEngine as _, hex, util}; @@ -89,15 +87,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 20); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl str::FromStr for Hash { type Err = hex::Error; fn from_str(s: &str) -> Result { @@ -527,7 +516,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = ripemd160::Hash::hash(&test.input.as_bytes()); assert_eq!(hash, ripemd160::Hash::from_hex(test.output_str).expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(&hash.to_hex(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/src/serde_macros.rs b/src/serde_macros.rs index 16a316a..d74afda 100644 --- a/src/serde_macros.rs +++ b/src/serde_macros.rs @@ -22,7 +22,7 @@ pub mod serde_details { use crate::Error; use core::marker::PhantomData; - use core::{fmt, ops, str}; + use core::{convert, fmt, str}; use core::str::FromStr; struct HexVisitor(PhantomData); use serde::{de, Serializer, Deserializer}; @@ -90,8 +90,7 @@ pub mod serde_details { Self: Sized + FromStr + fmt::Display - + ops::Index - + ops::Index, + + convert::AsRef<[u8]>, ::Err: fmt::Display, { /// Size, in bits, of the hash. @@ -105,7 +104,7 @@ pub mod serde_details { if s.is_human_readable() { s.collect_str(self) } else { - s.serialize_bytes(&self[..]) + s.serialize_bytes(self.as_ref()) } } diff --git a/src/sha1.rs b/src/sha1.rs index ad56e01..ed85e12 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -16,8 +16,6 @@ //! use core::{cmp, str}; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, HashEngine as _, hex, util}; @@ -84,15 +82,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 20); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl str::FromStr for Hash { type Err = hex::Error; fn from_str(s: &str) -> Result { @@ -253,7 +242,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha1::Hash::hash(&test.input.as_bytes()); assert_eq!(hash, sha1::Hash::from_hex(test.output_str).expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(&hash.to_hex(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/src/sha256.rs b/src/sha256.rs index 90f5200..5eb5710 100644 --- a/src/sha256.rs +++ b/src/sha256.rs @@ -16,8 +16,6 @@ //! use core::{cmp, str}; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, HashEngine as _, hex, util}; @@ -91,15 +89,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 32); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl crate::Hash for Hash { type Engine = HashEngine; type Inner = [u8; 32]; @@ -174,15 +163,6 @@ hex_fmt_impl!(LowerHex, Midstate); serde_impl!(Midstate, 32); borrow_slice_impl!(Midstate); -impl> Index for Midstate { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl str::FromStr for Midstate { type Err = hex::Error; fn from_str(s: &str) -> Result { @@ -262,7 +242,7 @@ impl HashEngine { assert!(length % BLOCK_SIZE == 0, "length is no multiple of the block size"); let mut ret = [0; 8]; - for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate[..].chunks(4)) { + for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate.as_ref().chunks(4)) { *ret_val = util::slice_to_u32_be(midstate_bytes); } @@ -424,7 +404,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha256::Hash::hash(&test.input.as_bytes()); assert_eq!(hash, sha256::Hash::from_hex(test.output_str).expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(&hash.to_hex(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/src/sha256d.rs b/src/sha256d.rs index ecc6f64..53b2238 100644 --- a/src/sha256d.rs +++ b/src/sha256d.rs @@ -16,8 +16,6 @@ //! use core::str; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, hex, sha256}; @@ -36,15 +34,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 32); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl str::FromStr for Hash { type Err = hex::Error; fn from_str(s: &str) -> Result { @@ -62,10 +51,10 @@ impl crate::Hash for Hash { fn from_engine(e: sha256::HashEngine) -> Hash { let sha2 = sha256::Hash::from_engine(e); - let sha2d = sha256::Hash::hash(&sha2[..]); + let sha2d = sha256::Hash::hash(sha2.as_ref()); let mut ret = [0; 32]; - ret.copy_from_slice(&sha2d[..]); + ret.copy_from_slice(sha2d.as_ref()); Hash(ret) } @@ -133,7 +122,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha256d::Hash::hash(&test.input.as_bytes()); assert_eq!(hash, sha256d::Hash::from_hex(test.output_str).expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(&hash.to_hex(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/src/sha256t.rs b/src/sha256t.rs index bd5c0c2..334fd97 100644 --- a/src/sha256t.rs +++ b/src/sha256t.rs @@ -18,8 +18,6 @@ use core::{cmp, str}; #[cfg(feature = "serde")] use core::fmt; use core::marker::PhantomData; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, hex, sha256}; #[cfg(feature="serde")] use crate::Hash as _; @@ -85,15 +83,6 @@ hex_fmt_impl!(Display, Hash, T:Tag); hex_fmt_impl!(LowerHex, Hash, T:Tag); borrow_slice_impl!(Hash, T:Tag); -impl, T: Tag> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl crate::Hash for Hash { type Engine = sha256::HashEngine; type Inner = [u8; 32]; @@ -172,7 +161,7 @@ impl serde::Serialize for Hash { if s.is_human_readable() { s.collect_str(self) } else { - s.serialize_bytes(&self[..]) + s.serialize_bytes(self.as_ref()) } } } diff --git a/src/sha512.rs b/src/sha512.rs index 734404f..52c078b 100644 --- a/src/sha512.rs +++ b/src/sha512.rs @@ -21,8 +21,6 @@ //! use core::{cmp, hash, str}; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, HashEngine as _, hex, util}; @@ -140,15 +138,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 64); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl crate::Hash for Hash { type Engine = HashEngine; type Inner = [u8; 64]; @@ -412,7 +401,7 @@ mod tests { // Hash through high-level API, check hex encoding/decoding let hash = sha512::Hash::hash(&test.input.as_bytes()); assert_eq!(hash, sha512::Hash::from_hex(test.output_str).expect("parse hex")); - assert_eq!(&hash[..], &test.output[..]); + assert_eq!(hash.as_ref(), &test.output[..]); assert_eq!(&hash.to_hex(), &test.output_str); // Hash through engine, checking that we can input byte by byte diff --git a/src/siphash24.rs b/src/siphash24.rs index f098428..385b00b 100644 --- a/src/siphash24.rs +++ b/src/siphash24.rs @@ -21,8 +21,6 @@ //! use core::{cmp, mem, ptr, str}; -use core::ops::Index; -use core::slice::SliceIndex; use crate::{Error, Hash as _, HashEngine as _, hex, util}; @@ -209,15 +207,6 @@ hex_fmt_impl!(LowerHex, Hash); serde_impl!(Hash, 8); borrow_slice_impl!(Hash); -impl> Index for Hash { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } -} - impl str::FromStr for Hash { type Err = hex::Error; fn from_str(s: &str) -> Result { diff --git a/src/util.rs b/src/util.rs index 7718579..9b83c7b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -43,13 +43,13 @@ macro_rules! borrow_slice_impl( ($ty:ident, $($gen:ident: $gent:ident),*) => ( impl<$($gen: $gent),*> $crate::_export::_core::borrow::Borrow<[u8]> for $ty<$($gen),*> { fn borrow(&self) -> &[u8] { - &self[..] + self.0.borrow() } } impl<$($gen: $gent),*> $crate::_export::_core::convert::AsRef<[u8]> for $ty<$($gen),*> { fn as_ref(&self) -> &[u8] { - &self[..] + self.0.as_ref() } } ) @@ -241,15 +241,6 @@ macro_rules! hash_newtype { $crate::hex::FromHex::from_hex(s) } } - - impl> $crate::_export::_core::ops::Index for $newtype { - type Output = I::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - &self.0[index] - } - } }; }