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

Commit a3b2881

Browse files
committed
Merge #142: Use SliceIndex trait to implement Index
a94b232 Use SliceIndex trait to implement Index (Tobin Harding) Pull request description: Since we bumped to Rust 1.29 it has not been necessary to implement `Index` manually, we can get it by way of the `SliceIndex` trait. Implement `Index` using `SliceIndex`. Keep the declarative macro so we only have to write the `SliceIndex` implementation once. This is in the same vein as rust-bitcoin/rust-bitcoin#805 but goes about it in a slightly different manner, we should probably be uniform across both crates. I'm unsure which is the best option? ACKs for top commit: apoelstra: ACK a94b232 Tree-SHA512: 17777440f7c1acfb44b31498e289ad26ba5f394daa9b9a48142a08d5b6eb6be38fccbad2e3793204e8b7faf4b027ecc1a46103f56d1bf453f4d5089e21fd5bd7
2 parents c85dd7d + a94b232 commit a3b2881

File tree

9 files changed

+106
-54
lines changed

9 files changed

+106
-54
lines changed

src/hash160.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
//! # HASH160 (SHA256 then RIPEMD160)
2121
2222
use core::str;
23+
use core::ops::Index;
24+
use core::slice::SliceIndex;
2325

2426
use sha256;
2527
use ripemd160;
@@ -40,10 +42,18 @@ pub struct Hash(
4042
hex_fmt_impl!(Debug, Hash);
4143
hex_fmt_impl!(Display, Hash);
4244
hex_fmt_impl!(LowerHex, Hash);
43-
index_impl!(Hash);
4445
serde_impl!(Hash, 20);
4546
borrow_slice_impl!(Hash);
4647

48+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
49+
type Output = I::Output;
50+
51+
#[inline]
52+
fn index(&self, index: I) -> &Self::Output {
53+
&self.0[index]
54+
}
55+
}
56+
4757
impl str::FromStr for Hash {
4858
type Err = ::hex::Error;
4959
fn from_str(s: &str) -> Result<Self, Self::Err> {

src/ripemd160.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
//! # RIPEMD160
2121
2222
use core::{cmp, str};
23+
use core::ops::Index;
24+
use core::slice::SliceIndex;
2325

2426
use HashEngine as EngineTrait;
2527
use Hash as HashTrait;
@@ -86,10 +88,18 @@ pub struct Hash(
8688
hex_fmt_impl!(Debug, Hash);
8789
hex_fmt_impl!(Display, Hash);
8890
hex_fmt_impl!(LowerHex, Hash);
89-
index_impl!(Hash);
9091
serde_impl!(Hash, 20);
9192
borrow_slice_impl!(Hash);
9293

94+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
95+
type Output = I::Output;
96+
97+
#[inline]
98+
fn index(&self, index: I) -> &Self::Output {
99+
&self.0[index]
100+
}
101+
}
102+
93103
impl str::FromStr for Hash {
94104
type Err = ::hex::Error;
95105
fn from_str(s: &str) -> Result<Self, Self::Err> {

src/sha1.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//! # SHA1
1616
1717
use core::{cmp, str};
18+
use core::ops::Index;
19+
use core::slice::SliceIndex;
1820

1921
use HashEngine as EngineTrait;
2022
use Hash as HashTrait;
@@ -81,10 +83,18 @@ pub struct Hash(
8183
hex_fmt_impl!(Debug, Hash);
8284
hex_fmt_impl!(Display, Hash);
8385
hex_fmt_impl!(LowerHex, Hash);
84-
index_impl!(Hash);
8586
serde_impl!(Hash, 20);
8687
borrow_slice_impl!(Hash);
8788

89+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
90+
type Output = I::Output;
91+
92+
#[inline]
93+
fn index(&self, index: I) -> &Self::Output {
94+
&self.0[index]
95+
}
96+
}
97+
8898
impl str::FromStr for Hash {
8999
type Err = ::hex::Error;
90100
fn from_str(s: &str) -> Result<Self, Self::Err> {

src/sha256.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//! # SHA256
1616
1717
use core::{cmp, str};
18+
use core::ops::Index;
19+
use core::slice::SliceIndex;
1820

1921
use hex;
2022
use HashEngine as EngineTrait;
@@ -89,10 +91,18 @@ impl str::FromStr for Hash {
8991
hex_fmt_impl!(Debug, Hash);
9092
hex_fmt_impl!(Display, Hash);
9193
hex_fmt_impl!(LowerHex, Hash);
92-
index_impl!(Hash);
9394
serde_impl!(Hash, 32);
9495
borrow_slice_impl!(Hash);
9596

97+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
98+
type Output = I::Output;
99+
100+
#[inline]
101+
fn index(&self, index: I) -> &Self::Output {
102+
&self.0[index]
103+
}
104+
}
105+
96106
impl HashTrait for Hash {
97107
type Engine = HashEngine;
98108
type Inner = [u8; 32];
@@ -160,10 +170,18 @@ pub struct Midstate(pub [u8; 32]);
160170
hex_fmt_impl!(Debug, Midstate);
161171
hex_fmt_impl!(Display, Midstate);
162172
hex_fmt_impl!(LowerHex, Midstate);
163-
index_impl!(Midstate);
164173
serde_impl!(Midstate, 32);
165174
borrow_slice_impl!(Midstate);
166175

176+
impl<I: SliceIndex<[u8]>> Index<I> for Midstate {
177+
type Output = I::Output;
178+
179+
#[inline]
180+
fn index(&self, index: I) -> &Self::Output {
181+
&self.0[index]
182+
}
183+
}
184+
167185
impl str::FromStr for Midstate {
168186
type Err = ::hex::Error;
169187
fn from_str(s: &str) -> Result<Self, Self::Err> {

src/sha256d.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//! # SHA256d
1616
1717
use core::str;
18+
use core::ops::Index;
19+
use core::slice::SliceIndex;
1820

1921
use sha256;
2022
use Hash as HashTrait;
@@ -32,10 +34,18 @@ pub struct Hash(
3234
hex_fmt_impl!(Debug, Hash);
3335
hex_fmt_impl!(Display, Hash);
3436
hex_fmt_impl!(LowerHex, Hash);
35-
index_impl!(Hash);
3637
serde_impl!(Hash, 32);
3738
borrow_slice_impl!(Hash);
3839

40+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
41+
type Output = I::Output;
42+
43+
#[inline]
44+
fn index(&self, index: I) -> &Self::Output {
45+
&self.0[index]
46+
}
47+
}
48+
3949
impl str::FromStr for Hash {
4050
type Err = ::hex::Error;
4151
fn from_str(s: &str) -> Result<Self, Self::Err> {

src/sha256t.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use core::{cmp, str};
1818
#[cfg(feature="serde")] use core::fmt;
1919
use core::marker::PhantomData;
20+
use core::ops::Index;
21+
use core::slice::SliceIndex;
2022

2123
use sha256;
2224
use Hash as HashTrait;
@@ -83,9 +85,17 @@ impl<T: Tag> str::FromStr for Hash<T> {
8385
hex_fmt_impl!(Debug, Hash, T:Tag);
8486
hex_fmt_impl!(Display, Hash, T:Tag);
8587
hex_fmt_impl!(LowerHex, Hash, T:Tag);
86-
index_impl!(Hash, T:Tag);
8788
borrow_slice_impl!(Hash, T:Tag);
8889

90+
impl<I: SliceIndex<[u8]>, T: Tag> Index<I> for Hash<T> {
91+
type Output = I::Output;
92+
93+
#[inline]
94+
fn index(&self, index: I) -> &Self::Output {
95+
&self.0[index]
96+
}
97+
}
98+
8999
impl<T: Tag> HashTrait for Hash<T> {
90100
type Engine = sha256::HashEngine;
91101
type Inner = [u8; 32];

src/sha512.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
//! # SHA512
2121
2222
use core::{cmp, hash, str};
23+
use core::ops::Index;
24+
use core::slice::SliceIndex;
2325

2426
use HashEngine as EngineTrait;
2527
use Hash as HashTrait;
@@ -137,10 +139,18 @@ impl str::FromStr for Hash {
137139
hex_fmt_impl!(Debug, Hash);
138140
hex_fmt_impl!(Display, Hash);
139141
hex_fmt_impl!(LowerHex, Hash);
140-
index_impl!(Hash);
141142
serde_impl!(Hash, 64);
142143
borrow_slice_impl!(Hash);
143144

145+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
146+
type Output = I::Output;
147+
148+
#[inline]
149+
fn index(&self, index: I) -> &Self::Output {
150+
&self.0[index]
151+
}
152+
}
153+
144154
impl HashTrait for Hash {
145155
type Engine = HashEngine;
146156
type Inner = [u8; 64];

src/siphash24.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
//! # SipHash 2-4
2121
2222
use core::{cmp, mem, ptr, str};
23+
use core::ops::Index;
24+
use core::slice::SliceIndex;
2325

2426
use Error;
2527
use Hash as HashTrait;
@@ -206,10 +208,18 @@ pub struct Hash(
206208
hex_fmt_impl!(Debug, Hash);
207209
hex_fmt_impl!(Display, Hash);
208210
hex_fmt_impl!(LowerHex, Hash);
209-
index_impl!(Hash);
210211
serde_impl!(Hash, 8);
211212
borrow_slice_impl!(Hash);
212213

214+
impl<I: SliceIndex<[u8]>> Index<I> for Hash {
215+
type Output = I::Output;
216+
217+
#[inline]
218+
fn index(&self, index: I) -> &Self::Output {
219+
&self.0[index]
220+
}
221+
}
222+
213223
impl str::FromStr for Hash {
214224
type Err = ::hex::Error;
215225
fn from_str(s: &str) -> Result<Self, Self::Err> {

src/util.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,50 +42,6 @@ macro_rules! hex_fmt_impl(
4242
)
4343
);
4444

45-
/// Adds `core::ops::Index` trait implementation to a given type `$ty`
46-
#[macro_export]
47-
macro_rules! index_impl(
48-
($ty:ident) => (
49-
index_impl!($ty, );
50-
);
51-
($ty:ident, $($gen:ident: $gent:ident),*) => (
52-
impl<$($gen: $gent),*> $crate::_export::_core::ops::Index<usize> for $ty<$($gen),*> {
53-
type Output = u8;
54-
fn index(&self, index: usize) -> &u8 {
55-
&self.0[index]
56-
}
57-
}
58-
59-
impl<$($gen: $gent),*> $crate::_export::_core::ops::Index<$crate::_export::_core::ops::Range<usize>> for $ty<$($gen),*> {
60-
type Output = [u8];
61-
fn index(&self, index: $crate::_export::_core::ops::Range<usize>) -> &[u8] {
62-
&self.0[index]
63-
}
64-
}
65-
66-
impl<$($gen: $gent),*> $crate::_export::_core::ops::Index<$crate::_export::_core::ops::RangeFrom<usize>> for $ty<$($gen),*> {
67-
type Output = [u8];
68-
fn index(&self, index: $crate::_export::_core::ops::RangeFrom<usize>) -> &[u8] {
69-
&self.0[index]
70-
}
71-
}
72-
73-
impl<$($gen: $gent),*> $crate::_export::_core::ops::Index<$crate::_export::_core::ops::RangeTo<usize>> for $ty<$($gen),*> {
74-
type Output = [u8];
75-
fn index(&self, index: $crate::_export::_core::ops::RangeTo<usize>) -> &[u8] {
76-
&self.0[index]
77-
}
78-
}
79-
80-
impl<$($gen: $gent),*> $crate::_export::_core::ops::Index<$crate::_export::_core::ops::RangeFull> for $ty<$($gen),*> {
81-
type Output = [u8];
82-
fn index(&self, index: $crate::_export::_core::ops::RangeFull) -> &[u8] {
83-
&self.0[index]
84-
}
85-
}
86-
)
87-
);
88-
8945
/// Adds slicing traits implementations to a given type `$ty`
9046
#[macro_export]
9147
macro_rules! borrow_slice_impl(
@@ -224,7 +180,6 @@ macro_rules! hash_newtype {
224180
hex_fmt_impl!(Debug, $newtype);
225181
hex_fmt_impl!(Display, $newtype);
226182
hex_fmt_impl!(LowerHex, $newtype);
227-
index_impl!($newtype);
228183
serde_impl!($newtype, $len);
229184
borrow_slice_impl!($newtype);
230185

@@ -296,6 +251,15 @@ macro_rules! hash_newtype {
296251
$crate::hex::FromHex::from_hex(s)
297252
}
298253
}
254+
255+
impl<I: $crate::_export::_core::slice::SliceIndex<[u8]>> $crate::_export::_core::ops::Index<I> for $newtype {
256+
type Output = I::Output;
257+
258+
#[inline]
259+
fn index(&self, index: I) -> &Self::Output {
260+
&self.0[index]
261+
}
262+
}
299263
};
300264
}
301265

0 commit comments

Comments
 (0)