From 91fd35e06f8ae24d66f6ba4598830d8dbc259c8a Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Mon, 19 Dec 2022 17:49:53 +0100 Subject: [PATCH 1/4] chore: upgrade to Rust edition 2021 and set MSRV (#130) The minimum supported Rust version is set to 1.60 due to the `parity-scale-codec` dependency. BREAKING CHANGE: Rust edition 2021 is now used --- Cargo.toml | 5 +++-- src/serde.rs | 2 -- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7bce59f..5a56cb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,11 +7,12 @@ authors = ["Friedel Ziegelmayer "] keywords = ["ipld", "ipfs", "cid", "multihash", "multiformats"] license = "MIT" readme = "README.md" -edition = "2018" +edition = "2021" +rust-version = "1.60" [features] default = ["std", "multihash/default"] -std = ["multihash/std", "unsigned-varint/std", "alloc", "multibase/std"] +std = ["multihash/std", "unsigned-varint/std", "alloc", "multibase/std", "serde/std"] alloc = ["multibase", "multihash/alloc", "core2/alloc", "serde/alloc"] arb = ["quickcheck", "rand", "multihash/arb", "multihash/sha2", "arbitrary"] scale-codec = ["parity-scale-codec", "multihash/scale-codec"] diff --git a/src/serde.rs b/src/serde.rs index 2583e8f..0da91a5 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -108,8 +108,6 @@ impl<'de, const SIZE: usize> de::Deserialize<'de> for CidGeneric { #[cfg(test)] mod tests { - use std::convert::TryFrom; - use crate::CidGeneric; #[test] From 715771c48fd47969e733ed1faad8b82d9ddbd7ca Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Mon, 19 Dec 2022 17:51:24 +0100 Subject: [PATCH 2/4] feat: add `encoded_len` and written bytes (#129) BREAKING CHANGE: add new method `Cid::encoded_len` and return `Result` now from `Cid::write_bytese`. --- Cargo.toml | 3 ++- src/cid.rs | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5a56cb5..26fdf05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } @@ -34,3 +34,4 @@ core2 = { version = "0.4", default-features = false } [dev-dependencies] serde_json = "1.0.59" + diff --git a/src/cid.rs b/src/cid.rs index 59d74a7..95614cd 100644 --- a/src/cid.rs +++ b/src/cid.rs @@ -159,33 +159,53 @@ impl Cid { } } - fn write_bytes_v1(&self, mut w: W) -> Result<()> { + fn write_bytes_v1(&self, mut w: W) -> Result { 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(&self, w: W) -> Result<()> { - match self.version { + /// Writes the bytes to a byte stream, returns the number of bytes written. + pub fn write_bytes(&self, w: W) -> Result { + 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 { 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 } From e4a12451ce9b6eeb67dc326b4dd2e24462f91048 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Thu, 22 Dec 2022 11:06:18 +0100 Subject: [PATCH 3/4] chore: add changelog for v0.10.0 release (#132) This commit finally starts to add a changelog. --- CHANGELOG.md | 17 +++++++++++++++++ RELEASE.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 RELEASE.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..eaed9cd --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,17 @@ +# [v0.10.0](https://github.com/multiformats/rust-cid/compare/v0.9.0...v0.10.0) (2022-12-22) + + +### chore + +* upgrade to Rust edition 2021 and set MSRV ([#130](https://github.com/multiformats/rust-cid/issues/130)) ([91fd35e](https://github.com/multiformats/rust-cid/commit/91fd35e06f8ae24d66f6ba4598830d8dbc259c8a)) + + +### Features + +* add `encoded_len` and written bytes ([#129](https://github.com/multiformats/rust-cid/issues/129)) ([715771c](https://github.com/multiformats/rust-cid/commit/715771c48fd47969e733ed1faad8b82d9ddbd7ca)) + + +### BREAKING CHANGES + +* Return `Result` (instead of `Result<()>`) now from `Cid::write_bytes`. +* Rust edition 2021 is now used diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000..0d9a3cd --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,48 @@ +Release process +=============== + +Generating Changelog +-------------------- + +Install dependencies + +```sh +$ npm install -g conventional-changelog-cli +$ cd rust-cid +$ conventional-changelog --preset angular +``` + +Add the output of that to `CHANGELOG.md`, and write a human-centric summary of changes. +Update the linked output to reference the new version, which conventional-changelog doesn't know about: + +```md +# [](https://github.com/multiformats/rust-cid/compare/v0.9.0...v) (2022-12-22) +``` +becomes: +```md +# [v0.10.0](https://github.com/multiformats/rust-cid/compare/v0.9.0...v0.10.0) (2022-12-22) +``` + +## Publishing + +Publishing on crates.io, bumping version & generating tags is done using [`cargo-release`](https://github.com/crate-ci/cargo-release). + +This requires the following permissions + +- on github.com/multiformats/rust-cid + - creating tags + - pushing to `master` +- on crates.io + - publish access to all published crates + +Dry run + +```sh +$ cargo release +``` + +Actual publishing + +```sh +$ cargo release --execute +``` From 8b9b30ee0313ca209bf36aae850af8cc5b7d0a64 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Thu, 22 Dec 2022 11:08:22 +0100 Subject: [PATCH 4/4] chore: Release cid version 0.10.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 26fdf05..82f574a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cid" -version = "0.9.0" +version = "0.10.0" description = "CID in rust" homepage = "https://github.com/multiformats/rust-cid" authors = ["Friedel Ziegelmayer "]