@@ -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]
6264macro_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