From bcb96c4a7f24d4af32cbb0df9818b7d8972eb63a Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 26 Aug 2024 21:12:31 +0200 Subject: feat: make get function return Option --- src/lib.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fefcb97..698e641 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,15 +101,12 @@ where pub fn get( &self, index: usize, - ) -> &>::Field + ) -> Option<&>::Field> where FieldSel: ItemFieldSelection, { if index >= self.length { - panic!( - "Index {index} is out of bounds in MultiVec with length {}", - self.length - ); + return None; } let field_metadata = FieldSel::metadata(); @@ -120,7 +117,7 @@ where let field_ptr = unsafe { field_arr_ptr.add(field_metadata.size * index) }; - unsafe { field_ptr.cast().as_ref() } + Some(unsafe { field_ptr.cast().as_ref() }) } /// Returns the number of items stored in this `MultiVec`. @@ -513,7 +510,7 @@ mod tests multi_vec.push(Foo { num_a: 12338, num_b: 191 }); - let item = multi_vec.get::(1); + let item = multi_vec.get::(1).unwrap(); println!("yay: {}", *item); } -- cgit v1.2.3-18-g5258