Skip to content

Commit

Permalink
Implement SystemParam for WorldId (bevyengine#7741)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#7736

## Solution

Implement the `SystemParam` trait for `WorldId`

## Changelog

- `WorldId` can now be taken as a system parameter and will return the id of the world the system is running in
  • Loading branch information
LiamGallagher737 authored and myreprise1 committed Feb 20, 2023
1 parent 5f61654 commit 1926b00
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion crates/bevy_ecs/src/world/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::storage::SparseSetIndex;
use crate::{
storage::SparseSetIndex,
system::{ReadOnlySystemParam, SystemParam},
};
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
Expand Down Expand Up @@ -27,6 +30,27 @@ impl WorldId {
}
}

// SAFETY: Has read-only access to shared World metadata
unsafe impl ReadOnlySystemParam for WorldId {}

// SAFETY: A World's ID is immutable and fetching it from the World does not borrow anything
unsafe impl SystemParam for WorldId {
type State = ();

type Item<'world, 'state> = WorldId;

fn init_state(_: &mut super::World, _: &mut crate::system::SystemMeta) -> Self::State {}

unsafe fn get_param<'world, 'state>(
_: &'state mut Self::State,
_: &crate::system::SystemMeta,
world: &'world super::World,
_: u32,
) -> Self::Item<'world, 'state> {
world.id
}
}

impl SparseSetIndex for WorldId {
#[inline]
fn sparse_set_index(&self) -> usize {
Expand Down

0 comments on commit 1926b00

Please sign in to comment.