Skip to content

Commit

Permalink
macro: newtype_of_entityview || don't debug print children when none …
Browse files Browse the repository at this point in the history
…are present
  • Loading branch information
Indra-db committed Oct 3, 2024
1 parent 883fb71 commit f0f3a89
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
22 changes: 16 additions & 6 deletions flecs_ecs/src/core/entity_view/entity_view_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ impl<'a> DerefMut for EntityView<'a> {
}
}

impl<'a> std::fmt::Display for EntityView<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<'a> core::fmt::Display for EntityView<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if let Some(name) = self.get_name() {
write!(f, "{}", name)
write!(f, "#{} | {}", self.id, name)
} else {
write!(f, "{}", *self.id)
write!(f, "#{}", self.id)
}
}
}

impl<'a> std::fmt::Debug for EntityView<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<'a> core::fmt::Debug for EntityView<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let entity_display = match self.get_name() {
Some(name_str) => format!("Entity: #{} | \"{}\"", self.id, name_str),
None => format!("Entity: #{}", self.id),
Expand All @@ -58,6 +58,16 @@ impl<'a> std::fmt::Debug for EntityView<'a> {
}
});
});

if children.is_empty() {
return write!(
f,
"\n {}\n Archetype:\n - {}\n",
entity_display,
archetype_types_str.join("\n - "),
);
}

write!(
f,
"\n {}\n Archetype:\n - {}\n Children:\n - {}\n",
Expand Down
74 changes: 74 additions & 0 deletions flecs_ecs/src/core/entity_view/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/// A macro to generate a newtype wrapper for `EntityView` with various utility implementations.
///
/// This macro creates a new struct with the specified name that wraps around `EntityView<'a>`.
/// It also provides implementations for common traits like `Deref`, `DerefMut`, `From`, `Debug` and `Display`
/// to make working with the new type seamless.
///
/// # Parameters
/// - `$name`: The name of the new struct that wraps `EntityView<'a>`.
///
/// # Generated Code
///
/// - A struct named `$name` is created, wrapping an `EntityView<'a>`.
/// - A constructor `new` that takes an `EntityView<'a>` is generated for the newtype.
/// - Implements `Deref` and `DerefMut` traits to allow easy access to the underlying `EntityView<'a>`.
/// - Implements `From` for both conversions between `EntityView<'a>` and the new type, and between the new type and `Entity`.
/// - Provides `Debug` and `Display` implementations for better formatting support.
#[macro_export]
macro_rules! newtype_of_entity_view {
($name:ident) => {
#[derive(Clone, Copy)]
pub struct $name<'a>(pub EntityView<'a>);

impl<'a> $name<'a> {
pub fn new(entity: EntityView<'a>) -> Self {
Self(entity)
}
}

impl<'a> core::ops::Deref for $name<'a> {
type Target = EntityView<'a>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<'a> core::ops::DerefMut for $name<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<'a> From<EntityView<'a>> for $name<'a> {
fn from(entity: EntityView<'a>) -> Self {
Self::new(entity)
}
}

impl<'a> From<$name<'a>> for EntityView<'a> {
fn from(entity: $name<'a>) -> Self {
entity.0
}
}

impl From<$name<'_>> for Entity {
#[inline]
fn from(name: $name) -> Self {
name.0.id()
}
}

impl<'a> core::fmt::Debug for $name<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self.0)
}
}

impl<'a> core::fmt::Display for $name<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
};
}
1 change: 1 addition & 0 deletions flecs_ecs/src/core/entity_view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod bulk_entity_builder;
mod entity_view_const;
mod entity_view_impl;
mod entity_view_mut;
mod macros;

pub use entity_view_const::EntityView;
pub use entity_view_const::EntityViewGet;
4 changes: 3 additions & 1 deletion flecs_ecs/src/core/utility/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ pub(crate) fn has_default_hook(world: *const sys::ecs_world_t, id: u64) -> bool
pub fn debug_separate_archetype_types_into_strings(archetype: &Archetype) -> Vec<String> {
let mut result = Vec::with_capacity(archetype.count());
let mut skip_next = false; // To skip the next part after joining
let archetype_str = archetype.to_string().unwrap_or_else(|| "empty".to_string());
let archetype_str = archetype
.to_string()
.unwrap_or_else(|| "empty entity | no components".to_string());

let parts: Vec<&str> = archetype_str.split(',').map(|s| s.trim()).collect();

Expand Down

0 comments on commit f0f3a89

Please sign in to comment.