Unverified Commit ab18de24 authored by Austin Abell's avatar Austin Abell Committed by GitHub
Browse files

Switch array impls to const generics (#261)

* Switch array impls to const generics

* addr comments
parent 1a0a11dc
Pipeline #132883 passed with stages
in 20 minutes and 53 seconds
......@@ -22,9 +22,9 @@ dependencies = [
[[package]]
name = "arrayvec"
version = "0.5.1"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
checksum = "5a2f58b0bb10c380af2b26e57212856b8c9a59e0925b4c20f4a174a49734eaf7"
[[package]]
name = "atty"
......
......@@ -9,7 +9,7 @@ categories = ["encoding"]
edition = "2018"
[dependencies]
arrayvec = { version = "0.5.1", default-features = false, features = ["array-sizes-33-128", "array-sizes-129-255"] }
arrayvec = { version = "0.7", default-features = false }
serde = { version = "1.0.102", optional = true }
parity-scale-codec-derive = { path = "derive", version = "2.0.1", default-features = false, optional = true }
bitvec = { version = "0.20.1", default-features = false, features = ["alloc"], optional = true }
......
......@@ -688,57 +688,31 @@ impl_for_non_zero! {
NonZeroU128,
}
macro_rules! impl_array {
( $( $n:expr, )* ) => {
$(
impl<T: Encode> Encode for [T; $n] {
fn size_hint(&self) -> usize {
mem::size_of::<T>() * $n
}
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
encode_slice_no_len(&self[..], dest)
}
}
impl<T: Encode, const N: usize> Encode for [T; N] {
fn size_hint(&self) -> usize {
mem::size_of::<T>() * N
}
impl<T: Decode> Decode for [T; $n] {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut r = ArrayVec::new();
for _ in 0..$n {
r.push(T::decode(input)?);
}
let i = r.into_inner();
fn encode_to<W: Output + ?Sized>(&self, dest: &mut W) {
encode_slice_no_len(&self[..], dest)
}
}
match i {
Ok(a) => Ok(a),
Err(_) => Err("failed to get inner array from ArrayVec".into()),
}
}
}
impl<T: Decode, const N: usize> Decode for [T; N] {
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
let mut array = ArrayVec::new();
for _ in 0..N {
array.push(T::decode(input)?);
}
impl<T: EncodeLike<U>, U: Encode> EncodeLike<[U; $n]> for [T; $n] {}
)*
match array.into_inner() {
Ok(a) => Ok(a),
Err(_) => panic!("We decode `N` elements; qed"),
}
}
}
impl_array!(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124,
125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172,
173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188,
189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236,
237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252,
253, 254, 255, 256, 384, 512, 768, 1024, 2048, 4096, 8192, 16384, 32768,
);
impl<T: EncodeLike<U>, U: Encode, const N: usize> EncodeLike<[U; N]> for [T; N] {}
impl Encode for str {
fn size_hint(&self) -> usize {
......
......@@ -23,9 +23,9 @@ use crate::Error;
#[cfg(feature = "fuzz")]
use arbitrary::Arbitrary;
struct ArrayVecWrapper<T: arrayvec::Array>(ArrayVec<T>);
struct ArrayVecWrapper<const N: usize>(ArrayVec<u8, N>);
impl<T: arrayvec::Array<Item=u8>> Output for ArrayVecWrapper<T> {
impl<const N: usize> Output for ArrayVecWrapper<N> {
fn write(&mut self, bytes: &[u8]) {
let old_len = self.0.len();
let new_len = old_len + bytes.len();
......@@ -248,7 +248,7 @@ impl<'a> Encode for CompactRef<'a, u8> {
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 2]>::new());
let mut r = ArrayVecWrapper(ArrayVec::<u8, 2>::new());
self.encode_to(&mut r);
f(&r.0)
}
......@@ -277,7 +277,7 @@ impl<'a> Encode for CompactRef<'a, u16> {
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
let mut r = ArrayVecWrapper(ArrayVec::<u8, 4>::new());
self.encode_to(&mut r);
f(&r.0)
}
......@@ -311,7 +311,7 @@ impl<'a> Encode for CompactRef<'a, u32> {
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 5]>::new());
let mut r = ArrayVecWrapper(ArrayVec::<u8, 5>::new());
self.encode_to(&mut r);
f(&r.0)
}
......@@ -353,7 +353,7 @@ impl<'a> Encode for CompactRef<'a, u64> {
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 9]>::new());
let mut r = ArrayVecWrapper(ArrayVec::<u8, 9>::new());
self.encode_to(&mut r);
f(&r.0)
}
......@@ -397,7 +397,7 @@ impl<'a> Encode for CompactRef<'a, u128> {
}
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let mut r = ArrayVecWrapper(ArrayVec::<[u8; 17]>::new());
let mut r = ArrayVecWrapper(ArrayVec::<u8, 17>::new());
self.encode_to(&mut r);
f(&r.0)
}
......@@ -813,13 +813,13 @@ mod tests {
#[test]
#[should_panic]
fn array_vec_output_oob() {
let mut v = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
let mut v = ArrayVecWrapper(ArrayVec::<u8, 4>::new());
v.write(&[1, 2, 3, 4, 5]);
}
#[test]
fn array_vec_output() {
let mut v = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
let mut v = ArrayVecWrapper(ArrayVec::<u8, 4>::new());
v.write(&[1, 2, 3, 4]);
}
......
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