Implement FromWorld for WorldId (#7726)

# Objective

Allow using `Local<WorldId>` in systems.

## Solution

- Describe the solution used to achieve the objective above.

---

## Changelog

+ `WorldId` now implements the `FromWorld` trait.
This commit is contained in:
JoJoJet 2023-02-20 15:02:16 +00:00
parent 93d7328c6a
commit a69e6a1207

View File

@ -1,12 +1,16 @@
use crate::{ use crate::{
storage::SparseSetIndex, storage::SparseSetIndex,
system::{ReadOnlySystemParam, SystemParam}, system::{ReadOnlySystemParam, SystemParam},
world::{FromWorld, World},
}; };
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
// We use usize here because that is the largest `Atomic` we want to require // 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 // Note that this *is* used by external crates as well as for internal safety checks
pub struct WorldId(usize); pub struct WorldId(usize);
@ -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 // SAFETY: Has read-only access to shared World metadata
unsafe impl ReadOnlySystemParam for WorldId {} unsafe impl ReadOnlySystemParam for WorldId {}