diff --git a/Cargo.lock b/Cargo.lock index d36efcb1..1a02a31b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index bb708f85..16336913 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/codec.rs b/src/codec.rs index 1db43ee4..54fe4c97 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -688,57 +688,31 @@ impl_for_non_zero! { NonZeroU128, } -macro_rules! impl_array { - ( $( $n:expr, )* ) => { - $( - impl Encode for [T; $n] { - fn size_hint(&self) -> usize { - mem::size_of::() * $n - } - - fn encode_to(&self, dest: &mut W) { - encode_slice_no_len(&self[..], dest) - } - } +impl Encode for [T; N] { + fn size_hint(&self) -> usize { + mem::size_of::() * N + } - impl Decode for [T; $n] { - fn decode(input: &mut I) -> Result { - let mut r = ArrayVec::new(); - for _ in 0..$n { - r.push(T::decode(input)?); - } - let i = r.into_inner(); + fn encode_to(&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 Decode for [T; N] { + fn decode(input: &mut I) -> Result { + let mut array = ArrayVec::new(); + for _ in 0..N { + array.push(T::decode(input)?); + } - impl, 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, U: Encode, const N: usize> EncodeLike<[U; N]> for [T; N] {} impl Encode for str { fn size_hint(&self) -> usize { diff --git a/src/compact.rs b/src/compact.rs index 073a84a0..4af468c7 100644 --- a/src/compact.rs +++ b/src/compact.rs @@ -23,9 +23,9 @@ use crate::Error; #[cfg(feature = "fuzz")] use arbitrary::Arbitrary; -struct ArrayVecWrapper(ArrayVec); +struct ArrayVecWrapper(ArrayVec); -impl> Output for ArrayVecWrapper { +impl Output for ArrayVecWrapper { 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>(&self, f: F) -> R { - let mut r = ArrayVecWrapper(ArrayVec::<[u8; 2]>::new()); + let mut r = ArrayVecWrapper(ArrayVec::::new()); self.encode_to(&mut r); f(&r.0) } @@ -277,7 +277,7 @@ impl<'a> Encode for CompactRef<'a, u16> { } fn using_encoded R>(&self, f: F) -> R { - let mut r = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new()); + let mut r = ArrayVecWrapper(ArrayVec::::new()); self.encode_to(&mut r); f(&r.0) } @@ -311,7 +311,7 @@ impl<'a> Encode for CompactRef<'a, u32> { } fn using_encoded R>(&self, f: F) -> R { - let mut r = ArrayVecWrapper(ArrayVec::<[u8; 5]>::new()); + let mut r = ArrayVecWrapper(ArrayVec::::new()); self.encode_to(&mut r); f(&r.0) } @@ -353,7 +353,7 @@ impl<'a> Encode for CompactRef<'a, u64> { } fn using_encoded R>(&self, f: F) -> R { - let mut r = ArrayVecWrapper(ArrayVec::<[u8; 9]>::new()); + let mut r = ArrayVecWrapper(ArrayVec::::new()); self.encode_to(&mut r); f(&r.0) } @@ -397,7 +397,7 @@ impl<'a> Encode for CompactRef<'a, u128> { } fn using_encoded R>(&self, f: F) -> R { - let mut r = ArrayVecWrapper(ArrayVec::<[u8; 17]>::new()); + let mut r = ArrayVecWrapper(ArrayVec::::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::::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::::new()); v.write(&[1, 2, 3, 4]); }