Skip to content

Commit

Permalink
Remove thread_local feature dependency on std
Browse files Browse the repository at this point in the history
It's now possible to provide a function that will give the current thread id

World has a builder to avoid the exponential number of new_* functions
Use siphasher when std feature is disabled

Part of #181
  • Loading branch information
leudz committed Sep 4, 2023
1 parent 4133552 commit 664cd34
Show file tree
Hide file tree
Showing 40 changed files with 491 additions and 280 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
with:
components: miri

- run: cargo miri test --tests --lib --no-default-features
- run: cargo miri test --tests --lib --no-default-features --features=std
clippy:
runs-on: ubuntu-latest
needs: changes
Expand Down
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ version = "0.6.0"
members = ["bunny_demo", "shipyard_proc", "square_eater", "visualizer"]

[dependencies]
hashbrown = "0.12.0"
hashbrown = { version = "0.14.0", default-features = false, features = [
"inline-more",
"allocator-api2",
] }
lock_api = "0.4.0"
rayon = { version = "1.5.1", optional = true }
serde = { version = "1.0.0", optional = true, default-features = false, features = [
"derive",
] }
shipyard_proc = { version = "0.3.0", path = "./shipyard_proc", optional = true }
siphasher = "1.0.0"
tracing = { version = "0.1.0", default-features = false, optional = true }

[features]
default = ["parallel", "proc", "std"]
parallel = ["rayon"]
proc = ["shipyard_proc"]
serde1 = ["serde", "hashbrown/serde"]
std = []
thread_local = ["std"]
std = ["hashbrown/ahash"]
thread_local = []

[dev-dependencies]
bincode = "1.3.3"
Expand Down
10 changes: 9 additions & 1 deletion project.yamis.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ serial = ["clean", "_miri"]

[tasks._miri]
program = "cargo"
args = ["+nightly", "miri", "test", "--tests", "--lib", "--no-default-features"]
args = [
"+nightly",
"miri",
"test",
"--tests",
"--lib",
"--no-default-features",
"--features=std",
]
private = true

