5
5
//!
6
6
//! As a library author that works with CIDs that should support hashes of anysize, you would
7
7
//! import the `Cid` type from this module.
8
- #[ cfg( feature = "std" ) ]
9
- use std:: convert:: TryFrom ;
8
+ use core:: convert:: TryFrom ;
10
9
11
- #[ cfg( feature = "std " ) ]
10
+ #[ cfg( feature = "alloc " ) ]
12
11
use multibase:: { encode as base_encode, Base } ;
12
+
13
13
use multihash:: { MultihashGeneric as Multihash , Size } ;
14
+ use unsigned_varint:: encode as varint_encode;
15
+
16
+ #[ cfg( feature = "alloc" ) ]
17
+ extern crate alloc;
18
+
19
+ #[ cfg( feature = "alloc" ) ]
20
+ use alloc:: {
21
+ borrow,
22
+ string:: { String , ToString } ,
23
+ vec:: Vec ,
24
+ } ;
25
+
26
+ #[ cfg( feature = "std" ) ]
27
+ pub ( crate ) use unsigned_varint:: io:: read_u64 as varint_read_u64;
28
+
29
+ /// Reads 64 bits from a byte array into a u64
30
+ /// Adapted from unsigned-varint's generated read_u64 function at
31
+ /// https://github.com/paritytech/unsigned-varint/blob/master/src/io.rs
32
+ #[ cfg( not( feature = "std" ) ) ]
33
+ pub ( crate ) fn varint_read_u64 < R : io:: Read > ( mut r : R ) -> Result < u64 > {
34
+ use unsigned_varint:: decode;
35
+ let mut b = varint_encode:: u64_buffer ( ) ;
36
+ for i in 0 ..b. len ( ) {
37
+ let n = r. read ( & mut ( b[ i..i + 1 ] ) ) ?;
38
+ if n == 0 {
39
+ return Err ( Error :: VarIntDecodeError ) ;
40
+ } else if decode:: is_last ( b[ i] ) {
41
+ return Ok ( decode:: u64 ( & b[ ..=i] ) . unwrap ( ) . 0 ) ;
42
+ }
43
+ }
44
+ Err ( Error :: VarIntDecodeError )
45
+ }
46
+
14
47
#[ cfg( feature = "std" ) ]
15
- use unsigned_varint:: { encode as varint_encode, io:: read_u64 as varint_read_u64} ;
48
+ use std:: io;
49
+
50
+ #[ cfg( not( feature = "std" ) ) ]
51
+ use core2:: io;
16
52
17
53
use crate :: error:: { Error , Result } ;
18
54
use crate :: version:: Version ;
@@ -93,8 +129,7 @@ impl<S: Size> Cid<S> {
93
129
}
94
130
95
131
/// Reads the bytes from a byte stream.
96
- #[ cfg( feature = "std" ) ]
97
- pub fn read_bytes < R : std:: io:: Read > ( mut r : R ) -> Result < Self > {
132
+ pub fn read_bytes < R : io:: Read > ( mut r : R ) -> Result < Self > {
98
133
let version = varint_read_u64 ( & mut r) ?;
99
134
let codec = varint_read_u64 ( & mut r) ?;
100
135
// CIDv0 has the fixed `0x12 0x20` prefix
@@ -110,8 +145,7 @@ impl<S: Size> Cid<S> {
110
145
}
111
146
}
112
147
113
- #[ cfg( feature = "std" ) ]
114
- fn write_bytes_v1 < W : std:: io:: Write > ( & self , mut w : W ) -> Result < ( ) > {
148
+ fn write_bytes_v1 < W : io:: Write > ( & self , mut w : W ) -> Result < ( ) > {
115
149
let mut version_buf = varint_encode:: u64_buffer ( ) ;
116
150
let version = varint_encode:: u64 ( self . version . into ( ) , & mut version_buf) ;
117
151
@@ -125,8 +159,7 @@ impl<S: Size> Cid<S> {
125
159
}
126
160
127
161
/// Writes the bytes to a byte stream.
128
- #[ cfg( feature = "std" ) ]
129
- pub fn write_bytes < W : std:: io:: Write > ( & self , w : W ) -> Result < ( ) > {
162
+ pub fn write_bytes < W : io:: Write > ( & self , w : W ) -> Result < ( ) > {
130
163
match self . version {
131
164
Version :: V0 => self . hash . write ( w) ?,
132
165
Version :: V1 => self . write_bytes_v1 ( w) ?,
@@ -135,19 +168,19 @@ impl<S: Size> Cid<S> {
135
168
}
136
169
137
170
/// Returns the encoded bytes of the `Cid`.
138
- #[ cfg( feature = "std " ) ]
171
+ #[ cfg( feature = "alloc " ) ]
139
172
pub fn to_bytes ( & self ) -> Vec < u8 > {
140
- let mut bytes = vec ! [ ] ;
173
+ let mut bytes = Vec :: new ( ) ;
141
174
self . write_bytes ( & mut bytes) . unwrap ( ) ;
142
175
bytes
143
176
}
144
177
145
- #[ cfg( feature = "std " ) ]
178
+ #[ cfg( feature = "alloc " ) ]
146
179
fn to_string_v0 ( & self ) -> String {
147
180
Base :: Base58Btc . encode ( self . hash . to_bytes ( ) )
148
181
}
149
182
150
- #[ cfg( feature = "std " ) ]
183
+ #[ cfg( feature = "alloc " ) ]
151
184
fn to_string_v1 ( & self ) -> String {
152
185
multibase:: encode ( Base :: Base32Lower , self . to_bytes ( ) . as_slice ( ) )
153
186
}
@@ -167,7 +200,7 @@ impl<S: Size> Cid<S> {
167
200
/// let encoded = cid.to_string_of_base(Base::Base64).unwrap();
168
201
/// assert_eq!(encoded, "mAVUSICwmtGto/8aP+ZtFPB0wQTQTQi1wZIO/oPmKXohiZueu");
169
202
/// ```
170
- #[ cfg( feature = "std " ) ]
203
+ #[ cfg( feature = "alloc " ) ]
171
204
pub fn to_string_of_base ( & self , base : Base ) -> Result < String > {
172
205
match self . version {
173
206
Version :: V0 => {
@@ -192,9 +225,9 @@ impl<S: Size> Default for Cid<S> {
192
225
}
193
226
}
194
227
195
- #[ cfg( feature = "std " ) ]
196
- impl < S : Size > std :: fmt:: Display for Cid < S > {
197
- fn fmt ( & self , f : & mut std :: fmt:: Formatter ) -> std :: fmt:: Result {
228
+ #[ cfg( feature = "alloc " ) ]
229
+ impl < S : Size > core :: fmt:: Display for Cid < S > {
230
+ fn fmt ( & self , f : & mut core :: fmt:: Formatter ) -> core :: fmt:: Result {
198
231
let output = match self . version {
199
232
Version :: V0 => self . to_string_v0 ( ) ,
200
233
Version :: V1 => self . to_string_v1 ( ) ,
@@ -222,16 +255,16 @@ impl<S: Size> std::fmt::Debug for Cid<S> {
222
255
}
223
256
}
224
257
225
- #[ cfg( feature = "std " ) ]
226
- impl < S : Size > std :: str:: FromStr for Cid < S > {
258
+ #[ cfg( feature = "alloc " ) ]
259
+ impl < S : Size > core :: str:: FromStr for Cid < S > {
227
260
type Err = Error ;
228
261
229
262
fn from_str ( cid_str : & str ) -> Result < Self > {
230
263
Self :: try_from ( cid_str)
231
264
}
232
265
}
233
266
234
- #[ cfg( feature = "std " ) ]
267
+ #[ cfg( feature = "alloc " ) ]
235
268
impl < S : Size > TryFrom < String > for Cid < S > {
236
269
type Error = Error ;
237
270
@@ -240,7 +273,7 @@ impl<S: Size> TryFrom<String> for Cid<S> {
240
273
}
241
274
}
242
275
243
- #[ cfg( feature = "std " ) ]
276
+ #[ cfg( feature = "alloc " ) ]
244
277
impl < S : Size > TryFrom < & str > for Cid < S > {
245
278
type Error = Error ;
246
279
@@ -267,7 +300,7 @@ impl<S: Size> TryFrom<&str> for Cid<S> {
267
300
}
268
301
}
269
302
270
- #[ cfg( feature = "std " ) ]
303
+ #[ cfg( feature = "alloc " ) ]
271
304
impl < S : Size > TryFrom < Vec < u8 > > for Cid < S > {
272
305
type Error = Error ;
273
306
@@ -276,7 +309,6 @@ impl<S: Size> TryFrom<Vec<u8>> for Cid<S> {
276
309
}
277
310
}
278
311
279
- #[ cfg( feature = "std" ) ]
280
312
impl < S : Size > TryFrom < & [ u8 ] > for Cid < S > {
281
313
type Error = Error ;
282
314
@@ -294,31 +326,31 @@ where
294
326
}
295
327
}
296
328
297
- #[ cfg( feature = "std " ) ]
329
+ #[ cfg( feature = "alloc " ) ]
298
330
impl < S : Size > From < Cid < S > > for Vec < u8 > {
299
331
fn from ( cid : Cid < S > ) -> Self {
300
332
cid. to_bytes ( )
301
333
}
302
334
}
303
335
304
- #[ cfg( feature = "std " ) ]
336
+ #[ cfg( feature = "alloc " ) ]
305
337
impl < S : Size > From < Cid < S > > for String {
306
338
fn from ( cid : Cid < S > ) -> Self {
307
339
cid. to_string ( )
308
340
}
309
341
}
310
342
311
- #[ cfg( feature = "std " ) ]
312
- impl < ' a , S : Size > From < Cid < S > > for std :: borrow:: Cow < ' a , Cid < S > > {
343
+ #[ cfg( feature = "alloc " ) ]
344
+ impl < ' a , S : Size > From < Cid < S > > for borrow:: Cow < ' a , Cid < S > > {
313
345
fn from ( from : Cid < S > ) -> Self {
314
- std :: borrow:: Cow :: Owned ( from)
346
+ borrow:: Cow :: Owned ( from)
315
347
}
316
348
}
317
349
318
350
#[ cfg( feature = "std" ) ]
319
- impl < ' a , S : Size > From < & ' a Cid < S > > for std :: borrow:: Cow < ' a , Cid < S > > {
351
+ impl < ' a , S : Size > From < & ' a Cid < S > > for borrow:: Cow < ' a , Cid < S > > {
320
352
fn from ( from : & ' a Cid < S > ) -> Self {
321
- std :: borrow:: Cow :: Borrowed ( from)
353
+ borrow:: Cow :: Borrowed ( from)
322
354
}
323
355
}
324
356
0 commit comments