Merge pull request #181 from hack3ric/master

Add `MultiValue::get` & add clear error in `Index`
This commit is contained in:
Alex Orlenko 2022-07-01 10:10:16 +01:00 committed by GitHub
commit 0919ff21c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -248,7 +248,15 @@ impl<'lua> Index<usize> for MultiValue<'lua> {
#[inline]
fn index(&self, index: usize) -> &Self::Output {
&self.0[self.0.len() - index - 1]
if let Some(result) = self.get(index) {
result
} else {
panic!(
"index out of bounds: the len is {} but the index is {}",
self.len(),
index
)
}
}
}
@ -266,6 +274,11 @@ impl<'lua> MultiValue<'lua> {
v
}
#[inline]
pub fn get(&self, index: usize) -> Option<&Value<'lua>> {
self.0.get(self.0.len() - index - 1)
}
#[inline]
pub(crate) fn reserve(&mut self, size: usize) {
self.0.reserve(size);