Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bytes::Bytes implementation #309

Merged
merged 5 commits into from
Jan 23, 2022
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
16 changes: 8 additions & 8 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = { 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 }
Expand Down
30 changes: 30 additions & 0 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,23 @@ mod feature_full_wrapper_type_encode {
impl EncodeLike<String> for &str {}
}

#[cfg(feature = "bytes")]
mod feature_wrapper_bytes {
use super::*;
use bytes::Bytes;

impl WrapperTypeEncode for Bytes {}
impl EncodeLike for Bytes {}
impl EncodeLike<&[u8]> for Bytes {}
impl EncodeLike<Vec<u8>> for Bytes {}
impl EncodeLike<Bytes> for &[u8] {}
impl EncodeLike<Bytes> for Vec<u8> {}

impl WrapperTypeDecode for Bytes {
type Wrapped = Vec<u8>;
}
}

impl<T, X> Encode for X where
T: Encode + ?Sized,
X: WrapperTypeEncode<Target = T>,
Expand Down Expand Up @@ -1429,6 +1446,19 @@ mod tests {
assert_eq!(<Vec<OptionBool>>::decode(&mut &encoded[..]).unwrap(), value);
}

#[cfg(feature = "bytes")]
#[test]
fn bytes_works_as_expected() {
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!(
&b"hello"[..],
bytes::Bytes::decode(&mut &encoded[..]).unwrap(),
);
}
fn test_encode_length<T: Encode + Decode + DecodeLength>(thing: &T, len: usize) {
assert_eq!(<T as DecodeLength>::len(&mut &thing.encode()[..]).unwrap(), len);
}
Expand Down
22 changes: 18 additions & 4 deletions tests/max_encoded_len_ui/not_mel.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0599]: the function or associated item `max_encoded_len` exists for struct `Generic<NotMel>`, 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`
Expand All @@ -13,9 +13,23 @@ error[E0599]: the function or associated item `max_encoded_len` exists for struc
12 | let _ = Generic::<NotMel>::max_encoded_len();
| ^^^^^^^^^^^^^^^ function or associated item cannot be called on `Generic<NotMel>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`NotMel: MaxEncodedLen`
which is required by `Generic<NotMel>: 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<T> {
| ^^^^^^^^^^
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`
= note: this error originates in the derive macro `MaxEncodedLen` (in Nightly builds, run with -Z macro-backtrace for more info)