[tasks.clean]
Expand Down
20 changes: 12 additions & 8 deletions src/all_storages/custom_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl CustomStorageAccess for AllStorages {
}),
}
} else {
if std::thread::current().id() != self.thread_id {
if (self.thread_id_generator)() != self.main_thread_id {
return Err(error::GetStorage::StorageBorrow {
name: Some(type_name::<S>()),
id: StorageId::of::<S>(),
Expand All @@ -366,7 +366,7 @@ impl CustomStorageAccess for AllStorages {
let storage = unsafe {
&*storages
.entry(storage_id)
.or_insert_with(|| SBox::new_non_send(f(), self.thread_id))
.or_insert_with(|| SBox::new_non_send(f(), self.thread_id_generator.clone()))
.0
}
.borrow()
Expand Down Expand Up @@ -478,7 +478,7 @@ impl CustomStorageAccess for AllStorages {
}),
}
} else {
if std::thread::current().id() != self.thread_id {
if (self.thread_id_generator)() != self.main_thread_id {
return Err(error::GetStorage::StorageBorrow {
name: Some(type_name::<S>()),
id: StorageId::of::<S>(),
Expand All @@ -492,7 +492,9 @@ impl CustomStorageAccess for AllStorages {
let storage = unsafe {
&*storages
.entry(storage_id)
.or_insert_with(|| SBox::new_non_send_sync(f(), self.thread_id))
.or_insert_with(|| {
SBox::new_non_send_sync(f(), self.thread_id_generator.clone())
})
.0
}
.borrow()
Expand Down Expand Up @@ -602,7 +604,7 @@ impl CustomStorageAccess for AllStorages {
}),
}
} else {
if std::thread::current().id() != self.thread_id {
if (self.thread_id_generator)() != self.main_thread_id {
return Err(error::GetStorage::StorageBorrow {
name: Some(type_name::<S>()),
id: StorageId::of::<S>(),
Expand All @@ -616,7 +618,7 @@ impl CustomStorageAccess for AllStorages {
let storage = unsafe {
&*storages
.entry(storage_id)
.or_insert_with(|| SBox::new_non_send(f(), self.thread_id))
.or_insert_with(|| SBox::new_non_send(f(), self.thread_id_generator.clone()))
.0
}
.borrow_mut()
Expand Down Expand Up @@ -728,7 +730,7 @@ impl CustomStorageAccess for AllStorages {
}),
}
} else {
if std::thread::current().id() != self.thread_id {
if (self.thread_id_generator)() != self.main_thread_id {
return Err(error::GetStorage::StorageBorrow {
name: Some(type_name::<S>()),
id: StorageId::of::<S>(),
Expand All @@ -742,7 +744,9 @@ impl CustomStorageAccess for AllStorages {
let storage = unsafe {
&*storages
.entry(storage_id)
.or_insert_with(|| SBox::new_non_send_sync(f(), self.thread_id))
.or_insert_with(|| {
SBox::new_non_send_sync(f(), self.thread_id_generator.clone())
})
.0
}
.borrow_mut()
Expand Down
8 changes: 4 additions & 4 deletions src/all_storages/delete_any/custom_delete_any.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::component::Component;
use crate::entity_id::EntityId;
use crate::sparse_set::SparseSet;
use hashbrown::hash_set::HashSet;
use crate::ShipHashSet;

/// Trait used as a bound for AllStorages::delete_any.
pub trait CustomDeleteAny {
fn delete_any(&mut self, ids: &mut HashSet<EntityId>, current: u32);
fn delete_any(&mut self, ids: &mut ShipHashSet<EntityId>, current: u32);
}

impl CustomDeleteAny for () {
#[inline]
fn delete_any(&mut self, _: &mut HashSet<EntityId>, _current: u32) {}
fn delete_any(&mut self, _: &mut ShipHashSet<EntityId>, _current: u32) {}
}

impl<T: Component> CustomDeleteAny for SparseSet<T> {
#[inline]
fn delete_any(&mut self, ids: &mut HashSet<EntityId>, current: u32) {
fn delete_any(&mut self, ids: &mut ShipHashSet<EntityId>, current: u32) {
ids.extend(&self.dense);
self.private_clear(current);
}
Expand Down
7 changes: 4 additions & 3 deletions src/all_storages/delete_any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::all_storages::AllStorages;
use crate::storage::{Storage, StorageId};
#[cfg(doc)]
use crate::world::World;
use crate::ShipHashSet;
use core::hash::BuildHasherDefault;
pub use custom_delete_any::CustomDeleteAny;
use hashbrown::hash_set::HashSet;

/// Trait used as a bound for [`World::delete_any`] and [AllStorages::delete_any].
pub trait TupleDeleteAny {
Expand All @@ -17,7 +18,7 @@ impl<T: 'static + Storage + CustomDeleteAny> TupleDeleteAny for T {
#[inline]
#[track_caller]
fn delete_any(all_storages: &mut AllStorages) {
let mut ids = HashSet::new();
let mut ids = ShipHashSet::with_hasher(BuildHasherDefault::default());

let current = all_storages.get_current();
let storages = all_storages.storages.get_mut();
Expand All @@ -41,7 +42,7 @@ macro_rules! impl_delete_any {
($(($storage: ident, $index: tt))+) => {
impl<$($storage: 'static + Storage + CustomDeleteAny),+> TupleDeleteAny for ($($storage,)+) {
fn delete_any(all_storages: &mut AllStorages) {
let mut ids = HashSet::default();
let mut ids = ShipHashSet::with_hasher(BuildHasherDefault::default());

let current = all_storages.get_current();
let storages = all_storages.storages.get_mut();
Expand Down
Loading

0 comments on commit 664cd34

Please sign in to comment.