impl Debug for Component

wip/graph
copygirl 2 months ago
parent ac16cbe527
commit bc3b3f5394
  1. 42
      src/ecs/component.rs

@ -95,8 +95,25 @@ impl std::hash::Hash for Component {
} }
} }
impl std::fmt::Debug for Component {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.is_entity() {
use super::entity::Entity;
unsafe { Entity::from_bits_unchecked(self.to_bits()).fmt(f) }
} else if self.is_relation() {
use super::relation::Relation;
unsafe { Relation::from_bits_unchecked(self.to_bits()).fmt(f) }
} else {
f.debug_struct("Component")
.field("low", &self.low)
.field("high", &self.high)
.finish()
}
}
}
impl Flags { impl Flags {
// NOTE: When changing this be sure to update documentation below. // NOTE: When changing this be sure to update documentation.
pub(crate) const MASK: u32 = 0xF0000000; pub(crate) const MASK: u32 = 0xF0000000;
/// Extracts only the flags from the specified `u32`. /// Extracts only the flags from the specified `u32`.
@ -162,4 +179,27 @@ mod tests {
let back_to_relation: Relation = component.try_into().unwrap(); let back_to_relation: Relation = component.try_into().unwrap();
assert_eq!(relation, back_to_relation); assert_eq!(relation, back_to_relation);
} }
#[test]
fn debug() {
// Component, as long as it's a valid Entity or Relation, will format as such.
// In cases where it's neither (not a valid Component), it has a default formatting.
let entity: Component = Entity::new_checked(69, 1337).unwrap().into();
let relation: Component = Relation::new_checked(420, 9001).unwrap().into();
let invalid = unsafe { Component::from_bits_unchecked(0x20000000_00000001) };
assert_eq!(
"Entity { id: 69, gen: Generation(1337) }",
format!("{entity:?}")
);
assert_eq!(
"Relation { target: 9001, kind: 420 }",
format!("{relation:?}")
);
assert_eq!(
"Component { low: 1, high: 536870912 }",
format!("{invalid:?}")
);
}
} }

Loading…
Cancel
Save