Skip to content

Commit 3ade879

Browse files
committed
Update label derive example
1 parent d75abf4 commit 3ade879

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

crates/bevy_ecs/examples/derive_label.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::marker::PhantomData;
1+
use std::{fmt::Debug, hash::Hash, marker::PhantomData};
22

33
use bevy_ecs::prelude::*;
44

@@ -30,6 +30,28 @@ fn main() {
3030
format!("{:?}", ConstGenericLabel::<21>.as_label()),
3131
"ConstGenericLabel::<21>"
3232
);
33+
34+
// Working with labels that need to be heap allocated.
35+
let label = ComplexLabel {
36+
people: vec!["John", "William", "Sharon"],
37+
};
38+
// Convert it to a LabelId. Its type gets erased.
39+
let id = label.as_label();
40+
assert_eq!(
41+
format!("{id:?}"),
42+
r#"ComplexLabel { people: ["John", "William", "Sharon"] }"#
43+
);
44+
45+
// Generic heap-allocated labels.
46+
let id = WrapLabel(1_i128).as_label();
47+
assert_eq!(format!("{id:?}"), "WrapLabel(1)");
48+
49+
// Different types with the same type constructor.
50+
let id2 = WrapLabel(1_u32).as_label();
51+
// The debug representations are the same...
52+
assert_eq!(format!("{id:?}"), format!("{id2:?}"));
53+
// ...but they do not compare equal.
54+
assert_ne!(id, id2);
3355
}
3456

3557
#[derive(SystemLabel)]
@@ -75,3 +97,13 @@ pub struct BadLabel2 {
7597
#[system_label(ignore_fields)]
7698
x: (),
7799
}*/
100+
101+
#[derive(Debug, Clone, PartialEq, Eq, Hash, SystemLabel)]
102+
#[system_label(intern)]
103+
pub struct ComplexLabel {
104+
people: Vec<&'static str>,
105+
}
106+
107+
#[derive(Debug, Clone, PartialEq, Eq, Hash, SystemLabel)]
108+
#[system_label(intern)]
109+
pub struct WrapLabel<T>(T);

0 commit comments

Comments
 (0)