|
1 |
| -use std::marker::PhantomData; |
| 1 | +use std::{fmt::Debug, hash::Hash, marker::PhantomData}; |
2 | 2 |
|
3 | 3 | use bevy_ecs::prelude::*;
|
4 | 4 |
|
@@ -30,6 +30,28 @@ fn main() {
|
30 | 30 | format!("{:?}", ConstGenericLabel::<21>.as_label()),
|
31 | 31 | "ConstGenericLabel::<21>"
|
32 | 32 | );
|
| 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); |
33 | 55 | }
|
34 | 56 |
|
35 | 57 | #[derive(SystemLabel)]
|
@@ -75,3 +97,13 @@ pub struct BadLabel2 {
|
75 | 97 | #[system_label(ignore_fields)]
|
76 | 98 | x: (),
|
77 | 99 | }*/
|
| 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