Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Adding Debug implementations for App, Stage, Schedule, Query, QueryState, etc. #6214

Closed
wants to merge 11 commits into from
16 changes: 16 additions & 0 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,28 @@ pub struct App {
sub_apps: HashMap<AppLabelId, SubApp>,
}

impl Debug for App {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "App {{ sub_apps: ")?;
f.debug_map()
.entries(self.sub_apps.iter().map(|(k, v)| (k, v)))
.finish()?;
write!(f, "}}")
}
}

/// Each `SubApp` has its own [`Schedule`] and [`World`], enabling a separation of concerns.
struct SubApp {
app: App,
runner: Box<dyn Fn(&mut World, &mut App)>,
}

impl Debug for SubApp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SubApp")
targrub marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Default for App {
fn default() -> Self {
let mut app = App::empty();
Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ pub struct QueryState<Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
pub(crate) filter_state: F::State,
}

impl<Q: WorldQuery, F: ReadOnlyWorldQuery> std::fmt::Debug for QueryState<Q, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"QueryState<Q, F> matched_table_ids: {} matched_archetype_ids: {}",
self.matched_table_ids.len(),
self.matched_archetype_ids.len()
)
}
}

impl<Q: WorldQuery, F: ReadOnlyWorldQuery> FromWorld for QueryState<Q, F> {
fn from_world(world: &mut World) -> Self {
world.query_filtered()
Expand Down
9 changes: 8 additions & 1 deletion crates/bevy_ecs/src/schedule/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{schedule::SystemContainer, world::World};
use core::fmt::Debug;
use downcast_rs::{impl_downcast, Downcast};

pub trait ParallelSystemExecutor: Downcast + Send + Sync {
Expand All @@ -8,9 +9,15 @@ pub trait ParallelSystemExecutor: Downcast + Send + Sync {
fn run_systems(&mut self, systems: &mut [SystemContainer], world: &mut World);
}

impl Debug for dyn ParallelSystemExecutor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "dyn ParallelSystemExecutor")
}
}

impl_downcast!(ParallelSystemExecutor);

#[derive(Default)]
#[derive(Debug, Default)]
pub struct SingleThreadedExecutor;

impl ParallelSystemExecutor for SingleThreadedExecutor {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use bevy_utils::HashMap;
/// In this way, the properties of the child schedule can be set differently from the parent.
/// For example, it can be set to run only once during app execution, while the parent schedule
/// runs indefinitely.
#[derive(Default)]
#[derive(Debug, Default)]
pub struct Schedule {
stages: HashMap<StageLabelId, Box<dyn Stage>>,
stage_order: Vec<StageLabelId>,
Expand Down
24 changes: 22 additions & 2 deletions crates/bevy_ecs/src/schedule/run_criteria.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{
prelude::System,
schedule::{GraphNode, RunCriteriaLabel, RunCriteriaLabelId},
system::{BoxedSystem, IntoSystem, Local},
world::World,
};
use core::fmt::Debug;
use std::borrow::Cow;

/// Determines whether a system should be executed or not, and how many times it should be ran each
Expand Down Expand Up @@ -66,7 +68,19 @@ impl From<bool> for ShouldRun {
}
}

#[derive(Default)]
impl Debug for dyn System<In = (), Out = ShouldRun> + 'static {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "System with In=(), Out=ShouldRun")
targrub marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Debug for dyn System<In = ShouldRun, Out = ShouldRun> + 'static {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "System with In=ShouldRun, Out=ShouldRun")
}
}

#[derive(Debug, Default)]
pub(crate) struct BoxedRunCriteria {
criteria_system: Option<BoxedSystem<(), ShouldRun>>,
initialized: bool,
Expand All @@ -93,6 +107,7 @@ impl BoxedRunCriteria {
}
}

#[derive(Debug)]
pub(crate) enum RunCriteriaInner {
Single(BoxedSystem<(), ShouldRun>),
Piped {
Expand All @@ -101,6 +116,7 @@ pub(crate) enum RunCriteriaInner {
},
}

#[derive(Debug)]
pub(crate) struct RunCriteriaContainer {
pub(crate) should_run: ShouldRun,
pub(crate) inner: RunCriteriaInner,
Expand Down Expand Up @@ -165,17 +181,19 @@ impl GraphNode for RunCriteriaContainer {
}
}

#[derive(Debug)]
pub enum RunCriteriaDescriptorOrLabel {
Descriptor(RunCriteriaDescriptor),
Label(RunCriteriaLabelId),
}

#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub(crate) enum DuplicateLabelStrategy {
Panic,
Discard,
}

