From 553251761fdbc85229a29da5b9b9ae07719e176a Mon Sep 17 00:00:00 2001 From: Eric Long Date: Fri, 1 Jul 2022 01:59:40 +0800 Subject: [PATCH] Add `MultiValue::get` & add clear error in `Index` --- src/value.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/value.rs b/src/value.rs index 1288ca0..b77f2bc 100644 --- a/src/value.rs +++ b/src/value.rs @@ -248,7 +248,15 @@ impl<'lua> Index 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);