Remove OnceLock
usage from bevy_ecs
(#16870)
# Objective - Fixes #16868 ## Solution - Replaced several usages of `OnceLock` within `bevy_ecs` with `const`s ## Testing - CI
This commit is contained in:
parent
1f2d0e6308
commit
1371619d84
@ -130,7 +130,6 @@ portable-atomic-util = { version = "0.2.4", features = [
|
||||
spin = { version = "0.9.8", default-features = false, features = [
|
||||
"spin_mutex",
|
||||
"rwlock",
|
||||
"once",
|
||||
] }
|
||||
tracing = { version = "0.1", default-features = false, optional = true }
|
||||
log = { version = "0.4", default-features = false }
|
||||
|
@ -8,12 +8,12 @@ use alloc::{borrow::ToOwned, boxed::Box};
|
||||
use core::{fmt::Debug, hash::Hash, ops::Deref};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::{OnceLock, PoisonError, RwLock};
|
||||
use std::sync::{PoisonError, RwLock};
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use spin::{once::Once as OnceLock, rwlock::RwLock};
|
||||
use spin::rwlock::RwLock;
|
||||
|
||||
use bevy_utils::HashSet;
|
||||
use bevy_utils::{FixedHasher, HashSet};
|
||||
|
||||
/// An interned value. Will stay valid until the end of the program and will not drop.
|
||||
///
|
||||
@ -126,12 +126,12 @@ impl Internable for str {
|
||||
/// The implementation ensures that two equal values return two equal [`Interned<T>`] values.
|
||||
///
|
||||
/// To use an [`Interner<T>`], `T` must implement [`Internable`].
|
||||
pub struct Interner<T: ?Sized + 'static>(OnceLock<RwLock<HashSet<&'static T>>>);
|
||||
pub struct Interner<T: ?Sized + 'static>(RwLock<HashSet<&'static T>>);
|
||||
|
||||
impl<T: ?Sized> Interner<T> {
|
||||
/// Creates a new empty interner
|
||||
pub const fn new() -> Self {
|
||||
Self(OnceLock::new())
|
||||
Self(RwLock::new(HashSet::with_hasher(FixedHasher)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,18 +142,12 @@ impl<T: Internable + ?Sized> Interner<T> {
|
||||
/// [`Interned<T>`] using the obtained static reference. Subsequent calls for the same `value`
|
||||
/// will return [`Interned<T>`] using the same static reference.
|
||||
pub fn intern(&self, value: &T) -> Interned<T> {
|
||||
#[cfg(feature = "std")]
|
||||
let lock = self.0.get_or_init(Default::default);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let lock = self.0.call_once(Default::default);
|
||||
|
||||
{
|
||||
#[cfg(feature = "std")]
|
||||
let set = lock.read().unwrap_or_else(PoisonError::into_inner);
|
||||
let set = self.0.read().unwrap_or_else(PoisonError::into_inner);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let set = lock.read();
|
||||
let set = self.0.read();
|
||||
|
||||
if let Some(value) = set.get(value) {
|
||||
return Interned(*value);
|
||||
@ -162,10 +156,10 @@ impl<T: Internable + ?Sized> Interner<T> {
|
||||
|
||||
{
|
||||
#[cfg(feature = "std")]
|
||||
let mut set = lock.write().unwrap_or_else(PoisonError::into_inner);
|
||||
let mut set = self.0.write().unwrap_or_else(PoisonError::into_inner);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let mut set = lock.write();
|
||||
let mut set = self.0.write();
|
||||
|
||||
if let Some(value) = set.get(value) {
|
||||
Interned(*value)
|
||||
|
@ -338,13 +338,13 @@ impl<T: SparseSetIndex> Access<T> {
|
||||
|
||||
/// Sets this as having access to all resources (i.e. `&World`).
|
||||
#[inline]
|
||||
pub fn read_all_resources(&mut self) {
|
||||
pub const fn read_all_resources(&mut self) {
|
||||
self.reads_all_resources = true;
|
||||
}
|
||||
|
||||
/// Sets this as having mutable access to all resources (i.e. `&mut World`).
|
||||
#[inline]
|
||||
pub fn write_all_resources(&mut self) {
|
||||
pub const fn write_all_resources(&mut self) {
|
||||
self.reads_all_resources = true;
|
||||
self.writes_all_resources = true;
|
||||
}
|
||||
|
@ -1,9 +1,3 @@
|
||||
#[cfg(feature = "std")]
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
use spin::once::Once as OnceLock;
|
||||
|
||||
use crate::{
|
||||
change_detection::{Mut, MutUntyped, Ref, Ticks, TicksMut},
|
||||
component::{ComponentId, Tick},
|
||||
@ -220,21 +214,13 @@ impl<'w, 's> From<&'w FilteredResourcesMut<'_, 's>> for FilteredResources<'w, 's
|
||||
|
||||
impl<'w> From<&'w World> for FilteredResources<'w, 'static> {
|
||||
fn from(value: &'w World) -> Self {
|
||||
static READ_ALL_RESOURCES: OnceLock<Access<ComponentId>> = OnceLock::new();
|
||||
let access = {
|
||||
let init = || {
|
||||
const READ_ALL_RESOURCES: &Access<ComponentId> = {
|
||||
const ACCESS: Access<ComponentId> = {
|
||||
let mut access = Access::new();
|
||||
access.read_all_resources();
|
||||
access
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
let access = READ_ALL_RESOURCES.get_or_init(init);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let access = READ_ALL_RESOURCES.call_once(init);
|
||||
|
||||
access
|
||||
&ACCESS
|
||||
};
|
||||
|
||||
let last_run = value.last_change_tick();
|
||||
@ -243,7 +229,7 @@ impl<'w> From<&'w World> for FilteredResources<'w, 'static> {
|
||||
unsafe {
|
||||
Self::new(
|
||||
value.as_unsafe_world_cell_readonly(),
|
||||
access,
|
||||
READ_ALL_RESOURCES,
|
||||
last_run,
|
||||
this_run,
|
||||
)
|
||||
@ -507,22 +493,13 @@ impl<'w, 's> FilteredResourcesMut<'w, 's> {
|
||||
|
||||
impl<'w> From<&'w mut World> for FilteredResourcesMut<'w, 'static> {
|
||||
fn from(value: &'w mut World) -> Self {
|
||||
static WRITE_ALL_RESOURCES: OnceLock<Access<ComponentId>> = OnceLock::new();
|
||||
|
||||
let access = {
|
||||
let init = || {
|
||||
const WRITE_ALL_RESOURCES: &Access<ComponentId> = {
|
||||
const ACCESS: Access<ComponentId> = {
|
||||
let mut access = Access::new();
|
||||
access.write_all_resources();
|
||||
access
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
let access = WRITE_ALL_RESOURCES.get_or_init(init);
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
let access = WRITE_ALL_RESOURCES.call_once(init);
|
||||
|
||||
access
|
||||
&ACCESS
|
||||
};
|
||||
|
||||
let last_run = value.last_change_tick();
|
||||
@ -531,7 +508,7 @@ impl<'w> From<&'w mut World> for FilteredResourcesMut<'w, 'static> {
|
||||
unsafe {
|
||||
Self::new(
|
||||
value.as_unsafe_world_cell_readonly(),
|
||||
access,
|
||||
WRITE_ALL_RESOURCES,
|
||||
last_run,
|
||||
this_run,
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user