Skip to content

Commit 02cb1a8

Browse files
joseph-giojames7132
authored andcommitted
improve documentation for macro-generated label types (bevyengine#5367)
# Objective I noticed while working on bevyengine#5366 that the documentation for label types wasn't working correctly. Having experimented with this for a few weeks, I believe that generating docs in macros is more effort than it's worth. ## Solution Add more boilerplate, copy-paste and edit the docs across types. This also lets us add custom doctests for specific types. Also, we don't need `concat_idents` as a dependency anymore.
1 parent 312adb7 commit 02cb1a8

File tree

4 files changed

+82
-55
lines changed

4 files changed

+82
-55
lines changed

crates/bevy_app/src/app.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ use std::fmt::Debug;
1515

1616
#[cfg(feature = "trace")]
1717
use bevy_utils::tracing::info_span;
18-
bevy_utils::define_label!(AppLabel);
18+
bevy_utils::define_label!(
19+
/// A strongly-typed class of labels used to identify an [`App`].
20+
AppLabel,
21+
/// A strongly-typed identifier for an [`AppLabel`].
22+
AppLabelId,
23+
);
1924

2025
#[allow(clippy::needless_doctest_main)]
2126
/// A container of app logic and data.

crates/bevy_ecs/src/schedule/label.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
pub use bevy_ecs_macros::{AmbiguitySetLabel, RunCriteriaLabel, StageLabel, SystemLabel};
22
use bevy_utils::define_label;
33

4-
define_label!(StageLabel);
5-
define_label!(SystemLabel);
6-
define_label!(AmbiguitySetLabel);
7-
define_label!(RunCriteriaLabel);
4+
define_label!(
5+
/// A strongly-typed class of labels used to identify [`Stage`](crate::schedule::Stage)s.
6+
StageLabel,
7+
/// Strongly-typed identifier for a [`StageLabel`].
8+
StageLabelId,
9+
);
10+
define_label!(
11+
/// A strongly-typed class of labels used to identify [`System`](crate::system::System)s.
12+
SystemLabel,
13+
/// Strongly-typed identifier for a [`SystemLabel`].
14+
SystemLabelId,
15+
);
16+
define_label!(
17+
/// A strongly-typed class of labels used to identify sets of systems with intentionally ambiguous execution order.
18+
AmbiguitySetLabel,
19+
/// Strongly-typed identifier for an [`AmbiguitySetLabel`].
20+
AmbiguitySetLabelId,
21+
);
22+
define_label!(
23+
/// A strongly-typed class of labels used to identify [run criteria](crate::schedule::RunCriteria).
24+
RunCriteriaLabel,
25+
/// Strongly-typed identifier for a [`RunCriteriaLabel`].
26+
RunCriteriaLabelId,
27+
);

crates/bevy_utils/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ tracing = { version = "0.1", default-features = false, features = ["std"] }
1414
instant = { version = "0.1", features = ["wasm-bindgen"] }
1515
uuid = { version = "1.1", features = ["v4", "serde"] }
1616
hashbrown = { version = "0.12", features = ["serde"] }
17-
concat-idents = "1"
1817

1918
[target.'cfg(target_arch = "wasm32")'.dependencies]
2019
getrandom = {version = "0.2.0", features = ["js"]}

crates/bevy_utils/src/label.rs

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,70 +47,73 @@ where
4747
}
4848
}
4949

50-
#[doc(hidden)]
51-
pub use concat_idents::concat_idents;
52-
5350
/// Macro to define a new label trait
5451
///
5552
/// # Example
5653
///
5754
/// ```
5855
/// # use bevy_utils::define_label;
59-
/// define_label!(MyNewLabelTrait);
56+
/// define_label!(
57+
/// /// A class of labels.
58+
/// MyNewLabelTrait,
59+
/// /// Identifies a value that implements `MyNewLabelTrait`.
60+
/// MyNewLabelId,
61+
/// );
6062
/// ```
6163
#[macro_export]
6264
macro_rules! define_label {
63-
($label_name:ident) => {
64-
$crate::label::concat_idents!(id_name = $label_name, Id {
65-
66-
/// Stores one of a set of strongly-typed labels for a class of objects.
67-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
68-
pub struct id_name(::core::any::TypeId, &'static str);
69-
70-
impl ::core::fmt::Debug for id_name {
71-
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
72-
write!(f, "{}", self.1)
73-
}
65+
(
66+
$(#[$label_attr:meta])*
67+
$label_name:ident,
68+
69+
$(#[$id_attr:meta])*
70+
$id_name:ident $(,)?
71+
) => {
72+
$(#[$id_attr])*
73+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
74+
pub struct $id_name(::core::any::TypeId, &'static str);
75+
76+
impl ::core::fmt::Debug for $id_name {
77+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
78+
write!(f, "{}", self.1)
7479
}
80+
}
7581

76-
/// Types that can be converted to a(n) [`id_name`].
77-
///
78-
/// Check the docs for [`define_label`](bevy_ecs::define_label) for more info.
79-
pub trait $label_name: 'static {
80-
/// Converts this type into an opaque, strongly-typed label.
81-
fn as_label(&self) -> id_name {
82-
let id = self.type_id();
83-
let label = self.as_str();
84-
id_name(id, label)
85-
}
86-
/// Returns the [`TypeId`] used to differentiate labels.
87-
fn type_id(&self) -> ::core::any::TypeId {
88-
::core::any::TypeId::of::<Self>()
89-
}
90-
/// Returns the representation of this label as a string literal.
91-
///
92-
/// In cases where you absolutely need a label to be determined at runtime,
93-
/// you can use [`Box::leak`] to get a `'static` reference.
94-
fn as_str(&self) -> &'static str;
82+
$(#[$label_attr])*
83+
pub trait $label_name: 'static {
84+
/// Converts this type into an opaque, strongly-typed label.
85+
fn as_label(&self) -> $id_name {
86+
let id = self.type_id();
87+
let label = self.as_str();
88+
$id_name(id, label)
89+
}
90+
/// Returns the [`TypeId`] used to differentiate labels.
91+
fn type_id(&self) -> ::core::any::TypeId {
92+
::core::any::TypeId::of::<Self>()
9593
}
94+
/// Returns the representation of this label as a string literal.
95+
///
96+
/// In cases where you absolutely need a label to be determined at runtime,
97+
/// you can use [`Box::leak`] to get a `'static` reference.
98+
fn as_str(&self) -> &'static str;
99+
}
96100

97-
impl $label_name for id_name {
98-
fn as_label(&self) -> Self {
99-
*self
100-
}
101-
fn type_id(&self) -> ::core::any::TypeId {
102-
self.0
103-
}
104-
fn as_str(&self) -> &'static str {
105-
self.1
106-
}
101+
impl $label_name for $id_name {
102+
fn as_label(&self) -> Self {
103+
*self
104+
}
105+
fn type_id(&self) -> ::core::any::TypeId {
106+
self.0
107107
}
108+
fn as_str(&self) -> &'static str {
109+
self.1
110+
}
111+
}
108112

109-
impl $label_name for &'static str {
110-
fn as_str(&self) -> Self {
111-
self
112-
}
113+
impl $label_name for &'static str {
114+
fn as_str(&self) -> Self {
115+
self
113116
}
114-
});
117+
}
115118
};
116119
}

0 commit comments

Comments
 (0)