1
1
use crate :: hasher:: { Digest , Size } ;
2
2
use crate :: Error ;
3
- #[ cfg( not ( feature = "std" ) ) ]
3
+ #[ cfg( feature = "alloc" ) ]
4
4
use alloc:: vec:: Vec ;
5
5
use core:: convert:: TryFrom ;
6
6
@@ -160,9 +160,11 @@ impl<S: Size> Multihash<S> {
160
160
write_multihash ( w, self . code ( ) , self . size ( ) , self . digest ( ) )
161
161
}
162
162
163
+
164
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
163
165
/// Returns the bytes of a multihash.
164
166
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 ( ) ) ;
166
168
self . write ( & mut bytes)
167
169
. expect ( "writing to a vec should never fail" ) ;
168
170
bytes
@@ -178,6 +180,7 @@ impl<S: Size> core::hash::Hash for Multihash<S> {
178
180
}
179
181
}
180
182
183
+ #[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
181
184
impl < S : Size > From < Multihash < S > > for Vec < u8 > {
182
185
fn from ( multihash : Multihash < S > ) -> Self {
183
186
multihash. to_bytes ( )
@@ -284,14 +287,8 @@ where
284
287
R : io:: Read ,
285
288
S : Size ,
286
289
{
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) ?;
295
292
296
293
if size > S :: to_u64 ( ) || size > u8:: MAX as u64 {
297
294
return Err ( Error :: InvalidSize ( size) ) ;
0 commit comments