pub struct Cid {
bytes: Vec<u8>,
codec: u64,
}Expand description
A content identifier, held as the bytes it hashes and prints from.
The binary form is the canonical one and the string is derived from it, not the other way around: a CID that came in as a string and goes out as one goes out identical, because there is only one string for these bytes.
Fields§
§bytes: Vec<u8>Version, codec and multihash, concatenated — the multibase payload.
codec: u64The codec, parsed out so a caller can ask without re-reading varints.
Implementations§
Source§impl Cid
impl Cid
Sourcepub fn of_dag_cbor(block: &[u8]) -> Self
pub fn of_dag_cbor(block: &[u8]) -> Self
The CID of a DAG-CBOR block: sha-256 over exactly these bytes.
This is the whole reason the encoder has to be canonical. The identity of a record is the hash of its encoding, so two encoders that disagree about map order or integer width give the same record two identities, and every other implementation would be right to say ours is the wrong one.
Sourcepub fn of_raw(block: &[u8]) -> Self
pub fn of_raw(block: &[u8]) -> Self
The CID of a raw block, which is what a blob is referenced by.
Sourcepub fn parse(text: &str) -> Result<Self, CidError>
pub fn parse(text: &str) -> Result<Self, CidError>
Parses the string form: b followed by lowercase base32.
use didbot_data::Cid;
let cid = Cid::parse("bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a").unwrap();
assert_eq!(cid.to_string(), "bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a");
assert!(Cid::parse("QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR").is_err());Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, CidError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, CidError>
Parses the binary form, which is what a DAG-CBOR link tag carries.
Sourcepub fn read(input: &mut &[u8]) -> Result<Self, CidError>
pub fn read(input: &mut &[u8]) -> Result<Self, CidError>
Reads a CID off the front of a byte stream, leaving the rest.
A CID is self-delimiting — the multihash declares its own digest
length — which is what lets a CAR file put one immediately before a
block with no length prefix between them. Cid::from_bytes is the
whole-slice form and refuses a trailing byte; this one is the streaming
form and hands the trailing bytes back.
use didbot_data::Cid;
let cid = Cid::of_raw(b"a block");
let mut framed = cid.as_bytes().to_vec();
framed.extend_from_slice(b"a block");
let mut rest = framed.as_slice();
assert_eq!(Cid::read(&mut rest).unwrap(), cid);
assert_eq!(rest, b"a block");Sourcepub fn of_block_under(codec: u64, block: &[u8]) -> Self
pub fn of_block_under(codec: u64, block: &[u8]) -> Self
The CID these bytes have under codec, which is how a reader checks
that a block is the one it was handed under a name.
The codec comes from the name being checked rather than from the caller’s expectation: a block is either the bytes its CID says it is or it is not, and which codec that CID declares is part of the claim.
Trait Implementations§
Source§impl Ord for Cid
impl Ord for Cid
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Cid
impl PartialOrd for Cid
impl Eq for Cid
impl StructuralPartialEq for Cid
Auto Trait Implementations§
impl Freeze for Cid
impl RefUnwindSafe for Cid
impl Send for Cid
impl Sync for Cid
impl Unpin for Cid
impl UnsafeUnpin for Cid
impl UnwindSafe for Cid
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.