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
pub use bevy_reflect_derive::TypeUuid;
pub use bevy_utils::Uuid;

/// A trait for types with a statically associated UUID.
pub trait TypeUuid {
    const TYPE_UUID: Uuid;
}

/// A trait for types with an associated UUID.
pub trait TypeUuidDynamic {
    fn type_uuid(&self) -> Uuid;
    fn type_name(&self) -> &'static str;
}

impl<T> TypeUuidDynamic for T
where
    T: TypeUuid,
{
    /// Returns the UUID associated with this value's type.
    fn type_uuid(&self) -> Uuid {
        Self::TYPE_UUID
    }

    /// Returns the [type name] of this value's type.
    ///
    /// [type name]: std::any::type_name
    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);
    }
}