Skip to content

Commit 2b8dfe3

Browse files
committed
feat: derive Copy trait for Code
Also make `wrap()` take a code instance and not a reference, that makes the API nicer.
1 parent 19eea13 commit 2b8dfe3

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/digests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub trait MultihashDigest {
190190
///
191191
/// The size of the hash is determoned by the size of the input hash. If it should be truncated
192192
/// the input data must already be the truncated hash.
193-
pub fn wrap(code: &Code, data: &[u8]) -> Multihash {
193+
pub fn wrap(code: Code, data: &[u8]) -> Multihash {
194194
let mut code_buf = varint_encode::u64_buffer();
195195
let code = varint_encode::u64(code.to_u64(), &mut code_buf);
196196

src/hashes.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tiny_keccak::{Hasher, Keccak, Sha3};
77

88
use crate::digests::{wrap, Multihash, MultihashDigest};
99

10-
#[derive(Clone, Debug, PartialEq)]
10+
#[derive(Clone, Copy, Debug, PartialEq)]
1111
pub enum Code {
1212
/// Identity (Raw binary )
1313
Identity,
@@ -136,7 +136,7 @@ impl Identity {
136136
if (data.len() as u64) >= u64::from(std::u32::MAX) {
137137
panic!("Input data for identity hash is too large, it needs to be less the 2^32.")
138138
}
139-
wrap(&Self::CODE, &data)
139+
wrap(Self::CODE, &data)
140140
}
141141
}
142142

@@ -154,7 +154,7 @@ impl Sha1 {
154154
pub const CODE: Code = Code::Sha1;
155155
pub fn digest(data: &[u8]) -> Multihash {
156156
let digest = Sha1Hasher::from(&data).digest().bytes();
157-
wrap(&Self::CODE, &digest)
157+
wrap(Self::CODE, &digest)
158158
}
159159
}
160160

@@ -172,7 +172,7 @@ impl Sha2_256 {
172172
pub const CODE: Code = Code::Sha2_256;
173173
pub fn digest(data: &[u8]) -> Multihash {
174174
let digest = Sha256::digest(&data);
175-
wrap(&Self::CODE, &digest)
175+
wrap(Self::CODE, &digest)
176176
}
177177
}
178178

@@ -190,7 +190,7 @@ impl Sha2_512 {
190190
pub const CODE: Code = Code::Sha2_512;
191191
pub fn digest(data: &[u8]) -> Multihash {
192192
let digest = Sha512::digest(&data);
193-
wrap(&Self::CODE, &digest)
193+
wrap(Self::CODE, &digest)
194194
}
195195
}
196196

@@ -211,7 +211,7 @@ impl Sha3_224 {
211211
let mut sha3 = Sha3::v224();
212212
sha3.update(&data);
213213
sha3.finalize(&mut digest);
214-
wrap(&Self::CODE, &digest)
214+
wrap(Self::CODE, &digest)
215215
}
216216
}
217217

@@ -232,7 +232,7 @@ impl Sha3_256 {
232232
let mut sha3 = Sha3::v256();
233233
sha3.update(&data);
234234
sha3.finalize(&mut digest);
235-
wrap(&Self::CODE, &digest)
235+
wrap(Self::CODE, &digest)
236236
}
237237
}
238238

@@ -253,7 +253,7 @@ impl Sha3_384 {
253253
let mut sha3 = Sha3::v384();
254254
sha3.update(&data);
255255
sha3.finalize(&mut digest);
256-
wrap(&Self::CODE, &digest)
256+
wrap(Self::CODE, &digest)
257257
}
258258
}
259259

@@ -274,7 +274,7 @@ impl Sha3_512 {
274274
let mut sha3 = Sha3::v512();
275275
sha3.update(&data);
276276
sha3.finalize(&mut digest);
277-
wrap(&Self::CODE, &digest)
277+
wrap(Self::CODE, &digest)
278278
}
279279
}
280280

@@ -295,7 +295,7 @@ impl Keccak224 {
295295
let mut keccak = Keccak::v224();
296296
keccak.update(&data);
297297
keccak.finalize(&mut digest);
298-
wrap(&Self::CODE, &digest)
298+
wrap(Self::CODE, &digest)
299299
}
300300
}
301301

@@ -316,7 +316,7 @@ impl Keccak256 {
316316
let mut keccak = Keccak::v256();
317317
keccak.update(&data);
318318
keccak.finalize(&mut digest);
319-
wrap(&Self::CODE, &digest)
319+
wrap(Self::CODE, &digest)
320320
}
321321
}
322322

@@ -337,7 +337,7 @@ impl Keccak384 {
337337
let mut keccak = Keccak::v384();
338338
keccak.update(&data);
339339
keccak.finalize(&mut digest);
340-
wrap(&Self::CODE, &digest)
340+
wrap(Self::CODE, &digest)
341341
}
342342
}
343343

@@ -358,7 +358,7 @@ impl Keccak512 {
358358
let mut keccak = Keccak::v512();
359359
keccak.update(&data);
360360
keccak.finalize(&mut digest);
361-
wrap(&Self::CODE, &digest)
361+
wrap(Self::CODE, &digest)
362362
}
363363
}
364364

@@ -380,7 +380,7 @@ impl Blake2b256 {
380380
.to_state()
381381
.update(&data)
382382
.finalize();
383-
wrap(&Self::CODE, &digest.as_bytes())
383+
wrap(Self::CODE, &digest.as_bytes())
384384
}
385385
}
386386

@@ -402,7 +402,7 @@ impl Blake2b512 {
402402
.to_state()
403403
.update(&data)
404404
.finalize();
405-
wrap(&Self::CODE, &digest.as_bytes())
405+
wrap(Self::CODE, &digest.as_bytes())
406406
}
407407
}
408408

@@ -424,7 +424,7 @@ impl Blake2s128 {
424424
.to_state()
425425
.update(&data)
426426
.finalize();
427-
wrap(&Self::CODE, &digest.as_bytes())
427+
wrap(Self::CODE, &digest.as_bytes())
428428
}
429429
}
430430

@@ -446,6 +446,6 @@ impl Blake2s256 {
446446
.to_state()
447447
.update(&data)
448448
.finalize();
449-
wrap(&Self::CODE, &digest.as_bytes())
449+
wrap(Self::CODE, &digest.as_bytes())
450450
}
451451
}

tests/hashes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn custom_multihash_digest() {
3131

3232
fn digest(&self, _data: &[u8]) -> Multihash {
3333
let data = b"alwaysthesame";
34-
wrap(&Self.code(), data)
34+
wrap(Self.code(), data)
3535
}
3636
}
3737

tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ fn test_long_identity_hash() {
212212
fn custom_multihash() {
213213
let code = Code::Custom(0x1234);
214214
let data = b"abcde".to_vec();
215-
let multihash = wrap(&code, &data);
215+
let multihash = wrap(code, &data);
216216

217217
assert_eq!(
218218
multihash.as_bytes(),

0 commit comments

Comments
 (0)