From 2c9ec7a5c154cb3071de4b0542c730cd5144af50 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Tue, 11 Jan 2022 12:05:27 +0300 Subject: [PATCH 1/5] TRYBUILD=overwrite --- tests/max_encoded_len_ui/not_mel.stderr | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/max_encoded_len_ui/not_mel.stderr b/tests/max_encoded_len_ui/not_mel.stderr index 8b9b2684..1506f930 100644 --- a/tests/max_encoded_len_ui/not_mel.stderr +++ b/tests/max_encoded_len_ui/not_mel.stderr @@ -1,5 +1,5 @@ error[E0599]: the function or associated item `max_encoded_len` exists for struct `Generic`, but its trait bounds were not satisfied - --> $DIR/not_mel.rs:12:29 + --> tests/max_encoded_len_ui/not_mel.rs:12:29 | 4 | struct NotMel; | -------------- doesn't satisfy `NotMel: MaxEncodedLen` @@ -16,6 +16,14 @@ error[E0599]: the function or associated item `max_encoded_len` exists for struc = note: the following trait bounds were not satisfied: `NotMel: MaxEncodedLen` which is required by `Generic: MaxEncodedLen` +note: the following trait must be implemented + --> src/max_encoded_len.rs + | + | / pub trait MaxEncodedLen: Encode { + | | /// Upper bound, in bytes, of the maximum encoded size of this item. + | | fn max_encoded_len() -> usize; + | | } + | |_^ = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it: candidate #1: `MaxEncodedLen` From f4917d0cfcc7d08500126d535a1b90402e64ef96 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Tue, 11 Jan 2022 11:33:48 +0300 Subject: [PATCH 2/5] Add bytes implementation --- .gitlab-ci.yml | 16 ++++++++-------- Cargo.lock | 7 +++++++ Cargo.toml | 2 ++ src/codec.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 86540b76..b21a2742 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -51,14 +51,14 @@ check-rust-stable-no_derive_no_std_full: stage: check <<: *docker-env script: - - time cargo +stable check --verbose --no-default-features --features bit-vec,generic-array,full + - time cargo +stable check --verbose --no-default-features --features bit-vec,bytes,generic-array,full - sccache -s check-rust-stable-no_derive_no_std: stage: check <<: *docker-env script: - - time cargo +stable check --verbose --no-default-features --features bit-vec,generic-array + - time cargo +stable check --verbose --no-default-features --features bit-vec,bytes,generic-array - sccache -s check-rust-stable-no_std-chain-error: @@ -72,7 +72,7 @@ check-rust-stable-no_derive_full: stage: check <<: *docker-env script: - - time cargo +stable check --verbose --features bit-vec,generic-array,full + - time cargo +stable check --verbose --features bit-vec,bytes,generic-array,full - sccache -s #### stage: test @@ -81,21 +81,21 @@ test-rust-stable: stage: test <<: *docker-env script: - - time cargo +stable test --verbose --all --features bit-vec,generic-array,derive,max-encoded-len + - time cargo +stable test --verbose --all --features bit-vec,bytes,generic-array,derive,max-encoded-len - sccache -s test-rust-stable-no_derive: stage: test <<: *docker-env script: - - time cargo +stable test --verbose --features bit-vec,generic-array + - time cargo +stable test --verbose --features bit-vec,bytes,generic-array - sccache -s bench-rust-nightly: stage: test <<: *docker-env script: - - time cargo +nightly bench --features bit-vec,generic-array,derive + - time cargo +nightly bench --features bit-vec,bytes,generic-array,derive - sccache -s miri: @@ -105,7 +105,7 @@ miri: RUST_BACKTRACE: 1 MIRIFLAGS: "-Zmiri-disable-isolation" script: - - time cargo +nightly miri test --features bit-vec,generic-array,arbitrary --release + - time cargo +nightly miri test --features bit-vec,bytes,generic-array,arbitrary --release #### stage: build @@ -118,5 +118,5 @@ build-linux-ubuntu-amd64: - if: $CI_COMMIT_REF_NAME == "master" - if: $CI_COMMIT_REF_NAME == "tags" script: - - cargo build --verbose --release --features bit-vec,generic-array,derive + - cargo build --verbose --release --features bit-vec,bytes,generic-array,derive - sccache -s diff --git a/Cargo.lock b/Cargo.lock index c5638948..c1b21a3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,6 +85,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d30c751592b77c499e7bce34d99d67c2c11bdc0574e9a488ddade14150a4698" +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + [[package]] name = "cast" version = "0.2.7" @@ -423,6 +429,7 @@ dependencies = [ "arrayvec", "bitvec", "byte-slice-cast", + "bytes", "criterion", "generic-array", "impl-trait-for-tuples", diff --git a/Cargo.toml b/Cargo.toml index 644ea109..82117510 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ arrayvec = { version = "0.7", default-features = false } serde = { version = "1.0.102", optional = true } parity-scale-codec-derive = { path = "derive", version = "2.3.1", default-features = false, optional = true } bitvec = { version = "0.20.1", default-features = false, features = ["alloc"], optional = true } +bytes-crate = { package = "bytes", version = "1", default-features = false, optional = true } byte-slice-cast = { version = "1.0.0", default-features = false } generic-array = { version = "0.14.4", optional = true } arbitrary = { version = "1.0.1", features = ["derive"], optional = true } @@ -39,6 +40,7 @@ default = ["std"] derive = ["parity-scale-codec-derive"] std = ["serde", "bitvec/std", "byte-slice-cast/std", "chain-error"] bit-vec = ["bitvec"] +bytes = ["bytes-crate"] fuzz = ["std", "arbitrary"] # Enables the new `MaxEncodedLen` trait. diff --git a/src/codec.rs b/src/codec.rs index 4e59cb8e..3a415b8f 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -371,6 +371,23 @@ mod feature_full_wrapper_type_encode { impl EncodeLike for &str {} } +#[cfg(feature = "bytes")] +mod feature_wrapper_bytes { + use super::*; + use bytes_crate::Bytes; + + impl WrapperTypeEncode for Bytes {} + impl EncodeLike for Bytes {} + impl EncodeLike<&[u8]> for Bytes {} + impl EncodeLike> for Bytes {} + impl EncodeLike for &[u8] {} + impl EncodeLike for Vec {} + + impl WrapperTypeDecode for Bytes { + type Wrapped = Vec; + } +} + impl Encode for X where T: Encode + ?Sized, X: WrapperTypeEncode, @@ -1429,6 +1446,19 @@ mod tests { assert_eq!(>::decode(&mut &encoded[..]).unwrap(), value); } + #[cfg(feature = "bytes")] + #[test] + fn bytes_works_as_expected() { + let input = bytes_crate::Bytes::from_static(b"hello"); + let encoded = Encode::encode(&input); + let encoded_vec = input.to_vec().encode(); + assert_eq!(encoded, encoded_vec); + + assert_eq!( + bytes_crate::Bytes::decode(&mut (&encoded as &[u8])).unwrap(), + Vec::::decode(&mut (&encoded as &[u8])).unwrap() + ); + } fn test_encode_length(thing: &T, len: usize) { assert_eq!(::len(&mut &thing.encode()[..]).unwrap(), len); } From 7edef42af85c63352a515a10ff4526beeebb19ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 22 Jan 2022 21:40:16 +0100 Subject: [PATCH 3/5] Apply suggestions from code review --- Cargo.toml | 3 +-- src/codec.rs | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 82117510..6ffd6e2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ arrayvec = { version = "0.7", default-features = false } serde = { version = "1.0.102", optional = true } parity-scale-codec-derive = { path = "derive", version = "2.3.1", default-features = false, optional = true } bitvec = { version = "0.20.1", default-features = false, features = ["alloc"], optional = true } -bytes-crate = { package = "bytes", version = "1", default-features = false, optional = true } +bytes = { version = "1", default-features = false, optional = true } byte-slice-cast = { version = "1.0.0", default-features = false } generic-array = { version = "0.14.4", optional = true } arbitrary = { version = "1.0.1", features = ["derive"], optional = true } @@ -40,7 +40,6 @@ default = ["std"] derive = ["parity-scale-codec-derive"] std = ["serde", "bitvec/std", "byte-slice-cast/std", "chain-error"] bit-vec = ["bitvec"] -bytes = ["bytes-crate"] fuzz = ["std", "arbitrary"] # Enables the new `MaxEncodedLen` trait. diff --git a/src/codec.rs b/src/codec.rs index 3a415b8f..64ad8164 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -374,7 +374,7 @@ mod feature_full_wrapper_type_encode { #[cfg(feature = "bytes")] mod feature_wrapper_bytes { use super::*; - use bytes_crate::Bytes; + use bytes::Bytes; impl WrapperTypeEncode for Bytes {} impl EncodeLike for Bytes {} @@ -1449,14 +1449,14 @@ mod tests { #[cfg(feature = "bytes")] #[test] fn bytes_works_as_expected() { - let input = bytes_crate::Bytes::from_static(b"hello"); + let input = bytes::Bytes::from_static(b"hello"); let encoded = Encode::encode(&input); let encoded_vec = input.to_vec().encode(); assert_eq!(encoded, encoded_vec); assert_eq!( - bytes_crate::Bytes::decode(&mut (&encoded as &[u8])).unwrap(), - Vec::::decode(&mut (&encoded as &[u8])).unwrap() + &*b"hello"[..], + bytes::Bytes::decode(&mut &encoded[..]).unwrap(), ); } fn test_encode_length(thing: &T, len: usize) { From cf5212fd65f6edb29bcf76153171e1bb44e5b78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 22 Jan 2022 21:56:18 +0100 Subject: [PATCH 4/5] Update src/codec.rs --- src/codec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codec.rs b/src/codec.rs index 64ad8164..69696ea6 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -1455,7 +1455,7 @@ mod tests { assert_eq!(encoded, encoded_vec); assert_eq!( - &*b"hello"[..], + &b"hello"[..], bytes::Bytes::decode(&mut &encoded[..]).unwrap(), ); } From a216506288b18701d6a52959db6da800f5e27045 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Sun, 23 Jan 2022 15:33:32 +0300 Subject: [PATCH 5/5] TRYBUILD=overwrite --- tests/max_encoded_len_ui/not_mel.stderr | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/max_encoded_len_ui/not_mel.stderr b/tests/max_encoded_len_ui/not_mel.stderr index 1506f930..1361561f 100644 --- a/tests/max_encoded_len_ui/not_mel.stderr +++ b/tests/max_encoded_len_ui/not_mel.stderr @@ -13,9 +13,14 @@ error[E0599]: the function or associated item `max_encoded_len` exists for struc 12 | let _ = Generic::::max_encoded_len(); | ^^^^^^^^^^^^^^^ function or associated item cannot be called on `Generic` due to unsatisfied trait bounds | - = note: the following trait bounds were not satisfied: - `NotMel: MaxEncodedLen` - which is required by `Generic: MaxEncodedLen` +note: the following trait bounds were not satisfied because of the requirements of the implementation of `MaxEncodedLen` for `_`: + `NotMel: MaxEncodedLen` + --> tests/max_encoded_len_ui/not_mel.rs:6:18 + | +6 | #[derive(Encode, MaxEncodedLen)] + | ^^^^^^^^^^^^^ +7 | struct Generic { + | ^^^^^^^^^^ note: the following trait must be implemented --> src/max_encoded_len.rs | @@ -27,3 +32,4 @@ note: the following trait must be implemented = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `max_encoded_len`, perhaps you need to implement it: candidate #1: `MaxEncodedLen` + = note: this error originates in the derive macro `MaxEncodedLen` (in Nightly builds, run with -Z macro-backtrace for more info)