Skip to content

Commit

Permalink
Add support for fixed sized arrays up to 32 elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
djg committed May 20, 2019
1 parent ef7262c commit 0ffde54
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ where
}
}

macro_rules! impl_for_arrays {
($($len:tt)+) => {
$(impl<T: PeekPoke> PeekPoke for [T; $len] {
fn max_size() -> usize {
$len * <T>::max_size()
}
fn poke_into(&self, bytes: *mut u8) -> *mut u8 {
self.iter().fold(bytes, |bytes, e| e.poke_into(bytes))
}
fn peek_from(&mut self, bytes: *const u8) -> *const u8 {
self.iter_mut().fold(bytes, |bytes, e| e.peek_from(bytes))
}
})+
}
}

impl_for_arrays! {
01 02 03 04 05 06 07 08 09 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32
}

impl PeekPoke for () {
fn max_size() -> usize {
0
Expand Down
2 changes: 0 additions & 2 deletions tests/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ fn test_option() {
);
}

/*
#[test]
fn test_fixed_size_array() {
assert_eq!(<[u32; 32]>::max_size(), 32 * size_of::<u32>());
assert_eq!(<[u64; 8]>::max_size(), 8 * size_of::<u64>());
assert_eq!(<[u8; 19]>::max_size(), 19 * size_of::<u8>());
}
*/

#[test]
fn test_tuple() {
Expand Down
2 changes: 0 additions & 2 deletions tests/round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ fn test_option() {
the_same(None::<usize>);
}

/*
#[test]
fn test_fixed_size_array() {
the_same([24u32; 32]);
the_same([1u64, 2, 3, 4, 5, 6, 7, 8]);
the_same([0u8; 19]);
}
*/

#[test]
fn test_tuple() {
Expand Down

0 comments on commit 0ffde54

Please sign in to comment.