Skip to content

Commit

Permalink
Add bytes implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 committed Jan 12, 2022
1 parent 2c9ec7a commit f4917d0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
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.

2 changes: 2 additions & 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-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 }
Expand All @@ -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.
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_crate::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_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::<u8>::decode(&mut (&encoded as &[u8])).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

0 comments on commit f4917d0

Please sign in to comment.