sp_cid/
lib.rs

1//! # cid
2//!
3//! Implementation of [cid](https://github.com/ipld/cid) in Rust.
4
5#![cfg_attr(not(feature = "std"), no_std)]
6
7mod cid;
8pub mod codec;
9mod error;
10mod typed_cid;
11mod version;
12
13#[cfg(any(test, feature = "arb"))]
14mod arb;
15
16pub use self::{
17  cid::Cid as CidGeneric,
18  codec::Codec,
19  error::{
20    Error,
21    Result,
22  },
23  version::Version,
24};
25
26pub use multibase;
27pub use sp_multihash;
28
29extern crate alloc;
30use bytecursor::ByteCursor;
31use unsigned_varint::{
32  decode,
33  encode as varint_encode,
34};
35
36/// Reader function from unsigned_varint
37pub fn varint_read_u64(r: &mut ByteCursor) -> Result<u64> {
38  let mut b = varint_encode::u64_buffer();
39  for i in 0..b.len() {
40    let n = r.read(&mut (b[i..i + 1]));
41    if n == 0 {
42      return Err(Error::VarIntDecodeError);
43    }
44    if decode::is_last(b[i]) {
45      return Ok(decode::u64(&b[..=i]).unwrap().0);
46    }
47  }
48  Err(Error::VarIntDecodeError)
49}
50
51/// A Cid that contains a multihash with an allocated size of 512 bits.
52///
53/// This is the same digest size the default multihash code table has.
54///
55/// If you need a CID that is generic over its digest size, use [`CidGeneric`]
56/// instead.
57pub type Cid = CidGeneric<64>;