Add MultiValue::get & add clear error in Index

This commit is contained in:
Eric Long 2022-07-01 01:59:40 +08:00
parent b46b476f80
commit 553251761f
No known key found for this signature in database
GPG key ID: 583FAB4005C652BE

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);