Skip to content

Commit

Permalink
Implement FromWorld for WorldId (#7726)
Browse files Browse the repository at this point in the history
# Objective

Allow using `Local<WorldId>` in systems.

## Solution

- Describe the solution used to achieve the objective above.

---

## Changelog

+ `WorldId` now implements the `FromWorld` trait.
  • Loading branch information
JoJoJet committed Feb 20, 2023
1 parent 93d7328 commit a69e6a1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/bevy_ecs/src/world/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::{
storage::SparseSetIndex,
system::{ReadOnlySystemParam, SystemParam},
world::{FromWorld, World},
};
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
// We use usize here because that is the largest `Atomic` we want to require
/// A unique identifier for a [`super::World`].
/// A unique identifier for a [`World`].
///
/// The trait [`FromWorld`] is implemented for this type, which returns the
/// ID of the world passed to [`FromWorld::from_world`].
// Note that this *is* used by external crates as well as for internal safety checks
pub struct WorldId(usize);

Expand All @@ -30,6 +34,13 @@ impl WorldId {
}
}

impl FromWorld for WorldId {
#[inline]
fn from_world(world: &mut World) -> Self {
world.id()
}
}

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

Expand Down

0 comments on commit a69e6a1

Please sign in to comment.