Skip to content

Commit 44d341e

Browse files
committed
feat: add Deref an Into<Vec<u8>> implementations
1 parent 8de5e2c commit 44d341e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/digests.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Borrow;
22
use std::convert::TryFrom;
3-
use std::{cmp, fmt, hash};
3+
use std::{cmp, fmt, hash, ops};
44

55
use unsigned_varint::{decode as varint_decode, encode as varint_encode};
66

@@ -87,6 +87,14 @@ impl AsRef<[u8]> for Multihash {
8787
}
8888
}
8989

90+
impl ops::Deref for Multihash {
91+
type Target = [u8];
92+
93+
fn deref(&self) -> &Self::Target {
94+
&self.as_bytes()
95+
}
96+
}
97+
9098
impl Borrow<[u8]> for Multihash {
9199
fn borrow(&self) -> &[u8] {
92100
self.as_bytes()
@@ -107,6 +115,12 @@ impl TryFrom<Vec<u8>> for Multihash {
107115
}
108116
}
109117

118+
impl Into<Vec<u8>> for Multihash {
119+
fn into(self) -> Vec<u8> {
120+
self.to_vec()
121+
}
122+
}
123+
110124
impl PartialOrd for Multihash {
111125
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
112126
Some(self.cmp(other))
@@ -180,6 +194,20 @@ impl<'a> PartialEq<Multihash> for MultihashRef<'a> {
180194
}
181195
}
182196

197+
impl<'a> ops::Deref for MultihashRef<'a> {
198+
type Target = [u8];
199+
200+
fn deref(&self) -> &Self::Target {
201+
self.as_bytes()
202+
}
203+
}
204+
205+
impl<'a> Into<Vec<u8>> for MultihashRef<'a> {
206+
fn into(self) -> Vec<u8> {
207+
self.to_vec()
208+
}
209+
}
210+
183211
/// The `MultihashDigest` trait specifies an interface common for all multihash functions.
184212
pub trait MultihashDigest {
185213
/// The Mutlihash byte value.

0 commit comments

Comments
 (0)