bevy/crates/bevy_ecs/hecs/examples/format.rs
2020-08-16 05:02:06 -07:00

39 lines
1.1 KiB
Rust

// modified by Bevy contributors
//! One way to the contents of an entity, as you might do for debugging. A similar pattern could
//! also be useful for serialization, or other row-oriented generic operations.
fn format_entity(entity: bevy_hecs::EntityRef<'_>) -> String {
fn fmt<T: bevy_hecs::Component + std::fmt::Display>(
entity: bevy_hecs::EntityRef<'_>,
) -> Option<String> {
Some(entity.get::<T>()?.to_string())
}
const FUNCTIONS: &[&dyn Fn(bevy_hecs::EntityRef<'_>) -> Option<String>] =
&[&fmt::<i32>, &fmt::<bool>, &fmt::<f64>];
let mut out = String::new();
for f in FUNCTIONS {
if let Some(x) = f(entity) {
if out.is_empty() {
out.push_str("[");
} else {
out.push_str(", ");
}
out.push_str(&x);
}
}
if out.is_empty() {
out.push_str(&"[]");
} else {
out.push(']');
}
out
}
fn main() {
let mut world = bevy_hecs::World::new();
let e = world.spawn((42, true));
println!("{}", format_entity(world.entity(e).unwrap()));
}