88
99mod errors;
1010mod hashes;
11+ mod storage;
1112
1213use std:: convert:: TryFrom ;
14+ use std:: fmt:: Debug ;
15+ use std:: hash;
1316
1417use blake2b_simd:: { blake2b, Params as Blake2bVariable } ;
1518use blake2s_simd:: { blake2s, Params as Blake2sVariable } ;
16- use bytes:: { BufMut , Bytes , BytesMut } ;
1719use sha2:: Digest ;
1820use tiny_keccak:: Keccak ;
1921use unsigned_varint:: { decode, encode} ;
2022
2123pub use errors:: { DecodeError , DecodeOwnedError , EncodeError } ;
2224pub use hashes:: Hash ;
25+ use std:: fmt;
26+ use storage:: Storage ;
2327
2428// Helper macro for encoding input into output using sha1, sha2, tiny_keccak, or blake2
2529macro_rules! encode {
@@ -104,15 +108,8 @@ pub fn encode(hash: Hash, input: &[u8]) -> Result<Multihash, EncodeError> {
104108 let code = encode:: u16 ( hash. code ( ) , & mut buf) ;
105109 let mut len_buf = encode:: u32_buffer ( ) ;
106110 let size = encode:: u32 ( input. len ( ) as u32 , & mut len_buf) ;
107-
108- let total_len = code. len ( ) + size. len ( ) + input. len ( ) ;
109-
110- let mut output = BytesMut :: with_capacity ( total_len) ;
111- output. put_slice ( code) ;
112- output. put_slice ( size) ;
113- output. put_slice ( input) ;
114111 Ok ( Multihash {
115- bytes : output . freeze ( ) ,
112+ storage : Storage :: from_slices ( & [ & code , & size , & input ] ) ,
116113 } )
117114 } else {
118115 let ( offset, mut output) = encode_hash ( hash) ;
@@ -135,31 +132,51 @@ pub fn encode(hash: Hash, input: &[u8]) -> Result<Multihash, EncodeError> {
135132 } ) ;
136133
137134 Ok ( Multihash {
138- bytes : output . freeze ( ) ,
135+ storage : Storage :: from_slice ( & output ) ,
139136 } )
140137 }
141138}
142139
143- // Encode the given [`Hash`] value and ensure the returned [`BytesMut `]
140+ // Encode the given [`Hash`] value and ensure the returned [`Vec<u8> `]
144141// has enough capacity to hold the actual digest.
145- fn encode_hash ( hash : Hash ) -> ( usize , BytesMut ) {
142+ fn encode_hash ( hash : Hash ) -> ( usize , Vec < u8 > ) {
146143 let mut buf = encode:: u16_buffer ( ) ;
147144 let code = encode:: u16 ( hash. code ( ) , & mut buf) ;
148145
149146 let len = code. len ( ) + 1 + usize:: from ( hash. size ( ) ) ;
150147
151- let mut output = BytesMut :: with_capacity ( len) ;
152- output. put_slice ( code) ;
153- output. put_u8 ( hash. size ( ) ) ;
148+ let mut output = Vec :: with_capacity ( len) ;
149+ output. extend_from_slice ( code) ;
150+ output. push ( hash. size ( ) ) ;
154151 output. resize ( len, 0 ) ;
155152
156153 ( code. len ( ) + 1 , output)
157154}
158155
159156/// Represents a valid multihash.
160- #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
157+ #[ derive( Clone ) ]
161158pub struct Multihash {
162- bytes : Bytes ,
159+ storage : Storage ,
160+ }
161+
162+ impl Debug for Multihash {
163+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
164+ f. debug_tuple ( "Multihash" ) . field ( & self . as_bytes ( ) ) . finish ( )
165+ }
166+ }
167+
168+ impl PartialEq for Multihash {
169+ fn eq ( & self , other : & Self ) -> bool {
170+ self . storage . bytes ( ) == other. storage . bytes ( )
171+ }
172+ }
173+
174+ impl Eq for Multihash { }
175+
176+ impl hash:: Hash for Multihash {
177+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
178+ self . storage . bytes ( ) . hash ( state) ;
179+ }
163180}
164181
165182impl Multihash {
@@ -172,7 +189,7 @@ impl Multihash {
172189 } ) ;
173190 }
174191 Ok ( Multihash {
175- bytes : Bytes :: from ( bytes) ,
192+ storage : Storage :: from_slice ( & bytes) ,
176193 } )
177194 }
178195
@@ -183,17 +200,19 @@ impl Multihash {
183200
184201 /// Returns the bytes representation of the multihash.
185202 pub fn to_vec ( & self ) -> Vec < u8 > {
186- Vec :: from ( & self . bytes [ .. ] )
203+ Vec :: from ( self . as_bytes ( ) )
187204 }
188205
189206 /// Returns the bytes representation of this multihash.
190207 pub fn as_bytes ( & self ) -> & [ u8 ] {
191- & self . bytes
208+ self . storage . bytes ( )
192209 }
193210
194211 /// Builds a `MultihashRef` corresponding to this `Multihash`.
195212 pub fn as_ref ( & self ) -> MultihashRef {
196- MultihashRef { bytes : & self . bytes }
213+ MultihashRef {
214+ bytes : self . as_bytes ( ) ,
215+ }
197216 }
198217
199218 /// Returns which hashing algorithm is used in this multihash.
@@ -215,7 +234,7 @@ impl AsRef<[u8]> for Multihash {
215234
216235impl < ' a > PartialEq < MultihashRef < ' a > > for Multihash {
217236 fn eq ( & self , other : & MultihashRef < ' a > ) -> bool {
218- & * self . bytes == other. bytes
237+ & * self . as_bytes ( ) == other. as_bytes ( )
219238 }
220239}
221240
@@ -290,7 +309,7 @@ impl<'a> MultihashRef<'a> {
290309 /// This operation allocates.
291310 pub fn to_owned ( & self ) -> Multihash {
292311 Multihash {
293- bytes : Bytes :: copy_from_slice ( self . bytes ) ,
312+ storage : Storage :: from_slice ( self . bytes ) ,
294313 }
295314 }
296315
@@ -302,7 +321,7 @@ impl<'a> MultihashRef<'a> {
302321
303322impl < ' a > PartialEq < Multihash > for MultihashRef < ' a > {
304323 fn eq ( & self , other : & Multihash ) -> bool {
305- self . bytes == & * other. bytes
324+ self . as_bytes ( ) == & * other. as_bytes ( )
306325 }
307326}
308327
0 commit comments