Skip to content

Commit

Permalink
Avoid bevy_reflect::List::iter wrapping in release mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rmsthebest committed May 7, 2024
1 parent 22305ac commit d4eb9fa
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ impl<'a> Iterator for ListIter<'a> {
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let value = self.list.get(self.index);
self.index += 1;
self.index += value.is_some() as usize;
value
}

Expand Down Expand Up @@ -508,6 +508,7 @@ pub fn list_debug(dyn_list: &dyn List, f: &mut Formatter<'_>) -> std::fmt::Resul
#[cfg(test)]
mod tests {
use super::DynamicList;
use crate::{Reflect, ReflectRef};
use std::assert_eq;

#[test]
Expand All @@ -522,4 +523,26 @@ mod tests {
assert_eq!(index, value);
}
}

// This test needs to be run in release mode
#[ignore]
#[test]
fn list_avoid_wrap_in_release() {
let b = Box::new(vec![(); usize::MAX]).into_reflect();

let ReflectRef::List(list) = b.reflect_ref() else {
panic!("Not a list...");
};

let mut iter = list.iter();
iter.index = usize::MAX - 1;
println!("{}", iter.index);
assert!(iter.next().is_some());
println!("{}", iter.index);
assert!(iter.next().is_none());
assert!(iter.index == usize::MAX);
// let's do it again to see we are indeed not moving
assert!(iter.next().is_none());
assert!(iter.index == usize::MAX);
}
}

0 comments on commit d4eb9fa

Please sign in to comment.