Skip to content

Commit

Permalink
Add no_std support to bevy_hierarchy (bevyengine#16998)
Browse files Browse the repository at this point in the history
# Objective

- Contributes to bevyengine#15460

## Solution

- Added the following features:
  - `std` (default)

## Testing

- CI

## Notes

- There was a minor issue with `bevy_reflect`'s `smallvec` feature
noticed in this PR which I have also resolved here. I can split this out
if desired, but I've left it here for now as it's a very small change
and I don't consider this PR itself to be very controversial.
  • Loading branch information
bushrat011899 authored and ecoskey committed Jan 6, 2025
1 parent 9ab2f0e commit 9fa9ba4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
51 changes: 41 additions & 10 deletions crates/bevy_hierarchy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,54 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
default = ["bevy_app"]
trace = []
bevy_app = ["reflect", "dep:bevy_app"]
reflect = ["bevy_ecs/bevy_reflect", "bevy_reflect"]
default = ["std", "bevy_app", "reflect"]

# Functionality

## Adds integration with the `bevy_app` plugin API.
bevy_app = ["dep:bevy_app"]

## Adds runtime reflection support using `bevy_reflect`.
reflect = ["bevy_ecs/bevy_reflect", "bevy_reflect", "bevy_app?/bevy_reflect"]

# Debugging Features

## Enables `tracing` integration, allowing spans and other metrics to be reported
## through that framework.
trace = ["dep:tracing"]

# Platform Compatibility

## Allows access to the `std` crate. Enabling this feature will prevent compilation
## on `no_std` targets, but provides access to certain additional features on
## supported platforms.
std = [
"bevy_app?/std",
"bevy_ecs/std",
"bevy_reflect/std",
"bevy_utils/std",
"disqualified/alloc",
]

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.15.0-dev", optional = true }
bevy_app = { path = "../bevy_app", version = "0.15.0-dev", default-features = false, optional = true }
bevy_ecs = { path = "../bevy_ecs", version = "0.15.0-dev", default-features = false }
bevy_reflect = { path = "../bevy_reflect", version = "0.15.0-dev", features = [
"bevy",
"smallvec",
], optional = true }
bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev" }
disqualified = "1.0"
], default-features = false, optional = true }
bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev", default-features = false, features = [
"alloc",
] }
disqualified = { version = "1.0", default-features = false }

smallvec = { version = "1.11", features = ["union", "const_generics"] }
# other
smallvec = { version = "1.11", default-features = false, features = [
"union",
"const_generics",
] }
tracing = { version = "0.1", default-features = false, optional = true }
log = { version = "0.4", default-features = false }

[lints]
workspace = true
Expand Down
26 changes: 13 additions & 13 deletions crates/bevy_hierarchy/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_ecs::{
system::EntityCommands,
world::{Command, DeferredWorld, EntityWorldMut, World},
};
use bevy_utils::tracing::debug;
use log::debug;

/// Despawns the given entity and all its children recursively
#[derive(Debug)]
Expand Down Expand Up @@ -69,11 +69,11 @@ fn despawn_children_recursive(world: &mut World, entity: Entity, warn: bool) {
impl Command for DespawnRecursive {
fn apply(self, world: &mut World) {
#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!(
let _span = tracing::info_span!(
"command",
name = "DespawnRecursive",
entity = bevy_utils::tracing::field::debug(self.entity),
warn = bevy_utils::tracing::field::debug(self.warn)
entity = tracing::field::debug(self.entity),
warn = tracing::field::debug(self.warn)
)
.entered();
despawn_with_children_recursive(world, self.entity, self.warn);
Expand All @@ -83,11 +83,11 @@ impl Command for DespawnRecursive {
impl Command for DespawnChildrenRecursive {
fn apply(self, world: &mut World) {
#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!(
let _span = tracing::info_span!(
"command",
name = "DespawnChildrenRecursive",
entity = bevy_utils::tracing::field::debug(self.entity),
warn = bevy_utils::tracing::field::debug(self.warn)
entity = tracing::field::debug(self.entity),
warn = tracing::field::debug(self.warn)
)
.entered();

Expand Down Expand Up @@ -150,10 +150,10 @@ fn despawn_recursive_inner(world: EntityWorldMut, warn: bool) {
let entity = world.id();

#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!(
let _span = tracing::info_span!(
"despawn_recursive",
entity = bevy_utils::tracing::field::debug(entity),
warn = bevy_utils::tracing::field::debug(warn)
entity = tracing::field::debug(entity),
warn = tracing::field::debug(warn)
)
.entered();

Expand All @@ -167,10 +167,10 @@ fn despawn_descendants_inner<'v, 'w>(
let entity = world.id();

#[cfg(feature = "trace")]
let _span = bevy_utils::tracing::info_span!(
let _span = tracing::info_span!(
"despawn_descendants",
entity = bevy_utils::tracing::field::debug(entity),
warn = bevy_utils::tracing::field::debug(warn)
entity = tracing::field::debug(entity),
warn = tracing::field::debug(warn)
)
.entered();

Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_hierarchy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
html_logo_url = "https://bevyengine.org/assets/icon.png",
html_favicon_url = "https://bevyengine.org/assets/icon.png"
)]
#![cfg_attr(not(feature = "std"), no_std)]

//! Parent-child relationships for Bevy entities.
//!
Expand Down Expand Up @@ -98,8 +99,9 @@ pub struct HierarchyPlugin;
#[cfg(feature = "bevy_app")]
impl Plugin for HierarchyPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Children>()
.register_type::<Parent>()
.add_event::<HierarchyEvent>();
#[cfg(feature = "reflect")]
app.register_type::<Children>().register_type::<Parent>();

app.add_event::<HierarchyEvent>();
}
}
4 changes: 2 additions & 2 deletions crates/bevy_hierarchy/src/valid_parent_check_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::marker::PhantomData;
use bevy_ecs::prelude::*;

#[cfg(feature = "bevy_app")]
use {crate::Parent, bevy_utils::HashSet, disqualified::ShortName};
use {crate::Parent, alloc::format, bevy_utils::HashSet, disqualified::ShortName};

/// When enabled, runs [`check_hierarchy_component_has_valid_parent<T>`].
///
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn check_hierarchy_component_has_valid_parent<T: Component>(
let parent = parent.get();
if !component_query.contains(parent) && !already_diagnosed.contains(&entity) {
already_diagnosed.insert(entity);
bevy_utils::tracing::warn!(
log::warn!(
"warning[B0004]: {name} with the {ty_name} component has a parent without {ty_name}.\n\
This will cause inconsistent behaviors! See: https://bevyengine.org/learn/errors/b0004",
ty_name = ShortName::of::<T>(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::any::Any;
use smallvec::{Array as SmallArray, SmallVec};

#[cfg(not(feature = "std"))]
use alloc::{format, vec};
use alloc::{format, vec, vec::Vec};

use crate::{
self as bevy_reflect, utility::GenericTypeInfoCell, ApplyError, FromReflect, FromType,
Expand Down
8 changes: 8 additions & 0 deletions tools/ci/src/commands/compile_check_no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ impl Prepare for CompileCheckNoStdCommand {
"Please fix compiler errors in output above for bevy_app no_std compatibility.",
));

commands.push(PreparedCommand::new::<Self>(
cmd!(
sh,
"cargo check -p bevy_hierarchy --no-default-features --features bevy_app,reflect --target {target}"
),
"Please fix compiler errors in output above for bevy_hierarchy no_std compatibility.",
));

commands
}
}

0 comments on commit 9fa9ba4

Please sign in to comment.