Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid bevy_reflect::List::iter wrapping in release mode #13271

Merged
merged 4 commits into from
May 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 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;
rmsthebest marked this conversation as resolved.
Show resolved Hide resolved
rmsthebest marked this conversation as resolved.
Show resolved Hide resolved
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,24 @@ mod tests {
assert_eq!(index, value);
}
}

// This test needs to be run in release mode
#[ignore]
rmsthebest marked this conversation as resolved.
Show resolved Hide resolved
#[test]
rmsthebest marked this conversation as resolved.
Show resolved Hide resolved
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;
assert!(iter.next().is_some());
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);
}
}