Skip to content

Commit 0f54128

Browse files
committed
Change Varargs to hold slices instead of iterators
Better performance of the code I will write next.
1 parent 5e58f6b commit 0f54128

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

gdnative-core/src/export/method.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ impl<C: NativeClass, F: StaticArgsMethod<C>> Method<C> for StaticArgs<F> {
212212
/// for common operations with them. Can also be used as an iterator.
213213
pub struct Varargs<'a> {
214214
idx: usize,
215-
iter: std::slice::Iter<'a, &'a Variant>,
215+
args: &'a [&'a Variant],
216216
}
217217

218218
impl<'a> Varargs<'a> {
219219
/// Returns the amount of arguments left.
220220
#[inline]
221221
pub fn len(&self) -> usize {
222-
self.iter.len()
222+
self.args.len() - self.idx
223223
}
224224

225225
#[inline]
@@ -250,7 +250,7 @@ impl<'a> Varargs<'a> {
250250
/// Returns the remaining arguments as a slice of `Variant`s.
251251
#[inline]
252252
pub fn as_slice(&self) -> &'a [&'a Variant] {
253-
self.iter.as_slice()
253+
self.args
254254
}
255255

256256
/// Discard the rest of the arguments, and return an error if there is any.
@@ -282,18 +282,21 @@ impl<'a> Varargs<'a> {
282282
pub unsafe fn from_sys(num_args: libc::c_int, args: *mut *mut sys::godot_variant) -> Self {
283283
let args = std::slice::from_raw_parts(args, num_args as usize);
284284
let args = std::mem::transmute::<&[*mut sys::godot_variant], &[&Variant]>(args);
285-
Self {
286-
idx: 0,
287-
iter: args.iter(),
288-
}
285+
Self { idx: 0, args }
289286
}
290287
}
291288

292289
impl<'a> Iterator for Varargs<'a> {
293290
type Item = &'a Variant;
294291
#[inline]
295292
fn next(&mut self) -> Option<Self::Item> {
296-
self.iter.next().copied()
293+
if self.idx < self.args.len() {
294+
let idx = self.idx;
295+
self.idx += 1;
296+
Some(self.args[idx])
297+
} else {
298+
None
299+
}
297300
}
298301
}
299302

@@ -391,8 +394,7 @@ impl<'r, 'a, T: FromVariant> ArgBuilder<'r, 'a, T> {
391394
} = self;
392395
let idx = args.idx;
393396

394-
if let Some(arg) = args.iter.next() {
395-
args.idx += 1;
397+
if let Some(arg) = args.next() {
396398
T::from_variant(arg).map(Some).map_err(|err| ArgumentError {
397399
site: *site,
398400
kind: ArgumentErrorKind::CannotConvert {

0 commit comments

Comments
 (0)