Skip to content

Commit 85448b7

Browse files
authored
Make DebugName work when building with no default features (#19824)
# Objective Let `bevy_utils` build with no default features. `cargo build -p bevy_utils --no-default-features` currently fails with ``` error[E0433]: failed to resolve: use of unresolved module or unlinked crate `alloc` --> crates\bevy_utils\src\debug_info.rs:1:5 | 1 | use alloc::{borrow::Cow, fmt, string::String}; | ^^^^^ use of unresolved module or unlinked crate `alloc` | = help: add `extern crate alloc` to use the `alloc` crate error[E0432]: unresolved import `alloc` --> crates\bevy_utils\src\debug_info.rs:1:5 | 1 | use alloc::{borrow::Cow, fmt, string::String}; | ^^^^^ help: a similar path exists: `core::alloc` ``` I would have expected CI to catch this earlier, but I have not investigated why it did not. ## Solution Wrap the parts of `DebugName` that use `Cow` and `String` in `cfg::alloc!`. If the `debug` feature is enabled, then `DebugName` itself stores a `Cow`, so make the `debug` feature require `bevy_platform/alloc`. That is, you can use `DebugName` in no-std contexts when it's just a ZST! (I bet it's even possible to support no-std `debug` by storing `&'static str` instead of `Cow<'static, str>`, but that seemed like too much complexity for now.)
1 parent 57e58ef commit 85448b7

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

crates/bevy_utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ parallel = ["bevy_platform/std", "dep:thread_local"]
1818

1919
std = ["disqualified/alloc"]
2020

21-
debug = []
21+
debug = ["bevy_platform/alloc"]
2222

2323
[dependencies]
2424
bevy_platform = { path = "../bevy_platform", version = "0.17.0-dev", default-features = false }

crates/bevy_utils/src/debug_info.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use alloc::{borrow::Cow, fmt, string::String};
1+
use crate::cfg;
2+
cfg::alloc! {
3+
use alloc::{borrow::Cow, fmt, string::String};
4+
}
25
#[cfg(feature = "debug")]
36
use core::any::type_name;
47
use disqualified::ShortName;
@@ -16,14 +19,16 @@ pub struct DebugName {
1619
name: Cow<'static, str>,
1720
}
1821

19-
impl fmt::Display for DebugName {
20-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21-
#[cfg(feature = "debug")]
22-
f.write_str(self.name.as_ref())?;
23-
#[cfg(not(feature = "debug"))]
24-
f.write_str(FEATURE_DISABLED)?;
22+
cfg::alloc! {
23+
impl fmt::Display for DebugName {
24+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25+
#[cfg(feature = "debug")]
26+
f.write_str(self.name.as_ref())?;
27+
#[cfg(not(feature = "debug"))]
28+
f.write_str(FEATURE_DISABLED)?;
2529

26-
Ok(())
30+
Ok(())
31+
}
2732
}
2833
}
2934

@@ -39,14 +44,16 @@ impl DebugName {
3944
}
4045
}
4146

42-
/// Create a new `DebugName` from a `String`
43-
///
44-
/// The value will be ignored if the `debug` feature is not enabled
45-
#[cfg_attr(not(feature = "debug"), expect(unused_variables))]
46-
pub fn owned(value: String) -> Self {
47-
DebugName {
48-
#[cfg(feature = "debug")]
49-
name: Cow::Owned(value),
47+
cfg::alloc! {
48+
/// Create a new `DebugName` from a `String`
49+
///
50+
/// The value will be ignored if the `debug` feature is not enabled
51+
#[cfg_attr(not(feature = "debug"), expect(unused_variables))]
52+
pub fn owned(value: String) -> Self {
53+
DebugName {
54+
#[cfg(feature = "debug")]
55+
name: Cow::Owned(value),
56+
}
5057
}
5158
}
5259

@@ -79,19 +86,21 @@ impl DebugName {
7986
}
8087
}
8188

82-
impl From<Cow<'static, str>> for DebugName {
83-
#[cfg_attr(not(feature = "debug"), expect(unused_variables))]
84-
fn from(value: Cow<'static, str>) -> Self {
85-
Self {
86-
#[cfg(feature = "debug")]
87-
name: value,
89+
cfg::alloc! {
90+
impl From<Cow<'static, str>> for DebugName {
91+
#[cfg_attr(not(feature = "debug"), expect(unused_variables))]
92+
fn from(value: Cow<'static, str>) -> Self {
93+
Self {
94+
#[cfg(feature = "debug")]
95+
name: value,
96+
}
8897
}
8998
}
90-
}
9199

92-
impl From<String> for DebugName {
93-
fn from(value: String) -> Self {
94-
Self::owned(value)
100+
impl From<String> for DebugName {
101+
fn from(value: String) -> Self {
102+
Self::owned(value)
103+
}
95104
}
96105
}
97106

0 commit comments

Comments
 (0)