Skip to content

Commit 00f8394

Browse files
committed
bevy_reflect: IntoIter for DynamicList and DynamicMap (#4108)
# Objective In some cases, you may want to take ownership of the values in `DynamicList` or `DynamicMap`. I came across this need while trying to implement a custom deserializer, but couldn't get ownership of the values in the list. ## Solution Implemented `IntoIter` for both `DynamicList` and `DynamicMap`.
1 parent 4aa5605 commit 00f8394

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

crates/bevy_reflect/src/list.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ impl<'a> Iterator for ListIter<'a> {
191191
}
192192
}
193193

194+
impl IntoIterator for DynamicList {
195+
type Item = Box<dyn Reflect>;
196+
type IntoIter = std::vec::IntoIter<Self::Item>;
197+
198+
fn into_iter(self) -> Self::IntoIter {
199+
self.values.into_iter()
200+
}
201+
}
202+
194203
impl<'a> ExactSizeIterator for ListIter<'a> {}
195204

196205
/// Applies the elements of `b` to the corresponding elements of `a`.
@@ -244,3 +253,21 @@ pub fn list_partial_eq<L: List>(a: &L, b: &dyn Reflect) -> Option<bool> {
244253

245254
Some(true)
246255
}
256+
257+
#[cfg(test)]
258+
mod tests {
259+
use super::DynamicList;
260+
261+
#[test]
262+
fn test_into_iter() {
263+
let mut list = DynamicList::default();
264+
list.push(0usize);
265+
list.push(1usize);
266+
list.push(2usize);
267+
let items = list.into_iter();
268+
for (index, item) in items.into_iter().enumerate() {
269+
let value = item.take::<usize>().expect("couldn't downcast to usize");
270+
assert_eq!(index, value);
271+
}
272+
}
273+
}

crates/bevy_reflect/src/map.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ impl<'a> Iterator for MapIter<'a> {
220220
}
221221
}
222222

223+
impl IntoIterator for DynamicMap {
224+
type Item = (Box<dyn Reflect>, Box<dyn Reflect>);
225+
type IntoIter = std::vec::IntoIter<Self::Item>;
226+
227+
fn into_iter(self) -> Self::IntoIter {
228+
self.values.into_iter()
229+
}
230+
}
231+
223232
impl<'a> ExactSizeIterator for MapIter<'a> {}
224233

225234
/// Compares a [`Map`] with a [`Reflect`] value.
@@ -253,3 +262,28 @@ pub fn map_partial_eq<M: Map>(a: &M, b: &dyn Reflect) -> Option<bool> {
253262

254263
Some(true)
255264
}
265+
266+
#[cfg(test)]
267+
mod tests {
268+
use super::DynamicMap;
269+
270+
#[test]
271+
fn test_into_iter() {
272+
let expected = vec!["foo", "bar", "baz"];
273+
274+
let mut map = DynamicMap::default();
275+
map.insert(0usize, expected[0].to_string());
276+
map.insert(1usize, expected[1].to_string());
277+
map.insert(2usize, expected[2].to_string());
278+
279+
for (index, item) in map.into_iter().enumerate() {
280+
let key = item.0.take::<usize>().expect("couldn't downcast to usize");
281+
let value = item
282+
.1
283+
.take::<String>()
284+
.expect("couldn't downcast to String");
285+
assert_eq!(index, key);
286+
assert_eq!(expected[index], value);
287+
}
288+
}
289+
}

0 commit comments

Comments
 (0)