1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
pub use bevy_reflect_derive::TypeUuid;
pub use bevy_utils::Uuid;
pub trait TypeUuid {
const TYPE_UUID: Uuid;
}
pub trait TypeUuidDynamic {
fn type_uuid(&self) -> Uuid;
fn type_name(&self) -> &'static str;
}
impl<T> TypeUuidDynamic for T
where
T: TypeUuid,
{
fn type_uuid(&self) -> Uuid {
Self::TYPE_UUID
}
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate as bevy_reflect;
use bevy_reflect_derive::TypeUuid;
use std::marker::PhantomData;
#[derive(TypeUuid)]
#[uuid = "af6466c2-a9f4-11eb-bcbc-0242ac130002"]
struct TestDeriveStruct<T>
where
T: Clone,
{
_value: T,
}
fn test_impl_type_uuid(_: &impl TypeUuid) {}
#[test]
fn test_generic_type_uuid_derive() {
#[derive(TypeUuid, Clone)]
#[uuid = "ebb16cc9-4d5a-453c-aa8c-c72bd8ec83a2"]
struct T;
let test_struct = TestDeriveStruct { _value: T };
test_impl_type_uuid(&test_struct);
}
#[test]
fn test_generic_type_unique_uuid() {
#[derive(TypeUuid, Clone)]
#[uuid = "49951b1c-4811-45e7-acc6-3119249fbd8f"]
struct A;
#[derive(TypeUuid, Clone)]
#[uuid = "4882b8f5-5556-4cee-bea6-a2e5991997b7"]
struct B;
let uuid_a = TestDeriveStruct::<A>::TYPE_UUID;
let uuid_b = TestDeriveStruct::<B>::TYPE_UUID;
assert_ne!(uuid_a, uuid_b);
assert_ne!(uuid_a, A::TYPE_UUID);
assert_ne!(uuid_b, B::TYPE_UUID);
}
#[test]
fn test_inverted_generic_type_unique_uuid() {
#[derive(TypeUuid, Clone)]
#[uuid = "49951b1c-4811-45e7-acc6-3119249fbd8f"]
struct Inner;
#[derive(TypeUuid, Clone)]
#[uuid = "23ebc0c3-ef69-4ea0-8c2a-dca1b4e27c0d"]
struct TestDeriveStructA<T>
where
T: Clone,
{
_phantom: PhantomData<T>,
}
#[derive(TypeUuid, Clone)]
#[uuid = "a82f9936-70cb-482a-bd3d-cb99d87de55f"]
struct TestDeriveStructB<T>
where
T: Clone,
{
_phantom: PhantomData<T>,
}
let uuid_ab = TestDeriveStructA::<TestDeriveStructB<Inner>>::TYPE_UUID;
let uuid_ba = TestDeriveStructB::<TestDeriveStructA<Inner>>::TYPE_UUID;
assert_ne!(uuid_ab, uuid_ba);
assert_ne!(uuid_ab, TestDeriveStructA::<Inner>::TYPE_UUID);
assert_ne!(uuid_ba, TestDeriveStructB::<Inner>::TYPE_UUID);
}
#[test]
fn test_generic_type_uuid_same_for_eq_param() {
#[derive(TypeUuid, Clone)]
#[uuid = "49951b1c-4811-45e7-acc6-3119249fbd8f"]
struct A;
#[derive(TypeUuid, Clone)]
#[uuid = "49951b1c-4811-45e7-acc6-3119249fbd8f"]
struct BButSameAsA;
let uuid_a = TestDeriveStruct::<A>::TYPE_UUID;
let uuid_b = TestDeriveStruct::<BButSameAsA>::TYPE_UUID;
assert_eq!(uuid_a, uuid_b);
}
#[test]
fn test_multiple_generic_uuid() {
#[derive(TypeUuid)]
#[uuid = "35c8a7d3-d4b3-4bd7-b847-1118dc78092f"]
struct TestGeneric<A, B> {
_value_a: A,
_value_b: B,
}
assert_ne!(
TestGeneric::<f32, bool>::TYPE_UUID,
TestGeneric::<bool, f32>::TYPE_UUID
);
}
#[test]
fn test_primitive_generic_uuid() {
test_impl_type_uuid(&true);
test_impl_type_uuid(&Some(true));
test_impl_type_uuid(&TestDeriveStruct::<bool> { _value: true });
assert_ne!(Option::<bool>::TYPE_UUID, Option::<f32>::TYPE_UUID);
assert_ne!(<[bool; 0]>::TYPE_UUID, <[bool; 1]>::TYPE_UUID);
assert_ne!(<[bool; 0]>::TYPE_UUID, <[f32; 0]>::TYPE_UUID);
assert_ne!(
<(bool, bool)>::TYPE_UUID,
<(bool, bool, bool, bool)>::TYPE_UUID
);
assert_ne!(<(bool, f32)>::TYPE_UUID, <(f32, bool)>::TYPE_UUID);
}
}