summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-08-26 21:12:31 +0200
committerHampusM <hampus@hampusmat.com>2024-08-26 21:12:31 +0200
commitbcb96c4a7f24d4af32cbb0df9818b7d8972eb63a (patch)
treeb3be93b286d227f70fe6e72cf289e1e7897eebd6 /src/lib.rs
parent17e05f57f4b95b51196893cd341fdc089f20c6bb (diff)
feat: make get function return Option
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs11
1 files 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<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);
}