Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 54ebf44

Browse files
committed
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()`.
1 parent 69f68d4 commit 54ebf44

17 files changed

+47
-188
lines changed

Diff for: fuzz/fuzz_targets/ripemd160.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn do_test(data: &[u8]) {
1515
rc_engine.input(data);
1616
rc_engine.result(&mut rc_hash);
1717

18-
assert_eq!(&our_hash[..], &rc_hash[..]);
18+
assert_eq!(our_hash.as_ref(), rc_hash.as_ref());
1919
}
2020

2121
#[cfg(feature = "honggfuzz")]

Diff for: fuzz/fuzz_targets/sha1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn do_test(data: &[u8]) {
1515
rc_sha1.input(data);
1616
rc_sha1.result(&mut rc_hash);
1717

18-
assert_eq!(&our_hash[..], &rc_hash[..]);
18+
assert_eq!(our_hash.as_ref(), rc_hash.as_ref());
1919
}
2020

2121
#[cfg(feature = "honggfuzz")]

Diff for: fuzz/fuzz_targets/sha256.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn do_test(data: &[u8]) {
1515
rc_engine.input(data);
1616
rc_engine.result(&mut rc_hash);
1717

18-
assert_eq!(&our_hash[..], &rc_hash[..]);
18+
assert_eq!(our_hash.as_ref(), rc_hash.as_ref());
1919
}
2020

2121
#[cfg(feature = "honggfuzz")]

Diff for: fuzz/fuzz_targets/sha512.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn do_test(data: &[u8]) {
1515
rc_engine.input(data);
1616
rc_engine.result(&mut rc_hash);
1717

18-
assert_eq!(&our_hash[..], &rc_hash[..]);
18+
assert_eq!(our_hash.as_ref(), rc_hash.as_ref());
1919
}
2020

2121
#[cfg(feature = "honggfuzz")]

Diff for: src/cmp.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ mod benches {
9191
let hash_a = sha256::Hash::hash(&[0; 1]);
9292
let hash_b = sha256::Hash::hash(&[1; 1]);
9393
bh.iter(|| {
94-
fixed_time_eq(&hash_a[..], &hash_b[..])
94+
fixed_time_eq(hash_a.as_ref(), hash_b.as_ref())
9595
})
9696
}
9797

@@ -100,7 +100,7 @@ mod benches {
100100
let hash_a = sha256::Hash::hash(&[0; 1]);
101101
let hash_b = sha256::Hash::hash(&[1; 1]);
102102
bh.iter(|| {
103-
&hash_a[..] == &hash_b[..]
103+
hash_a.as_ref() == hash_b.as_ref()
104104
})
105105
}
106106

@@ -109,7 +109,7 @@ mod benches {
109109
let hash_a = sha256::Hash::hash(&[0; 1]);
110110
let hash_b = sha256::Hash::hash(&[0; 1]);
111111
bh.iter(|| {
112-
fixed_time_eq(&hash_a[..], &hash_b[..])
112+
fixed_time_eq(hash_a.as_ref(), hash_b.as_ref())
113113
})
114114
}
115115

@@ -118,7 +118,7 @@ mod benches {
118118
let hash_a = sha256::Hash::hash(&[0; 1]);
119119
let hash_b = sha256::Hash::hash(&[0; 1]);
120120
bh.iter(|| {
121-
&hash_a[..] == &hash_b[..]
121+
hash_a.as_ref() == hash_b.as_ref()
122122
})
123123
}
124124

@@ -127,7 +127,7 @@ mod benches {
127127
let hash_a = sha512::Hash::hash(&[0; 1]);
128128
let hash_b = sha512::Hash::hash(&[1; 1]);
129129
bh.iter(|| {
130-
fixed_time_eq(&hash_a[..], &hash_b[..])
130+
fixed_time_eq(hash_a.as_ref(), hash_b.as_ref())
131131
})
132132
}
133133

@@ -136,7 +136,7 @@ mod benches {
136136
let hash_a = sha512::Hash::hash(&[0; 1]);
137137
let hash_b = sha512::Hash::hash(&[1; 1]);
138138
bh.iter(|| {
139-
&hash_a[..] == &hash_b[..]
139+
hash_a.as_ref() == hash_b.as_ref()
140140
})
141141
}
142142

@@ -145,7 +145,7 @@ mod benches {
145145
let hash_a = sha512::Hash::hash(&[0; 1]);
146146
let hash_b = sha512::Hash::hash(&[0; 1]);
147147
bh.iter(|| {
148-
fixed_time_eq(&hash_a[..], &hash_b[..])
148+
fixed_time_eq(hash_a.as_ref(), hash_b.as_ref())
149149
})
150150
}
151151

@@ -154,7 +154,7 @@ mod benches {
154154
let hash_a = sha512::Hash::hash(&[0; 1]);
155155
let hash_b = sha512::Hash::hash(&[0; 1]);
156156
bh.iter(|| {
157-
&hash_a[..] == &hash_b[..]
157+
hash_a.as_ref() == hash_b.as_ref()
158158
})
159159
}
160160
}

Diff for: src/hash160.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
//!
2222
2323
use core::str;
24-
use core::ops::Index;
25-
use core::slice::SliceIndex;
2624

2725
use crate::{Error, hex, ripemd160, sha256};
2826

@@ -41,15 +39,6 @@ hex_fmt_impl!(LowerHex, Hash);
4139
serde_impl!(Hash, 20);
4240
borrow_slice_impl!(Hash);
4341

44-
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
45-
type Output = I::Output;
46-
47-
#[inline]
48-
fn index(&self, index: I) -> &Self::Output {
49-
&self.0[index]
50-
}
51-
}
52-
5342
impl str::FromStr for Hash {
5443
type Err = hex::Error;
5544
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -67,10 +56,10 @@ impl crate::Hash for Hash {
6756

6857
fn from_engine(e: sha256::HashEngine) -> Hash {
6958
let sha2 = sha256::Hash::from_engine(e);
70-
let rmd = ripemd160::Hash::hash(&sha2[..]);
59+
let rmd = ripemd160::Hash::hash(sha2.as_ref());
7160

7261
let mut ret = [0; 20];
73-
ret.copy_from_slice(&rmd[..]);
62+
ret.copy_from_slice(rmd.as_ref());
7463
Hash(ret)
7564
}
7665

@@ -143,9 +132,9 @@ mod tests {
143132

144133
for test in tests {
145134
// Hash through high-level API, check hex encoding/decoding
146-
let hash = hash160::Hash::hash(&test.input[..]);
135+
let hash = hash160::Hash::hash(test.input.as_ref());
147136
assert_eq!(hash, hash160::Hash::from_hex(test.output_str).expect("parse hex"));
148-
assert_eq!(&hash[..], &test.output[..]);
137+
assert_eq!(hash.as_ref(), &test.output[..]);
149138
assert_eq!(&hash.to_hex(), &test.output_str);
150139

151140
// Hash through engine, checking that we can input byte by byte
@@ -155,7 +144,7 @@ mod tests {
155144
}
156145
let manual_hash = Hash::from_engine(engine);
157146
assert_eq!(hash, manual_hash);
158-
assert_eq!(hash.into_inner()[..].as_ref(), test.output.as_slice());
147+
assert_eq!(hash.into_inner().as_ref(), test.output.as_slice());
159148
}
160149
}
161150

Diff for: src/hmac.rs

+11-40
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! Hash-based Message Authentication Code (HMAC).
2121
//!
2222
23-
use core::{borrow, fmt, ops, str};
23+
use core::{borrow, convert, fmt, str};
2424
#[cfg(feature = "serde")]
2525
use serde::{Serialize, Serializer, Deserialize, Deserializer};
2626

@@ -81,10 +81,10 @@ impl<T: Hash> HmacEngine<T> {
8181

8282
if key.len() > T::Engine::BLOCK_SIZE {
8383
let hash = <T as Hash>::hash(key);
84-
for (b_i, b_h) in ipad.iter_mut().zip(&hash[..]) {
84+
for (b_i, b_h) in ipad.iter_mut().zip(hash.as_ref()) {
8585
*b_i ^= *b_h;
8686
}
87-
for (b_o, b_h) in opad.iter_mut().zip(&hash[..]) {
87+
for (b_o, b_h) in opad.iter_mut().zip(hash.as_ref()) {
8888
*b_o ^= *b_h;
8989
}
9090
} else {
@@ -149,44 +149,15 @@ impl<T: Hash> fmt::LowerHex for Hmac<T> {
149149
}
150150
}
151151

152-
impl<T: Hash> ops::Index<usize> for Hmac<T> {
153-
type Output = u8;
154-
fn index(&self, index: usize) -> &u8 {
155-
&self.0[index]
156-
}
157-
}
158-
159-
impl<T: Hash> ops::Index<ops::Range<usize>> for Hmac<T> {
160-
type Output = [u8];
161-
fn index(&self, index: ops::Range<usize>) -> &[u8] {
162-
&self.0[index]
163-
}
164-
}
165-
166-
impl<T: Hash> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
167-
type Output = [u8];
168-
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
169-
&self.0[index]
170-
}
171-
}
172-
173-
impl<T: Hash> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
174-
type Output = [u8];
175-
fn index(&self, index: ops::RangeTo<usize>) -> &[u8] {
176-
&self.0[index]
177-
}
178-
}
179-
180-
impl<T: Hash> ops::Index<ops::RangeFull> for Hmac<T> {
181-
type Output = [u8];
182-
fn index(&self, index: ops::RangeFull) -> &[u8] {
183-
&self.0[index]
152+
impl<T: Hash> borrow::Borrow<[u8]> for Hmac<T> {
153+
fn borrow(&self) -> &[u8] {
154+
self.0.borrow()
184155
}
185156
}
186157

187-
impl<T: Hash> borrow::Borrow<[u8]> for Hmac<T> {
188-
fn borrow(&self) -> &[u8] {
189-
&self[..]
158+
impl<T: Hash> convert::AsRef<[u8]> for Hmac<T> {
159+
fn as_ref(&self) -> &[u8] {
160+
self.0.as_ref()
190161
}
191162
}
192163

@@ -196,7 +167,7 @@ impl<T: Hash> Hash for Hmac<T> {
196167

197168
fn from_engine(mut e: HmacEngine<T>) -> Hmac<T> {
198169
let ihash = T::from_engine(e.iengine);
199-
e.oengine.input(&ihash[..]);
170+
e.oengine.input(ihash.as_ref());
200171
let ohash = T::from_engine(e.oengine);
201172
Hmac(ohash)
202173
}
@@ -364,7 +335,7 @@ mod tests {
364335
let mut engine = HmacEngine::<sha256::Hash>::new(&test.key);
365336
engine.input(&test.input);
366337
let hash = Hmac::<sha256::Hash>::from_engine(engine);
367-
assert_eq!(&hash[..], &test.output[..]);
338+
assert_eq!(hash.as_ref(), &test.output[..]);
368339
assert_eq!(hash.into_inner()[..].as_ref(), test.output.as_slice());
369340
}
370341
}

Diff for: src/lib.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub mod siphash24;
7474
pub mod sha512;
7575
pub mod cmp;
7676

77-
use core::{borrow, fmt, hash, ops};
77+
use core::{borrow, convert, fmt, hash};
7878

7979
pub use hmac::{Hmac, HmacEngine};
8080
pub use error::Error;
@@ -99,14 +99,9 @@ pub trait HashEngine: Clone + Default {
9999
}
100100

101101
/// Trait which applies to hashes of all types.
102-
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
103-
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
104-
ops::Index<ops::RangeFull, Output = [u8]> +
105-
ops::Index<ops::RangeFrom<usize>, Output = [u8]> +
106-
ops::Index<ops::RangeTo<usize>, Output = [u8]> +
107-
ops::Index<ops::Range<usize>, Output = [u8]> +
108-
ops::Index<usize, Output = u8> +
109-
borrow::Borrow<[u8]>
102+
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord
103+
+ hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex
104+
+ borrow::Borrow<[u8]> + convert::AsRef<[u8]>
110105
{
111106
/// A hashing engine which bytes can be serialized into. It is expected
112107
/// to implement the `io::Write` trait, and to never return errors under
@@ -170,7 +165,7 @@ mod tests {
170165
fn convert_newtypes() {
171166
let h1 = TestNewtype::hash(&[]);
172167
let h2: TestNewtype2 = h1.as_hash().into();
173-
assert_eq!(&h1[..], &h2[..]);
168+
assert_eq!(h1.as_ref(), h2.as_ref());
174169

175170
let h = sha256d::Hash::hash(&[]);
176171
let h2: TestNewtype = h.to_string().parse().unwrap();

Diff for: src/ripemd160.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
//!
2222
2323
use core::{cmp, str};
24-
use core::ops::Index;
25-
use core::slice::SliceIndex;
2624

2725
use crate::{Error, HashEngine as _, hex, util};
2826

@@ -89,15 +87,6 @@ hex_fmt_impl!(LowerHex, Hash);
8987
serde_impl!(Hash, 20);
9088
borrow_slice_impl!(Hash);
9189

92-
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
93-
type Output = I::Output;
94-
95-
#[inline]
96-
fn index(&self, index: I) -> &Self::Output {
97-
&self.0[index]
98-
}
99-
}
100-
10190
impl str::FromStr for Hash {
10291
type Err = hex::Error;
10392
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -527,7 +516,7 @@ mod tests {
527516
// Hash through high-level API, check hex encoding/decoding
528517
let hash = ripemd160::Hash::hash(&test.input.as_bytes());
529518
assert_eq!(hash, ripemd160::Hash::from_hex(test.output_str).expect("parse hex"));
530-
assert_eq!(&hash[..], &test.output[..]);
519+
assert_eq!(hash.as_ref(), &test.output[..]);
531520
assert_eq!(&hash.to_hex(), &test.output_str);
532521

533522
// Hash through engine, checking that we can input byte by byte

Diff for: src/serde_macros.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod serde_details {
2222
use crate::Error;
2323

2424
use core::marker::PhantomData;
25-
use core::{fmt, ops, str};
25+
use core::{convert, fmt, str};
2626
use core::str::FromStr;
2727
struct HexVisitor<ValueT>(PhantomData<ValueT>);
2828
use serde::{de, Serializer, Deserializer};
@@ -90,8 +90,7 @@ pub mod serde_details {
9090
Self: Sized
9191
+ FromStr
9292
+ fmt::Display
93-
+ ops::Index<usize, Output = u8>
94-
+ ops::Index<ops::RangeFull, Output = [u8]>,
93+
+ convert::AsRef<[u8]>,
9594
<Self as FromStr>::Err: fmt::Display,
9695
{
9796
/// Size, in bits, of the hash.
@@ -105,7 +104,7 @@ pub mod serde_details {
105104
if s.is_human_readable() {
106105
s.collect_str(self)
107106
} else {
108-
s.serialize_bytes(&self[..])
107+
s.serialize_bytes(self.as_ref())
109108
}
110109
}
111110

Diff for: src/sha1.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
//!
1717
1818
use core::{cmp, str};
19-
use core::ops::Index;
20-
use core::slice::SliceIndex;
2119

2220
use crate::{Error, HashEngine as _, hex, util};
2321

@@ -84,15 +82,6 @@ hex_fmt_impl!(LowerHex, Hash);
8482
serde_impl!(Hash, 20);
8583
borrow_slice_impl!(Hash);
8684

87-
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
88-
type Output = I::Output;
89-
90-
#[inline]
91-
fn index(&self, index: I) -> &Self::Output {
92-
&self.0[index]
93-
}
94-
}
95-
9685
impl str::FromStr for Hash {
9786
type Err = hex::Error;
9887
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -253,7 +242,7 @@ mod tests {
253242
// Hash through high-level API, check hex encoding/decoding
254243
let hash = sha1::Hash::hash(&test.input.as_bytes());
255244
assert_eq!(hash, sha1::Hash::from_hex(test.output_str).expect("parse hex"));
256-
assert_eq!(&hash[..], &test.output[..]);
245+
assert_eq!(hash.as_ref(), &test.output[..]);
257246
assert_eq!(&hash.to_hex(), &test.output_str);
258247

259248
// Hash through engine, checking that we can input byte by byte

0 commit comments

Comments
 (0)