11use crate :: hasher:: { Digest , Size } ;
22use crate :: Error ;
3- #[ cfg( not ( feature = "std" ) ) ]
3+ #[ cfg( feature = "alloc" ) ]
44use alloc:: vec:: Vec ;
55use core:: convert:: TryFrom ;
66
@@ -160,9 +160,11 @@ impl<S: Size> Multihash<S> {
160160 write_multihash ( w, self . code ( ) , self . size ( ) , self . digest ( ) )
161161 }
162162
163+
164+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
163165 /// Returns the bytes of a multihash.
164166 pub fn to_bytes ( & self ) -> Vec < u8 > {
165- let mut bytes: Vec < u8 > = Vec :: with_capacity ( self . size ( ) . into ( ) ) ;
167+ let mut bytes = Vec :: with_capacity ( self . size ( ) . into ( ) ) ;
166168 self . write ( & mut bytes)
167169 . expect ( "writing to a vec should never fail" ) ;
168170 bytes
@@ -178,6 +180,7 @@ impl<S: Size> core::hash::Hash for Multihash<S> {
178180 }
179181}
180182
183+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
181184impl < S : Size > From < Multihash < S > > for Vec < u8 > {
182185 fn from ( multihash : Multihash < S > ) -> Self {
183186 multihash. to_bytes ( )
@@ -284,14 +287,8 @@ where
284287 R : io:: Read ,
285288 S : Size ,
286289{
287- let code = match read_u64 ( & mut r) {
288- Ok ( c) => c,
289- Err ( e) => return Err ( e. into ( ) ) ,
290- } ;
291- let size = match read_u64 ( & mut r) {
292- Ok ( s) => s,
293- Err ( e) => return Err ( e. into ( ) ) ,
294- } ;
290+ let code = read_u64 ( & mut r) ?;
291+ let size = read_u64 ( & mut r) ?;
295292
296293 if size > S :: to_u64 ( ) || size > u8:: MAX as u64 {
297294 return Err ( Error :: InvalidSize ( size) ) ;
0 commit comments