Commit 3c2e0423 authored by Wei Tang's avatar Wei Tang Committed by Bastian Köcher
Browse files

Add generic-array and vecarray support (#134)

* Add generic-array support

* Add vecarray support

* Use 0.12 version of generic-array

* Update cargo lock

* Move impl to separate files

* Make sure std features of vecarray are disabled by default

* Use with_capacity

* Add features to CI

* Add missing license

* Add tests for generic-array

* Add tests for vec-array

* Apply suggestions from code review
parent 89a6e55d
Pipeline #44918 passed with stages
in 12 minutes and 52 seconds
......@@ -23,7 +23,7 @@ cache:
test:rust:stable:
stage: test
script:
- time cargo test --verbose --all --features bit-vec,derive
- time cargo test --verbose --all --features bit-vec,generic-array,vec-array,derive
only:
- triggers
- tags
......@@ -37,7 +37,7 @@ test:rust:stable:
test:rust:stable:no_derive:
stage: test
script:
- time cargo test --verbose --features bit-vec
- time cargo test --verbose --features bit-vec,generic-array,vec-array
only:
- triggers
- tags
......@@ -51,7 +51,7 @@ test:rust:stable:no_derive:
check:rust:stable:no_derive_no_std_full:
stage: test
script:
- time cargo check --verbose --no-default-features --features bit-vec,full
- time cargo check --verbose --no-default-features --features bit-vec,generic-array,vec-array,full
only:
- triggers
- tags
......@@ -65,7 +65,7 @@ check:rust:stable:no_derive_no_std_full:
check:rust:stable:no_derive_no_std:
stage: test
script:
- time cargo check --verbose --no-default-features --features bit-vec
- time cargo check --verbose --no-default-features --features bit-vec,generic-array,vec-array
only:
- triggers
- tags
......@@ -79,7 +79,7 @@ check:rust:stable:no_derive_no_std:
check:rust:stable:no_derive_full:
stage: test
script:
- time cargo check --verbose --features bit-vec,full
- time cargo check --verbose --features bit-vec,generic-array,vec-array,full
only:
- triggers
- tags
......@@ -93,7 +93,7 @@ check:rust:stable:no_derive_full:
bench:rust:nightly:
stage: test
script:
- time cargo +nightly bench --features bit-vec,derive
- time cargo +nightly bench --features bit-vec,generic-array,vec-array,derive
only:
- triggers
- tags
......@@ -109,11 +109,10 @@ bench:rust:nightly:
build:linux:ubuntu:amd64:
stage: build
script:
- cargo build --verbose --release --features bit-vec,derive
- cargo build --verbose --release --features bit-vec,generic-array,vec-array,derive
only:
- master
- tags
- web
tags:
- rust
......@@ -181,6 +181,14 @@ name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "generic-array"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "honggfuzz"
version = "0.5.45"
......@@ -270,9 +278,12 @@ dependencies = [
"bitvec 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byte-slice-cast 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"criterion 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)",
"parity-scale-codec-derive 1.0.1",
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"vecarray 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -475,6 +486,11 @@ dependencies = [
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "typenum"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-width"
version = "0.1.5"
......@@ -485,6 +501,15 @@ name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vecarray"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "walkdir"
version = "2.2.7"
......@@ -544,6 +569,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum csv-core 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa5cdef62f37e6ffe7d1f07a381bc0db32b7a3ff1cac0de56cb0d81e71f53d65"
"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b"
"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
"checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
"checksum honggfuzz 0.5.45 (registry+https://github.com/rust-lang/crates.io-index)" = "24c27b4aa3049d6d10d8e33d52c9d03ca9aec18f8a449b246f8c4a5b0c10fb34"
"checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358"
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
......@@ -579,8 +605,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum tinytemplate 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4574b75faccaacddb9b284faecdf0b544b80b6b294f3d062d325c5726a209c20"
"checksum toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b8c96d7873fa7ef8bdeb3a9cda3ac48389b4154f32b9803b4bc26220b677b039"
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
"checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum vecarray 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "961d198df806bde7351c13988de47ae31a02989ee881190e73d42214caf1a091"
"checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1"
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
......
......@@ -14,6 +14,9 @@ serde = { version = "1.0", optional = true }
parity-scale-codec-derive = { path = "derive", version = "1.0", default-features = false, optional = true }
bitvec = { version = "0.14.0", default-features = false, features = ["alloc"], optional = true }
byte-slice-cast = { version = "0.3.1", optional = true }
generic-array = { version = "0.12", optional = true }
vecarray = { version = "0.1", default-features = false, optional = true }
typenum = { version = "1.10", optional = true }
[dev-dependencies]
serde_derive = { version = "1.0" }
......@@ -31,8 +34,9 @@ bench = false
[features]
default = ["std"]
derive = ["parity-scale-codec-derive"]
std = ["serde", "bitvec/std"]
std = ["serde", "bitvec/std", "vecarray/std"]
bit-vec = ["bitvec", "byte-slice-cast"]
vec-array = ["vecarray", "typenum"]
# WARNING: DO _NOT_ USE THIS FEATURE IF YOU ARE WORKING ON CONSENSUS CODE!*
#
......
......@@ -890,7 +890,6 @@ macro_rules! impl_non_endians {
impl_endians!(u16, u32, u64, u128, i16, i32, i64, i128);
impl_non_endians!(u8 {IS_U8}, i8, bool);
#[cfg(test)]
mod tests {
use super::*;
......
// Copyright 2019 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{Encode, Decode, Error, Output, Input};
use crate::alloc::vec::Vec;
impl<T: Encode, L: generic_array::ArrayLength<T>> Encode for generic_array::GenericArray<T, L> {
fn encode_to<W: Output>(&self, dest: &mut W) {
for item in self.iter() {
item.encode_to(dest);
}
}
}
impl<T: Decode, L: generic_array::ArrayLength<T>> Decode for generic_array::GenericArray<T, L> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut r = Vec::with_capacity(L::to_usize());
for _ in 0..L::to_usize() {
r.push(T::decode(input)?);
}
let i = generic_array::GenericArray::from_exact_iter(r);
match i {
Some(a) => Ok(a),
None => Err("array length does not match definition".into()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use generic_array::{GenericArray, arr, arr_impl};
#[test]
fn generic_array() {
let test = arr![u8; 3, 4, 5];
let encoded = test.encode();
assert_eq!(test, GenericArray::<u8, typenum::U3>::decode(&mut &encoded[..]).unwrap());
let test = arr![u16; 3, 4, 5, 6, 7, 8, 0];
let encoded = test.encode();
assert_eq!(test, GenericArray::<u16, typenum::U7>::decode(&mut &encoded[..]).unwrap());
let test = arr![u32; 3, 4, 5, 0, 1];
let encoded = test.encode();
assert_eq!(test, GenericArray::<u32, typenum::U5>::decode(&mut &encoded[..]).unwrap());
let test = arr![u64; 3];
let encoded = test.encode();
assert_eq!(test, GenericArray::<u64, typenum::U1>::decode(&mut &encoded[..]).unwrap());
}
}
......@@ -243,6 +243,10 @@ mod joiner;
mod keyedvec;
#[cfg(feature = "bit-vec")]
mod bit_vec;
#[cfg(feature = "generic-array")]
mod generic_array;
#[cfg(feature = "vec-array")]
mod vec_array;
mod decode_all;
mod encode_append;
......
// Copyright 2019 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{Encode, Decode, Error, Output, Input};
use crate::alloc::vec::Vec;
use core::convert::TryFrom;
impl<T: Encode, L: typenum::Unsigned> Encode for vecarray::VecArray<T, L> {
fn encode_to<W: Output>(&self, dest: &mut W) {
for item in self.iter() {
item.encode_to(dest);
}
}
}
impl<T: Decode, L: typenum::Unsigned> Decode for vecarray::VecArray<T, L> {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut r = Vec::with_capacity(L::to_usize());
for _ in 0..L::to_usize() {
r.push(T::decode(input)?);
}
vecarray::VecArray::try_from(r)
.map_err(|_| "array length does not match definition".into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use vecarray::VecArray;
#[test]
fn vec_array() {
let test = VecArray::<u8, typenum::U3>::try_from([3u8, 4, 5].to_vec()).ok().unwrap();
let encoded = test.encode();
assert_eq!(test, VecArray::<u8, typenum::U3>::decode(&mut &encoded[..]).unwrap());
let test = VecArray::<u16, typenum::U7>::try_from([3u16, 4, 5, 6, 7, 8, 0].to_vec()).ok().unwrap();
let encoded = test.encode();
assert_eq!(test, VecArray::<u16, typenum::U7>::decode(&mut &encoded[..]).unwrap());
let test = VecArray::<u32, typenum::U5>::try_from([3u32, 4, 5, 0, 1].to_vec()).ok().unwrap();
let encoded = test.encode();
assert_eq!(test, VecArray::<u32, typenum::U5>::decode(&mut &encoded[..]).unwrap());
let test = VecArray::<u64, typenum::U1>::try_from([3u64].to_vec()).ok().unwrap();
let encoded = test.encode();
assert_eq!(test, VecArray::<u64, typenum::U1>::decode(&mut &encoded[..]).unwrap());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment