-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add World::iter_resources()
and World::iter_resources_mut()
methods
#12019
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2184,6 +2184,53 @@ impl World { | |||||||||||||||||||||||||||
.get_mut_by_id(component_id) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
/// Iterates over all resources in the world | ||||||||||||||||||||||||||||
pub fn iter_resources(&self) -> impl Iterator<Item = (&ComponentInfo, Ptr<'_>)> { | ||||||||||||||||||||||||||||
self.storages.resources.iter().map(|(component_id, data)| { | ||||||||||||||||||||||||||||
let component_info = self.components.get_info(component_id).unwrap_or_else(|| { | ||||||||||||||||||||||||||||
panic!("ComponentInfo should exist for all resources in the world, but it does not for {:?}", component_id); | ||||||||||||||||||||||||||||
alice-i-cecile marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
let ptr = data.get_data().unwrap_or_else(|| { | ||||||||||||||||||||||||||||
panic!( | ||||||||||||||||||||||||||||
"When iterating all resources, resource of type {} was supposed to exist, but did not.", | ||||||||||||||||||||||||||||
component_info.name() | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
(component_info, ptr) | ||||||||||||||||||||||||||||
Comment on lines
+2270
to
+2280
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
/// Mutably iterates over all resources in the world | ||||||||||||||||||||||||||||
pub fn iter_resources_mut(&mut self) -> impl Iterator<Item = (&ComponentInfo, MutUntyped<'_>)> { | ||||||||||||||||||||||||||||
self.storages.resources.iter().map(|(component_id, data)| { | ||||||||||||||||||||||||||||
let component_info = self.components.get_info(component_id).unwrap_or_else(|| { | ||||||||||||||||||||||||||||
panic!("ComponentInfo should exist for all resources in the world, but it does not for {:?}", component_id); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let (ptr, ticks) = data.get_with_ticks().unwrap_or_else(|| { | ||||||||||||||||||||||||||||
panic!( | ||||||||||||||||||||||||||||
"When iterating all resources, resource of type {} was supposed to exist, but did not.", | ||||||||||||||||||||||||||||
component_info.name() | ||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// SAFETY: | ||||||||||||||||||||||||||||
// - ??? | ||||||||||||||||||||||||||||
coreh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
let ticks = unsafe { | ||||||||||||||||||||||||||||
TicksMut::from_tick_cells(ticks, self.last_change_tick(), self.read_change_tick()) | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let mut_untyped = MutUntyped { | ||||||||||||||||||||||||||||
// SAFETY: | ||||||||||||||||||||||||||||
// - ??? | ||||||||||||||||||||||||||||
value: unsafe { ptr.assert_unique() }, | ||||||||||||||||||||||||||||
coreh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
ticks, | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
(component_info, mut_untyped) | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
// Schedule-related methods | ||||||||||||||||||||||||||||
|
@@ -2495,6 +2542,9 @@ mod tests { | |||||||||||||||||||||||||||
#[derive(Resource)] | ||||||||||||||||||||||||||||
struct TestResource(u32); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[derive(Resource)] | ||||||||||||||||||||||||||||
struct TestResource2(String); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||
fn get_resource_by_id() { | ||||||||||||||||||||||||||||
let mut world = World::new(); | ||||||||||||||||||||||||||||
|
@@ -2796,4 +2846,57 @@ mod tests { | |||||||||||||||||||||||||||
let mut world = World::new(); | ||||||||||||||||||||||||||||
world.spawn(()); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||
fn iterate_resources() { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These need a test case to ensure that iteration doesn't panic if you remove a resource. Also for miri. |
||||||||||||||||||||||||||||
let mut world = World::new(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
world.insert_resource(TestResource(42)); | ||||||||||||||||||||||||||||
world.insert_resource(TestResource2("Hello, world!".to_string())); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let mut iter = world.iter_resources(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let (info, ptr) = iter.next().unwrap(); | ||||||||||||||||||||||||||||
assert_eq!(info.name(), std::any::type_name::<TestResource>()); | ||||||||||||||||||||||||||||
assert_eq!(unsafe { ptr.deref::<TestResource>().0 }, 42); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let (info, ptr) = iter.next().unwrap(); | ||||||||||||||||||||||||||||
assert_eq!(info.name(), std::any::type_name::<TestResource2>()); | ||||||||||||||||||||||||||||
assert_eq!( | ||||||||||||||||||||||||||||
unsafe { &ptr.deref::<TestResource2>().0 }, | ||||||||||||||||||||||||||||
&"Hello, world!".to_string() | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
assert!(iter.next().is_none()); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#[test] | ||||||||||||||||||||||||||||
fn iterate_resources_mut() { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These need a test case to ensure that iteration doesn't panic if you remove a resource. Also for miri. |
||||||||||||||||||||||||||||
let mut world = World::new(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
world.insert_resource(TestResource(42)); | ||||||||||||||||||||||||||||
world.insert_resource(TestResource2("Hello, world!".to_string())); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let mut iter = world.iter_resources_mut(); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let (info, mut mut_untyped) = iter.next().unwrap(); | ||||||||||||||||||||||||||||
assert_eq!(info.name(), std::any::type_name::<TestResource>()); | ||||||||||||||||||||||||||||
unsafe { mut_untyped.as_mut().deref_mut::<TestResource>().0 = 43 }; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
let (info, mut mut_untyped) = iter.next().unwrap(); | ||||||||||||||||||||||||||||
assert_eq!(info.name(), std::any::type_name::<TestResource2>()); | ||||||||||||||||||||||||||||
unsafe { | ||||||||||||||||||||||||||||
mut_untyped.as_mut().deref_mut::<TestResource2>().0 = "Hello, world?".to_string() | ||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
assert!(iter.next().is_none()); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
std::mem::drop(iter); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
assert_eq!(world.resource::<TestResource>().0, 43); | ||||||||||||||||||||||||||||
assert_eq!( | ||||||||||||||||||||||||||||
world.resource::<TestResource2>().0, | ||||||||||||||||||||||||||||
"Hello, world?".to_string() | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.