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

Commit b270023

Browse files
committed
Update to use edition 2018
Add `edition = "2018"` to the minifest file. In order to get the codebase to build cleanly do: - Remove usage of `use Hash as HashTrait`, instead use `impl crate::Hash for Hash` and `use Hash as _`. - Same for HashEngine (remove EngineTrait). - Add `crate::` to import statements and group same level (only did this for crate imports, the rest can wait for rustfmt :) - Make test imports uniform, elect to _not_ use `super::*` because it seems cleaner, we are always importing the module we are testing and the same set of traits in each `test` module. Can change if requested.
1 parent e762962 commit b270023

File tree

16 files changed

+126
-166
lines changed

16 files changed

+126
-166
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repository = "https://github.com/rust-bitcoin/bitcoin_hashes/"
99
documentation = "https://docs.rs/bitcoin_hashes/"
1010
keywords = [ "crypto", "bitcoin", "hash", "digest" ]
1111
readme = "README.md"
12+
edition = "2018"
1213

1314
[lib]
1415
name = "bitcoin_hashes"

src/cmp.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,8 @@ fn eq_test() {
8383
mod benches {
8484
use test::Bencher;
8585

86-
use sha256;
87-
use sha512;
88-
use Hash;
89-
use cmp::fixed_time_eq;
86+
use crate::{Hash, sha256, sha512};
87+
use crate::cmp::fixed_time_eq;
9088

9189
#[bench]
9290
fn bench_32b_constant_time_cmp_ne(bh: &mut Bencher) {

src/hash160.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ use core::str;
2424
use core::ops::Index;
2525
use core::slice::SliceIndex;
2626

27-
use sha256;
28-
use ripemd160;
29-
use Hash as HashTrait;
30-
use Error;
27+
use crate::{Error, hex, ripemd160, sha256};
3128

3229
/// Output of the Bitcoin HASH160 hash function.
3330
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
@@ -54,13 +51,13 @@ impl<I: SliceIndex<[u8]>> Index<I> for Hash {
5451
}
5552

5653
impl str::FromStr for Hash {
57-
type Err = ::hex::Error;
54+
type Err = hex::Error;
5855
fn from_str(s: &str) -> Result<Self, Self::Err> {
59-
::hex::FromHex::from_hex(s)
56+
hex::FromHex::from_hex(s)
6057
}
6158
}
6259

63-
impl HashTrait for Hash {
60+
impl crate::Hash for Hash {
6461
type Engine = sha256::HashEngine;
6562
type Inner = [u8; 20];
6663

@@ -107,8 +104,8 @@ mod tests {
107104
#[test]
108105
#[cfg(any(feature = "std", feature = "alloc"))]
109106
fn test() {
110-
use {hash160, Hash, HashEngine};
111-
use hex::{FromHex, ToHex};
107+
use crate::{hash160, Hash, HashEngine};
108+
use crate::hex::{FromHex, ToHex};
112109

113110
#[derive(Clone)]
114111
#[cfg(any(feature = "std", feature = "alloc"))]
@@ -162,7 +159,7 @@ mod tests {
162159
#[test]
163160
fn ripemd_serde() {
164161
use serde_test::{Configure, Token, assert_tokens};
165-
use {hash160, Hash};
162+
use crate::{hash160, Hash};
166163

167164
static HASH_BYTES: [u8; 20] = [
168165
0x13, 0x20, 0x72, 0xdf,
@@ -182,9 +179,7 @@ mod tests {
182179
mod benches {
183180
use test::Bencher;
184181

185-
use hash160;
186-
use Hash;
187-
use HashEngine;
182+
use crate::{Hash, HashEngine, hash160};
188183

189184
#[bench]
190185
pub fn hash160_10(bh: &mut Bencher) {

src/hex.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
//!
1717
1818
#[cfg(any(feature = "std", feature = "alloc"))]
19-
use alloc::{string::String, vec::Vec};
19+
use crate::alloc::{string::String, vec::Vec};
2020
#[cfg(feature = "alloc")]
21-
use alloc::format;
21+
use crate::alloc::format;
2222

2323
#[cfg(feature = "std")]
2424
use std::io;
2525
#[cfg(all(not(feature = "std"), feature = "core2"))]
2626
use core2::io;
2727

2828
use core::{fmt, str};
29-
use Hash;
29+
use crate::Hash;
3030

3131
/// Hex decoding error.
3232
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

src/hmac.rs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,44 @@ use core::{borrow, fmt, ops, str};
2424
#[cfg(feature = "serde")]
2525
use serde::{Serialize, Serializer, Deserialize, Deserializer};
2626

27-
use HashEngine as EngineTrait;
28-
use Hash as HashTrait;
29-
use Error;
27+
use crate::{Error, Hash, HashEngine};
3028

3129
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
3230
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
3331
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3432
#[cfg_attr(feature = "schemars", schemars(transparent))]
3533
#[repr(transparent)]
36-
pub struct Hmac<T: HashTrait>(T);
34+
pub struct Hmac<T: Hash>(T);
3735

38-
impl<T: HashTrait + str::FromStr> str::FromStr for Hmac<T> {
36+
impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {
3937
type Err = <T as str::FromStr>::Err;
4038
fn from_str(s: &str) -> Result<Self, Self::Err> {
4139
Ok(Hmac(str::FromStr::from_str(s)?))
4240
}
4341
}
4442

4543
/// Pair of underlying hash midstates which represent the current state of an `HmacEngine`.
46-
pub struct HmacMidState<T: HashTrait> {
44+
pub struct HmacMidState<T: Hash> {
4745
/// Midstate of the inner hash engine
48-
pub inner: <T::Engine as EngineTrait>::MidState,
46+
pub inner: <T::Engine as HashEngine>::MidState,
4947
/// Midstate of the outer hash engine
50-
pub outer: <T::Engine as EngineTrait>::MidState,
48+
pub outer: <T::Engine as HashEngine>::MidState,
5149
}
5250

5351
/// Pair of underyling hash engines, used for the inner and outer hash of HMAC.
5452
#[derive(Clone)]
55-
pub struct HmacEngine<T: HashTrait> {
53+
pub struct HmacEngine<T: Hash> {
5654
iengine: T::Engine,
5755
oengine: T::Engine,
5856
}
5957

60-
impl<T: HashTrait> Default for HmacEngine<T> {
58+
impl<T: Hash> Default for HmacEngine<T> {
6159
fn default() -> Self {
6260
HmacEngine::new(&[])
6361
}
6462
}
6563

66-
impl<T: HashTrait> HmacEngine<T> {
64+
impl<T: Hash> HmacEngine<T> {
6765
/// Constructs a new keyed HMAC from `key`.
6866
///
6967
/// We only support underlying hashes whose block sizes are ≤ 128 bytes.
@@ -77,12 +75,12 @@ impl<T: HashTrait> HmacEngine<T> {
7775
let mut ipad = [0x36u8; 128];
7876
let mut opad = [0x5cu8; 128];
7977
let mut ret = HmacEngine {
80-
iengine: <T as HashTrait>::engine(),
81-
oengine: <T as HashTrait>::engine(),
78+
iengine: <T as Hash>::engine(),
79+
oengine: <T as Hash>::engine(),
8280
};
8381

8482
if key.len() > T::Engine::BLOCK_SIZE {
85-
let hash = <T as HashTrait>::hash(key);
83+
let hash = <T as Hash>::hash(key);
8684
for (b_i, b_h) in ipad.iter_mut().zip(&hash[..]) {
8785
*b_i ^= *b_h;
8886
}
@@ -98,8 +96,8 @@ impl<T: HashTrait> HmacEngine<T> {
9896
}
9997
};
10098

101-
EngineTrait::input(&mut ret.iengine, &ipad[..T::Engine::BLOCK_SIZE]);
102-
EngineTrait::input(&mut ret.oengine, &opad[..T::Engine::BLOCK_SIZE]);
99+
HashEngine::input(&mut ret.iengine, &ipad[..T::Engine::BLOCK_SIZE]);
100+
HashEngine::input(&mut ret.oengine, &opad[..T::Engine::BLOCK_SIZE]);
103101
ret
104102
}
105103

@@ -112,7 +110,7 @@ impl<T: HashTrait> HmacEngine<T> {
112110
}
113111
}
114112

115-
impl<T: HashTrait> EngineTrait for HmacEngine<T> {
113+
impl<T: Hash> HashEngine for HmacEngine<T> {
116114
type MidState = HmacMidState<T>;
117115

118116
fn midstate(&self) -> Self::MidState {
@@ -133,66 +131,66 @@ impl<T: HashTrait> EngineTrait for HmacEngine<T> {
133131
}
134132
}
135133

136-
impl<T: HashTrait> fmt::Debug for Hmac<T> {
134+
impl<T: Hash> fmt::Debug for Hmac<T> {
137135
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138136
fmt::Debug::fmt(&self.0, f)
139137
}
140138
}
141139

142-
impl<T: HashTrait> fmt::Display for Hmac<T> {
140+
impl<T: Hash> fmt::Display for Hmac<T> {
143141
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144142
fmt::Display::fmt(&self.0, f)
145143
}
146144
}
147145

148-
impl<T: HashTrait> fmt::LowerHex for Hmac<T> {
146+
impl<T: Hash> fmt::LowerHex for Hmac<T> {
149147
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150148
fmt::LowerHex::fmt(&self.0, f)
151149
}
152150
}
153151

154-
impl<T: HashTrait> ops::Index<usize> for Hmac<T> {
152+
impl<T: Hash> ops::Index<usize> for Hmac<T> {
155153
type Output = u8;
156154
fn index(&self, index: usize) -> &u8 {
157155
&self.0[index]
158156
}
159157
}
160158

161-
impl<T: HashTrait> ops::Index<ops::Range<usize>> for Hmac<T> {
159+
impl<T: Hash> ops::Index<ops::Range<usize>> for Hmac<T> {
162160
type Output = [u8];
163161
fn index(&self, index: ops::Range<usize>) -> &[u8] {
164162
&self.0[index]
165163
}
166164
}
167165

168-
impl<T: HashTrait> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
166+
impl<T: Hash> ops::Index<ops::RangeFrom<usize>> for Hmac<T> {
169167
type Output = [u8];
170168
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
171169
&self.0[index]
172170
}
173171
}
174172

175-
impl<T: HashTrait> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
173+
impl<T: Hash> ops::Index<ops::RangeTo<usize>> for Hmac<T> {
176174
type Output = [u8];
177175
fn index(&self, index: ops::RangeTo<usize>) -> &[u8] {
178176
&self.0[index]
179177
}
180178
}
181179

182-
impl<T: HashTrait> ops::Index<ops::RangeFull> for Hmac<T> {
180+
impl<T: Hash> ops::Index<ops::RangeFull> for Hmac<T> {
183181
type Output = [u8];
184182
fn index(&self, index: ops::RangeFull) -> &[u8] {
185183
&self.0[index]
186184
}
187185
}
188186

189-
impl<T: HashTrait> borrow::Borrow<[u8]> for Hmac<T> {
187+
impl<T: Hash> borrow::Borrow<[u8]> for Hmac<T> {
190188
fn borrow(&self) -> &[u8] {
191189
&self[..]
192190
}
193191
}
194192

195-
impl<T: HashTrait> HashTrait for Hmac<T> {
193+
impl<T: Hash> Hash for Hmac<T> {
196194
type Engine = HmacEngine<T>;
197195
type Inner = T::Inner;
198196

@@ -224,15 +222,15 @@ impl<T: HashTrait> HashTrait for Hmac<T> {
224222

225223
#[cfg(feature = "serde")]
226224
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
227-
impl<T: HashTrait + Serialize> Serialize for Hmac<T> {
225+
impl<T: Hash + Serialize> Serialize for Hmac<T> {
228226
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
229227
Serialize::serialize(&self.0, s)
230228
}
231229
}
232230

233231
#[cfg(feature = "serde")]
234232
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
235-
impl<'de, T: HashTrait + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
233+
impl<'de, T: Hash + Deserialize<'de>> Deserialize<'de> for Hmac<T> {
236234
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Hmac<T>, D::Error> {
237235
let inner = Deserialize::deserialize(d)?;
238236
Ok(Hmac(inner))
@@ -244,7 +242,7 @@ mod tests {
244242
#[test]
245243
#[cfg(any(feature = "std", feature = "alloc"))]
246244
fn test() {
247-
use {sha256, HashEngine, HmacEngine, Hash, Hmac};
245+
use crate::{sha256, HashEngine, HmacEngine, Hash, Hmac};
248246

249247
#[derive(Clone)]
250248
struct Test {
@@ -370,7 +368,7 @@ mod tests {
370368
#[test]
371369
fn hmac_sha512_serde() {
372370
use serde_test::{Configure, Token, assert_tokens};
373-
use {sha512, Hash, Hmac};
371+
use crate::{sha512, Hash, Hmac};
374372

375373
static HASH_BYTES: [u8; 64] = [
376374
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21,
@@ -399,8 +397,7 @@ mod tests {
399397
mod benches {
400398
use test::Bencher;
401399

402-
use sha256;
403-
use {Hmac, Hash, HashEngine};
400+
use crate::{Hmac, Hash, HashEngine, sha256};
404401

405402
#[bench]
406403
pub fn hmac_sha256_10(bh: &mut Bencher) {

src/impls.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ use std::{error, io};
2323
#[cfg(not(feature = "std"))]
2424
use core2::{error, io};
2525

26-
use {hex, sha1, sha256, sha512, ripemd160, siphash24, hmac};
27-
use HashEngine;
28-
use Error;
26+
use crate::{Error, HashEngine, hex, sha1, sha256, sha512, ripemd160, siphash24, hmac};
2927

3028
impl error::Error for Error {
3129
#[cfg(feature = "std")]
@@ -86,7 +84,7 @@ impl io::Write for siphash24::HashEngine {
8684
}
8785
}
8886

89-
impl<T: ::Hash> io::Write for hmac::HmacEngine<T> {
87+
impl<T: crate::Hash> io::Write for hmac::HmacEngine<T> {
9088
fn flush(&mut self) -> io::Result<()> { Ok(()) }
9189

9290
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
@@ -99,8 +97,7 @@ impl<T: ::Hash> io::Write for hmac::HmacEngine<T> {
9997
mod tests {
10098
use super::io::Write;
10199

102-
use {sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};
103-
use Hash;
100+
use crate::{Hash, sha1, sha256, sha256d, sha512, ripemd160, hash160, siphash24, hmac};
104101

105102
macro_rules! write_test {
106103
($mod:ident, $exp_empty:expr, $exp_256:expr, $exp_64k:expr,) => {

src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,18 @@ pub trait Hash: Copy + Clone + PartialEq + Eq + Default + PartialOrd + Ord +
153153

154154
#[cfg(test)]
155155
mod tests {
156-
use Hash;
157-
hash_newtype!(TestNewtype, ::sha256d::Hash, 32, doc="A test newtype");
158-
hash_newtype!(TestNewtype2, ::sha256d::Hash, 32, doc="A test newtype");
156+
use crate::{Hash, sha256d};
157+
158+
hash_newtype!(TestNewtype, sha256d::Hash, 32, doc="A test newtype");
159+
hash_newtype!(TestNewtype2, sha256d::Hash, 32, doc="A test newtype");
159160

160161
#[test]
161162
fn convert_newtypes() {
162163
let h1 = TestNewtype::hash(&[]);
163164
let h2: TestNewtype2 = h1.as_hash().into();
164165
assert_eq!(&h1[..], &h2[..]);
165166

166-
let h = ::sha256d::Hash::hash(&[]);
167+
let h = sha256d::Hash::hash(&[]);
167168
let h2: TestNewtype = h.to_string().parse().unwrap();
168169
assert_eq!(h2.as_hash(), h);
169170
}

0 commit comments

Comments
 (0)