#[derive(Debug)]
pub struct RunCriteriaDescriptor {
pub(crate) system: RunCriteriaSystem,
pub(crate) label: Option<RunCriteriaLabelId>,
Expand All @@ -184,6 +202,7 @@ pub struct RunCriteriaDescriptor {
pub(crate) after: Vec<RunCriteriaLabelId>,
}

#[derive(Debug)]
pub(crate) enum RunCriteriaSystem {
Single(BoxedSystem<(), ShouldRun>),
Piped(BoxedSystem<ShouldRun, ShouldRun>),
Expand Down Expand Up @@ -326,6 +345,7 @@ where
}
}

#[derive(Debug)]
pub struct RunCriteria {
label: RunCriteriaLabelId,
}
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
};
use bevy_ecs_macros::Resource;
use bevy_utils::{tracing::warn, HashMap, HashSet};
use core::fmt::Debug;
use downcast_rs::{impl_downcast, Downcast};

use super::IntoSystemDescriptor;
Expand All @@ -25,6 +26,12 @@ pub trait Stage: Downcast + Send + Sync {
fn run(&mut self, world: &mut World);
}

impl Debug for dyn Stage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Stage {:?}", self.as_any().downcast_ref::<SystemStage>())
targrub marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl_downcast!(Stage);

/// When this resource is present in the `App`'s `Resources`,
Expand All @@ -51,6 +58,7 @@ pub struct ReportExecutionOrderAmbiguities;

/// Stores and executes systems. Execution order is not defined unless explicitly specified;
/// see `SystemDescriptor` documentation.
#[derive(Debug)]
pub struct SystemStage {
/// The WorldId this stage was last run on.
world_id: Option<WorldId>,
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ecs/src/schedule/system_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
schedule::{GraphNode, RunCriteriaLabelId, SystemDescriptor, SystemLabelId},
system::System,
};
use core::fmt::Debug;
use std::borrow::Cow;

pub struct SystemContainer {
Expand Down Expand Up @@ -79,6 +80,12 @@ impl SystemContainer {
}
}

impl Debug for SystemContainer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "\n{{{:?}}}", &self.system())
targrub marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl GraphNode for SystemContainer {
type Label = SystemLabelId;

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ecs/src/schedule/system_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
/// .with_system(do_the_other_thing.after(Something))
/// .with_system(do_something_else.at_end());
/// ```
#[derive(Debug)]
pub struct SystemDescriptor {
pub(crate) system: BoxedSystem<(), ()>,
pub(crate) exclusive_insertion_point: Option<ExclusiveInsertionPoint>,
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ pub struct Query<'world, 'state, Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
pub(crate) change_tick: u32,
}

impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> std::fmt::Debug for Query<'w, 's, Q, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Query {{ matched entities: {}, world: {:?}, state: {:?}, last_change_tick: {}, change_tick: {} }}", self.iter().count(), self.world, self.state, self.last_change_tick, self.change_tick)
}
}

impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
/// Creates a new query.
///
Expand Down
19 changes: 19 additions & 0 deletions crates/bevy_ecs/src/system/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bevy_utils::tracing::warn;
use core::fmt::Debug;

use crate::{
archetype::ArchetypeComponentId, change_detection::MAX_CHANGE_AGE, component::ComponentId,
Expand Down Expand Up @@ -95,3 +96,21 @@ pub(crate) fn check_system_change_tick(
*last_change_tick = change_tick.wrapping_sub(MAX_CHANGE_AGE);
}
}

impl Debug for dyn System<In = (), Out = ()> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {{{}}}", self.name(), {
if self.is_send() {
if self.is_exclusive() {
"is_send is_exclusive"
} else {
"is_send"
}
} else if self.is_exclusive() {
"is_exclusive"
} else {
""
}
},)
}
}