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 da7ec0d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 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,25 @@ 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![1u32]).into_reflect();

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

let mut iter = list.iter();

let one = iter.next().unwrap();
assert_eq!(one.downcast_ref::<u32>().unwrap(), &1);

for _ in 0..(usize::MAX) {
assert!(iter.next().is_none());
}
// wrapping could occur here
assert!(iter.next().is_none());
}
}

0 comments on commit da7ec0d

Please sign in to comment.