@@ -8,6 +8,8 @@ use core::convert::TryInto;
8
8
use core:: fmt:: Debug ;
9
9
use generic_array:: { ArrayLength , GenericArray } ;
10
10
11
+ use unsigned_varint:: { encode as varint_encode, decode} ;
12
+
11
13
#[ cfg( feature = "std" ) ]
12
14
use std:: io;
13
15
@@ -259,7 +261,6 @@ pub fn write_multihash<W>(mut w: W, code: u64, size: u8, digest: &[u8]) -> Resul
259
261
where
260
262
W : io:: Write ,
261
263
{
262
- use unsigned_varint:: encode as varint_encode;
263
264
264
265
let mut code_buf = varint_encode:: u64_buffer ( ) ;
265
266
let code = varint_encode:: u64 ( code, & mut code_buf) ;
@@ -285,7 +286,7 @@ where
285
286
S : Size ,
286
287
{
287
288
#[ cfg( not( feature = "std" ) ) ]
288
- use crate :: varint_read_u64 as read_u64;
289
+ use crate :: read_u64 as read_u64;
289
290
290
291
#[ cfg( feature = "std" ) ]
291
292
use unsigned_varint:: io:: read_u64;
@@ -308,6 +309,24 @@ where
308
309
Ok ( ( code, size as u8 , digest) )
309
310
}
310
311
312
+ /// Reads 64 bits from a byte array into a u64
313
+ /// Adapted from unsigned-varint's generated read_u64 function at
314
+ /// https://github.com/paritytech/unsigned-varint/blob/master/src/io.rs
315
+ pub fn read_u64 < R : io:: Read > ( mut r : R ) -> Result < u64 , Error > {
316
+ let mut b = varint_encode:: u64_buffer ( ) ;
317
+ for i in 0 ..b. len ( ) {
318
+ let n = r. read ( & mut ( b[ i..i + 1 ] ) ) ?;
319
+ if n == 0 {
320
+ return Err ( Error :: Varint ( decode:: Error :: Insufficient ) ) ;
321
+ }
322
+ else if decode:: is_last ( b[ i] ) {
323
+ return Ok ( decode:: u64 ( & b[ ..=i] ) . unwrap ( ) . 0 ) ;
324
+ }
325
+ }
326
+ Err ( Error :: Varint ( decode:: Error :: Overflow ) )
327
+ }
328
+
329
+
311
330
#[ cfg( test) ]
312
331
mod tests {
313
332
use super :: * ;
0 commit comments