Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ pre-release-commit-message = "Release {{version}} 🎉🎉"
no-dev-version = true

[features]
default = ["std"]
default = ["std", "multihash/default"]
std = ["multibase", "multihash/std", "unsigned-varint/std"]
arb = ["quickcheck", "rand", "multihash/arb"]
scale-codec = ["parity-scale-codec", "multihash/scale-codec"]
serde-codec = ["serde", "multihash/serde-codec"]

[dependencies]
multihash = { version = "0.12", default-features = false }
unsigned-varint = { version = "0.5.1", default-features = false }

multibase = { version = "0.8.0", optional = true }
parity-scale-codec = { version = "1.3.5", optional = true, default-features = false, features = ["derive"] }
quickcheck = { version = "0.9.2", optional = true }
rand = { version = "0.7.3", optional = true }
serde = { version = "1.0.116", optional = true }

[dev-dependencies]
quickcheck = "0.9.2"
rand = "0.7.3"
multihash = { version = "0.12", default-features = false, features = ["arb", "multihash-impl"] }
serde_json = "1.0.59"
57 changes: 57 additions & 0 deletions src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const SHA2_256: u64 = 0x12;
///
/// The generic is about the allocated size of the multihash.
#[derive(PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Decode))]
#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Encode))]
#[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde-codec", derive(serde::Serialize))]
#[cfg_attr(feature = "serde-codec", serde(bound = "S: Size"))]
pub struct Cid<S: Size> {
/// The version of CID.
version: Version,
Expand Down Expand Up @@ -177,6 +182,16 @@ impl<S: Size> Cid<S> {
}
}

impl<S: Size> Default for Cid<S> {
fn default() -> Self {
Self {
version: Version::V1,
codec: 0,
hash: Multihash::<S>::default(),
}
}
}

#[cfg(feature = "std")]
#[allow(clippy::derive_hash_xor_eq)]
impl<S: Size> std::hash::Hash for Cid<S> {
Expand Down Expand Up @@ -281,3 +296,45 @@ impl<S: Size> From<Cid<S>> for String {
cid.to_string()
}
}

#[cfg(feature = "std")]
impl<'a, S: Size> From<Cid<S>> for std::borrow::Cow<'a, Cid<S>> {
fn from(from: Cid<S>) -> Self {
std::borrow::Cow::Owned(from)
}
}

#[cfg(feature = "std")]
impl<'a, S: Size> From<&'a Cid<S>> for std::borrow::Cow<'a, Cid<S>> {
fn from(from: &'a Cid<S>) -> Self {
std::borrow::Cow::Borrowed(from)
}
}

#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "scale-codec")]
fn test_cid_scale_codec() {
use super::Cid;
use multihash::U64;
use parity_scale_codec::{Decode, Encode};

let cid = Cid::<U64>::default();
let bytes = cid.encode();
let cid2 = Cid::decode(&mut &bytes[..]).unwrap();
assert_eq!(cid, cid2);
}

#[test]
#[cfg(feature = "serde-codec")]
fn test_cid_serde() {
use super::Cid;
use multihash::U64;

let cid = Cid::<U64>::default();
let bytes = serde_json::to_string(&cid).unwrap();
let cid2 = serde_json::from_str(&bytes).unwrap();
assert_eq!(cid, cid2);
}
}
4 changes: 4 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use crate::error::{Error, Result};

/// The version of the CID.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Decode))]
#[cfg_attr(feature = "scale-codec", derive(parity_scale_codec::Encode))]
#[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))]
#[cfg_attr(feature = "serde-codec", derive(serde::Serialize))]
pub enum Version {
/// CID version 0.
V0,
Expand Down