From 3248590d18018dcb38afb4af0741c030bf3f8cd3 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 27 Aug 2024 20:41:15 +0200 Subject: feat: add function to get slice with fields of all items --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index e55d8ec..943ad04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,8 @@ where } /// Returns a field of the item with the given index. + /// + /// This function is equivalant to doing `.get_all().get(index)` pub fn get( &self, index: usize, @@ -138,6 +140,18 @@ where Some(unsafe { field_ptr.cast().as_ref() }) } + /// Returns a slice containing the specified field of all items. + pub fn get_all(&self) -> &[>::Field] + where + FieldSel: ItemFieldSelection, + { + let field_arr_byte_offset = self.field_arr_byte_offsets[FieldSel::INDEX]; + + let field_arr_ptr = unsafe { self.ptr.byte_add(field_arr_byte_offset) }; + + unsafe { std::slice::from_raw_parts(field_arr_ptr.as_ptr().cast(), self.len()) } + } + /// Returns the number of items stored in this `MultiVec`. pub fn len(&self) -> usize { @@ -707,4 +721,35 @@ mod tests [9090, 2233, u16::MAX - 75, 8182] ); } + + #[test] + fn get_all_works() + { + let mut multi_vec = MultiVec::::new(); + + #[repr(packed)] + #[allow(dead_code)] + struct Data + { + num_a: [u32; 3], + num_b: [u16; 3], + } + + let data = Data { + num_a: [u32::MAX - 3000, 901, 5560000], + num_b: [20210, 7120, 1010], + }; + + multi_vec.ptr = NonNull::from(&data).cast(); + multi_vec.field_arr_byte_offsets = vec![0, size_of::() * 3]; + multi_vec.length = 3; + multi_vec.capacity = 3; + + assert_eq!( + multi_vec.get_all::(), + [u32::MAX - 3000, 901, 5560000] + ); + + assert_eq!(multi_vec.get_all::(), [20210, 7120, 1010]); + } } -- cgit v1.2.3-18-g5258