Skip to content

Commit

Permalink
Merge pull request #127 from ferrumc-rs/hotfix/ecs_query_iter_item
Browse files Browse the repository at this point in the history
Made query return `(Entity=usize, Item)` instead of `Item` use FERRUMC_LOG for logging.
  • Loading branch information
ReCore-sys authored Nov 17, 2024
2 parents 96379f1 + 95ae29e commit 00ec7bb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
14 changes: 14 additions & 0 deletions src/lib/ecs/src/components/storage.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use dashmap::DashMap;
use dashmap::mapref::one::{Ref, RefMut};
Expand Down Expand Up @@ -95,4 +96,17 @@ impl<T> DerefMut for ComponentRefMut<'_, T> {
#[allow(clippy::explicit_auto_deref)]
&mut *self.guard
}
}


impl<T: Component+Debug> Debug for ComponentRef<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.guard.fmt(f)
}
}

impl<T: Component+Debug> Debug for ComponentRefMut<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.guard.fmt(f)
}
}
17 changes: 10 additions & 7 deletions src/lib/ecs/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::{ComponentManager};
use crate::components::storage::{Component, ComponentRef, ComponentRefMut};
use crate::components::ComponentManager;
use crate::entities::Entity;
use crate::ECSResult;

Expand Down Expand Up @@ -78,11 +78,11 @@ impl<'a, Q: QueryItem> Query<'a, Q> {
_marker: std::marker::PhantomData,
}
}

pub fn entities(&self) -> &[Entity] {
&self.entities
}

pub fn into_entities(self) -> Vec<Entity> {
self.entities
}
Expand All @@ -95,14 +95,14 @@ mod iter_impl {

impl<'a, Q: QueryItem> Iterator for Query<'a, Q>
{
type Item = Q::Item<'a>;
type Item = (Entity, Q::Item<'a>);

fn next(&mut self) -> Option<Self::Item> {
while let Some(entity) = self.entities.pop() {
let Ok(item) = Q::fetch(entity, self.component_storage) else {
continue;
};
return Some(item);
return Some((entity, item));
}
None
}
Expand All @@ -113,15 +113,18 @@ mod iter_impl {
Q: QueryItem + Send,
Q::Item<'a>: Send,
{
type Item = Q::Item<'a>;
type Item = (Entity, Q::Item<'a>);

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
{
self.entities
.into_par_iter()
.filter_map(move |entity| Q::fetch(entity, self.component_storage).ok())
.filter_map(|entity| {
let item = Q::fetch(entity, self.component_storage);
item.ok().map(|item| (entity, item))
})
.drive_unindexed(consumer)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ecs/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn test_basic() {
}
let query = Query::<(&Player, &mut Position)>::new(&component_storage);

ParallelIterator::for_each(query.into_par_iter(), |(_player, position)| {
ParallelIterator::for_each(query.into_par_iter(), |(_eid, (_player, position))| {
let sleep_duration = Duration::from_millis(100 * (position.x as u64));
thread::sleep(sleep_duration);
});
Expand Down
5 changes: 3 additions & 2 deletions src/lib/utils/logging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const LOG_LEVEL: &str = "trace";

pub fn init_logging() {
let trace_level = {
let trace_level = std::env::args()
/*let trace_level = std::env::args()
.find(|arg| arg.starts_with("--log="))
.map(|arg| arg.replace("--log=", ""));
.map(|arg| arg.replace("--log=", ""));*/
let trace_level = std::env::var("FERRUMC_LOG").ok();

let trace_level = trace_level.unwrap_or_else(|| LOG_LEVEL.to_string());

Expand Down
2 changes: 1 addition & 1 deletion src/lib/world/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl From<std::io::Error> for WorldError {
fn from(err: std::io::Error) -> Self {
match err.kind() {
ErrorKind::PermissionDenied => PermissionError(err.to_string()),
ErrorKind::ReadOnlyFilesystem => PermissionError(err.to_string()),
// ErrorKind::ReadOnlyFilesystem => PermissionError(err.to_string()),
_ => GenericIOError(err.to_string()),
}
}
Expand Down

0 comments on commit 00ec7bb

Please sign in to comment.