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

impl ExclusiveSystemParam for WorldId #11164

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions crates/bevy_ecs/src/world/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::system::{ExclusiveSystemParam, SystemMeta};
use crate::{
component::Tick,
storage::SparseSetIndex,
Expand Down Expand Up @@ -65,6 +66,19 @@ unsafe impl SystemParam for WorldId {
}
}

impl ExclusiveSystemParam for WorldId {
type State = WorldId;
type Item<'s> = WorldId;

fn init(world: &mut World, _system_meta: &mut SystemMeta) -> Self::State {
world.id()
}

fn get_param<'s>(state: &'s mut Self::State, _system_meta: &SystemMeta) -> Self::Item<'s> {
*state
}
}

impl SparseSetIndex for WorldId {
#[inline]
fn sparse_set_index(&self) -> usize {
Expand Down Expand Up @@ -95,6 +109,30 @@ mod tests {
}
}

#[test]
fn world_id_system_param() {
fn test_system(world_id: WorldId) -> WorldId {
world_id
}

let mut world = World::default();
let system_id = world.register_system(test_system);
let world_id = world.run_system(system_id).unwrap();
assert_eq!(world.id(), world_id);
}

#[test]
fn world_id_exclusive_system_param() {
fn test_system(_world: &mut World, world_id: WorldId) -> WorldId {
world_id
}

let mut world = World::default();
let system_id = world.register_system(test_system);
let world_id = world.run_system(system_id).unwrap();
assert_eq!(world.id(), world_id);
}

// We cannot use this test as-is, as it causes other tests to panic due to using the same atomic variable.
// #[test]
// #[should_panic]
Expand Down