Skip to content

Commit

Permalink
feat: add encoded_len and written bytes (#129)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: add new method `Cid::encoded_len` and return
`Result<usize>` now from `Cid::write_bytese`.
  • Loading branch information
dignifiedquire committed Dec 19, 2022
1 parent 91fd35e commit 715771c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ scale-codec = ["parity-scale-codec", "multihash/scale-codec"]
serde-codec = ["alloc", "serde", "multihash/serde-codec", "serde_bytes"]

[dependencies]
multihash = { version = "0.17.0", default-features = false }
multihash = { version = "0.18.0", default-features = false }
unsigned-varint = { version = "0.7.0", default-features = false }

multibase = { version = "0.9.1", optional = true, default-features = false }
Expand All @@ -34,3 +34,4 @@ core2 = { version = "0.4", default-features = false }

[dev-dependencies]
serde_json = "1.0.59"

36 changes: 28 additions & 8 deletions src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,53 @@ impl<const S: usize> Cid<S> {
}
}

fn write_bytes_v1<W: io::Write>(&self, mut w: W) -> Result<()> {
fn write_bytes_v1<W: io::Write>(&self, mut w: W) -> Result<usize> {
let mut version_buf = varint_encode::u64_buffer();
let version = varint_encode::u64(self.version.into(), &mut version_buf);

let mut codec_buf = varint_encode::u64_buffer();
let codec = varint_encode::u64(self.codec, &mut codec_buf);

let mut written = version.len() + codec.len();

w.write_all(version)?;
w.write_all(codec)?;
self.hash.write(&mut w)?;
Ok(())
written += self.hash.write(&mut w)?;

Ok(written)
}

/// Writes the bytes to a byte stream.
pub fn write_bytes<W: io::Write>(&self, w: W) -> Result<()> {
match self.version {
/// Writes the bytes to a byte stream, returns the number of bytes written.
pub fn write_bytes<W: io::Write>(&self, w: W) -> Result<usize> {
let written = match self.version {
Version::V0 => self.hash.write(w)?,
Version::V1 => self.write_bytes_v1(w)?,
};
Ok(written)
}

/// Returns the length in bytes needed to encode this cid into bytes.
pub fn encoded_len(&self) -> usize {
match self.version {
Version::V0 => self.hash.encoded_len(),
Version::V1 => {
let mut version_buf = varint_encode::u64_buffer();
let version = varint_encode::u64(self.version.into(), &mut version_buf);

let mut codec_buf = varint_encode::u64_buffer();
let codec = varint_encode::u64(self.codec, &mut codec_buf);

version.len() + codec.len() + self.hash.encoded_len()
}
}
Ok(())
}

/// Returns the encoded bytes of the `Cid`.
#[cfg(feature = "alloc")]
pub fn to_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
self.write_bytes(&mut bytes).unwrap();
let written = self.write_bytes(&mut bytes).unwrap();
debug_assert_eq!(written, bytes.len());
bytes
}

Expand Down

0 comments on commit 715771c

Please sign in to comment.