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

Commit b13b06a

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 688c862 commit b13b06a

17 files changed

+47
-188
lines changed

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")]

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")]

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")]

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")]

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
}

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

@@ -39,15 +37,6 @@ hex_fmt_impl!(Hash);
3937
serde_impl!(Hash, 20);
4038
borrow_slice_impl!(Hash);
4139

42-
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
43-
type Output = I::Output;
44-
45-
#[inline]
46-
fn index(&self, index: I) -> &Self::Output {
47-
&self.0[index]
48-
}
49-
}
50-
5140
impl str::FromStr for Hash {
5241
type Err = hex::Error;
5342
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -65,10 +54,10 @@ impl crate::Hash for Hash {
6554

6655
fn from_engine(e: sha256::HashEngine) -> Hash {
6756
let sha2 = sha256::Hash::from_engine(e);
68-
let rmd = ripemd160::Hash::hash(&sha2[..]);
57+
let rmd = ripemd160::Hash::hash(sha2.as_ref());
6958

7059
let mut ret = [0; 20];
71-
ret.copy_from_slice(&rmd[..]);
60+
ret.copy_from_slice(rmd.as_ref());
7261
Hash(ret)
7362
}
7463

@@ -141,9 +130,9 @@ mod tests {
141130

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

149138
// Hash through engine, checking that we can input byte by byte
@@ -153,7 +142,7 @@ mod tests {
153142
}
154143
let manual_hash = Hash::from_engine(engine);
155144
assert_eq!(hash, manual_hash);
156-
assert_eq!(hash.into_inner()[..].as_ref(), test.output.as_slice());
145+
assert_eq!(hash.into_inner().as_ref(), test.output.as_slice());
157146
}
158147
}
159148

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
}

src/lib.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub mod siphash24;
131131
pub mod sha512;
132132
pub mod cmp;
133133

134-
use core::{borrow, fmt, hash, ops};
134+
use core::{borrow, convert, fmt, hash};
135135

136136
pub use hmac::{Hmac, HmacEngine};
137137
pub use error::Error;
@@ -156,14 +156,9 @@ pub trait HashEngine: Clone + Default {
156156
}
157157

158158
/// Trait which applies to hashes of all types.
159-
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord +
160-
hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex +
161-
ops::Index<ops::RangeFull, Output = [u8]> +
162-
ops::Index<ops::RangeFrom<usize>, Output = [u8]> +
163-
ops::Index<ops::RangeTo<usize>, Output = [u8]> +
164-
ops::Index<ops::Range<usize>, Output = [u8]> +
165-
ops::Index<usize, Output = u8> +
166-
borrow::Borrow<[u8]>
159+
pub trait Hash: Copy + Clone + PartialEq + Eq + PartialOrd + Ord
160+
+ hash::Hash + fmt::Debug + fmt::Display + fmt::LowerHex
161+
+ borrow::Borrow<[u8]> + convert::AsRef<[u8]>
167162
{
168163
/// A hashing engine which bytes can be serialized into. It is expected
169164
/// to implement the `io::Write` trait, and to never return errors under
@@ -227,7 +222,7 @@ mod tests {
227222
fn convert_newtypes() {
228223
let h1 = TestNewtype::hash(&[]);
229224
let h2: TestNewtype2 = h1.as_hash().into();
230-
assert_eq!(&h1[..], &h2[..]);
225+
assert_eq!(h1.as_ref(), h2.as_ref());
231226

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

src/ripemd160.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
2323
use core::{cmp, str};
2424
use core::convert::TryInto;
25-
use core::ops::Index;
26-
use core::slice::SliceIndex;
2725

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

@@ -88,15 +86,6 @@ hex_fmt_impl!(Hash);
8886
serde_impl!(Hash, 20);
8987
borrow_slice_impl!(Hash);
9088

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

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

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

src/sha1.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
1818
use core::{cmp, str};
1919
use core::convert::TryInto;
20-
use core::ops::Index;
21-
use core::slice::SliceIndex;
2220

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

@@ -83,15 +81,6 @@ hex_fmt_impl!(Hash);
8381
serde_impl!(Hash, 20);
8482
borrow_slice_impl!(Hash);
8583

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

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

0 commit comments

Comments
 (0)