Skip to content

Commit 85a5435

Browse files
committed
u128 PodLength support
1 parent 517cb83 commit 85a5435

2 files changed

Lines changed: 56 additions & 33 deletions

File tree

pod/src/list/list_view.rs

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ mod tests {
187187
super::*,
188188
crate::{
189189
list::List,
190-
primitives::{PodU16, PodU32, PodU64},
190+
primitives::{PodU128, PodU16, PodU32, PodU64},
191191
},
192192
bytemuck_derive::{Pod as DerivePod, Zeroable},
193193
};
@@ -435,38 +435,6 @@ mod tests {
435435
assert_eq!(view_mut.as_slice(), &items[..]);
436436
}
437437

438-
#[test]
439-
fn test_unpack_success_different_length_type() {
440-
// T = u64 (align 8), L = PodU16 (size 2, align 2). Needs 6 bytes padding.
441-
let padding = ListView::<u64, PodU16>::header_padding().unwrap();
442-
assert_eq!(padding, 6);
443-
444-
let length: u16 = 1;
445-
let capacity: usize = 1;
446-
let item_size = size_of::<u64>();
447-
let len_size = size_of::<PodU16>();
448-
let buf_size = len_size + padding + capacity * item_size;
449-
let mut buf = vec![0u8; buf_size];
450-
451-
let pod_len: PodU16 = length.into();
452-
buf[0..len_size].copy_from_slice(bytemuck::bytes_of(&pod_len));
453-
454-
let data_start = len_size + padding;
455-
let items = [12345u64];
456-
let items_bytes = bytemuck::cast_slice(&items);
457-
buf[data_start..].copy_from_slice(items_bytes);
458-
459-
let view_ro = ListView::<u64, PodU16>::unpack(&buf).unwrap();
460-
assert_eq!(view_ro.len(), length as usize);
461-
assert_eq!(view_ro.capacity(), capacity);
462-
assert_eq!(view_ro.as_slice(), &items[..]);
463-
464-
let view_mut = ListView::<u64, PodU16>::unpack_mut(&mut buf).unwrap();
465-
assert_eq!(view_mut.len(), length as usize);
466-
assert_eq!(view_mut.capacity(), capacity);
467-
assert_eq!(view_mut.as_slice(), &items[..]);
468-
}
469-
470438
#[test]
471439
fn test_unpack_fail_buffer_too_small_for_header() {
472440
// T = u64 (align 8), L = PodU32 (size 4). Header size is 8.
@@ -621,4 +589,57 @@ mod tests {
621589
let length_bytes = &buf[0..len_size];
622590
assert_eq!(length_bytes, &[0u8; 4]);
623591
}
592+
593+
macro_rules! test_list_view_for_length_type {
594+
($test_name:ident, $LengthType:ty) => {
595+
#[test]
596+
fn $test_name() {
597+
type T = u64;
598+
599+
let padding = ListView::<T, $LengthType>::header_padding().unwrap();
600+
let length_usize = 2usize;
601+
let capacity = 3;
602+
603+
let item_size = size_of::<T>();
604+
let len_size = size_of::<$LengthType>();
605+
let buf_size = len_size + padding + capacity * item_size;
606+
let mut buf = vec![0u8; buf_size];
607+
608+
// Write length
609+
let pod_len = <$LengthType>::try_from(length_usize).unwrap();
610+
buf[0..len_size].copy_from_slice(bytemuck::bytes_of(&pod_len));
611+
612+
// Write data
613+
let data_start = len_size + padding;
614+
let items = [1000 as T, 2000 as T];
615+
let items_bytes = bytemuck::cast_slice(&items);
616+
buf[data_start..(data_start + items_bytes.len())].copy_from_slice(items_bytes);
617+
618+
// Test read-only view
619+
let view_ro = ListView::<T, $LengthType>::unpack(&buf).unwrap();
620+
assert_eq!(view_ro.len(), length_usize);
621+
assert_eq!(view_ro.capacity(), capacity);
622+
assert_eq!(view_ro.as_slice(), &items[..]);
623+
624+
// Test mutable view
625+
let mut buf_mut = buf.clone();
626+
let view_mut = ListView::<T, $LengthType>::unpack_mut(&mut buf_mut).unwrap();
627+
assert_eq!(view_mut.len(), length_usize);
628+
assert_eq!(view_mut.capacity(), capacity);
629+
assert_eq!(view_mut.as_slice(), &items[..]);
630+
631+
// Test init
632+
let mut init_buf = vec![0xFFu8; buf_size];
633+
let init_view = ListView::<T, $LengthType>::init(&mut init_buf).unwrap();
634+
assert_eq!(init_view.len(), 0);
635+
assert_eq!(init_view.capacity(), capacity);
636+
assert_eq!(<$LengthType>::try_from(0usize).unwrap(), *init_view.length);
637+
}
638+
};
639+
}
640+
641+
test_list_view_for_length_type!(list_view_with_pod_u16, PodU16);
642+
test_list_view_for_length_type!(list_view_with_pod_u32, PodU32);
643+
test_list_view_for_length_type!(list_view_with_pod_u64, PodU64);
644+
test_list_view_for_length_type!(list_view_with_pod_u128, PodU128);
624645
}

pod/src/pod_length.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::primitives::PodU128;
12
use {
23
crate::{
34
error::PodSliceError,
@@ -38,3 +39,4 @@ macro_rules! impl_pod_length_for {
3839
impl_pod_length_for!(PodU16, u16);
3940
impl_pod_length_for!(PodU32, u32);
4041
impl_pod_length_for!(PodU64, u64);
42+
impl_pod_length_for!(PodU128, u128);

0 commit comments

Comments
 (0)