diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-26 21:12:31 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-26 21:12:31 +0200 |
commit | bcb96c4a7f24d4af32cbb0df9818b7d8972eb63a (patch) | |
tree | b3be93b286d227f70fe6e72cf289e1e7897eebd6 | |
parent | 17e05f57f4b95b51196893cd341fdc089f20c6bb (diff) |
feat: make get function return Option
-rw-r--r-- | src/lib.rs | 11 |
1 files changed, 4 insertions, 7 deletions
@@ -101,15 +101,12 @@ where pub fn get<FieldSel>( &self, index: usize, - ) -> &<FieldSel as ItemFieldSelection<ItemT>>::Field + ) -> Option<&<FieldSel as ItemFieldSelection<ItemT>>::Field> where FieldSel: ItemFieldSelection<ItemT>, { 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::<FooFieldNumA>(1); + let item = multi_vec.get::<FooFieldNumA>(1).unwrap(); println!("yay: {}", *item); } |