Skip to content

Commit

Permalink
Switch array impls to const generics (#261)
Browse files Browse the repository at this point in the history
* Switch array impls to const generics

* addr comments
  • Loading branch information
austinabell authored Apr 6, 2021
1 parent 1a0a11d commit ab18de2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
64 changes: 19 additions & 45 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions src/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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]);
}

Expand Down

0 comments on commit ab18de2

Please sign in to comment.