๐Ÿ“ฆ bevyengine / bevy

๐Ÿ“„ helpers.rs ยท 193 lines
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193use crate::{
    enums::{Enum, VariantType},
    utility::reflect_hasher,
    PartialReflect, ReflectRef,
};
use core::{
    fmt::Debug,
    hash::{Hash, Hasher},
};

/// Returns the `u64` hash of the given [enum](Enum).
#[inline(never)]
pub fn enum_hash(value: &dyn Enum) -> Option<u64> {
    let mut hasher = reflect_hasher();
    core::any::Any::type_id(value).hash(&mut hasher);
    value.variant_name().hash(&mut hasher);
    value.variant_type().hash(&mut hasher);
    for field in value.iter_fields() {
        hasher.write_u64(field.value().reflect_hash()?);
    }
    Some(hasher.finish())
}

/// Compares an [`Enum`] with a [`PartialReflect`] value.
///
/// Returns true if and only if all of the following are true:
/// - `b` is an enum;
/// - `b` is the same variant as `a`;
/// - For each field in `a`, `b` contains a field with the same name and
///   [`PartialReflect::reflect_partial_eq`] returns `Some(true)` for the two field
///   values.
#[inline(never)]
pub fn enum_partial_eq(a: &dyn Enum, b: &dyn PartialReflect) -> Option<bool> {
    // Both enums?
    let ReflectRef::Enum(b) = b.reflect_ref() else {
        return Some(false);
    };

    // Same variant name?
    if a.variant_name() != b.variant_name() {
        return Some(false);
    }

    // Same variant type?
    if !a.is_variant(b.variant_type()) {
        return Some(false);
    }

    match a.variant_type() {
        VariantType::Struct => {
            if a.field_len() != b.field_len() {
                return Some(false);
            }
            // Same struct fields?
            for field in a.iter_fields() {
                let field_name = field.name().unwrap();
                if let Some(field_value) = b.field(field_name) {
                    if let Some(false) | None = field_value.reflect_partial_eq(field.value()) {
                        // Fields failed comparison
                        return Some(false);
                    }
                } else {
                    // Field does not exist
                    return Some(false);
                }
            }
            Some(true)
        }
        VariantType::Tuple => {
            if a.field_len() != b.field_len() {
                return Some(false);
            }
            // Same tuple fields?
            for (i, field) in a.iter_fields().enumerate() {
                if let Some(field_value) = b.field_at(i) {
                    if let Some(false) | None = field_value.reflect_partial_eq(field.value()) {
                        // Fields failed comparison
                        return Some(false);
                    }
                } else {
                    // Field does not exist
                    return Some(false);
                }
            }
            Some(true)
        }
        _ => Some(true),
    }
}

/// Compares two [`Enum`] values (by variant) and returns their ordering.
///
/// Returns [`None`] if the comparison couldn't be performed (e.g., kinds mismatch
/// or an element comparison returns `None`).
///
/// The ordering is same with `derive` macro. First order by variant index, then by fields.
#[inline(never)]
pub fn enum_partial_cmp(a: &dyn Enum, b: &dyn PartialReflect) -> Option<::core::cmp::Ordering> {
    // Both enums?
    let ReflectRef::Enum(b) = b.reflect_ref() else {
        return None;
    };

    // Same variant name?
    if a.variant_name() != b.variant_name() {
        // Different variant names.
        // Ordering by variant index here can result in inconsistencies with
        // partial_eq when comparing between two different concrete enums,
        // so we simply return None here
        return None;
    }

    // Same variant type?
    if !a.is_variant(b.variant_type()) {
        return None;
    }

    match a.variant_type() {
        VariantType::Struct => {
            if a.field_len() != b.field_len() {
                return None;
            }
            crate::structs::partial_cmp_by_field_names(
                a.field_len(),
                |i| a.name_at(i),
                |i| a.field_at(i),
                |i| b.name_at(i),
                |i| b.field_at(i),
                |name| b.field(name),
            )
        }
        VariantType::Tuple => {
            if a.field_len() != b.field_len() {
                return None;
            }
            for (i, field) in a.iter_fields().enumerate() {
                if let Some(field_value) = b.field_at(i) {
                    match field.value().reflect_partial_cmp(field_value) {
                        None => return None,
                        Some(core::cmp::Ordering::Equal) => continue,
                        Some(ord) => return Some(ord),
                    }
                }
                return None;
            }
            Some(core::cmp::Ordering::Equal)
        }
        _ => Some(core::cmp::Ordering::Equal),
    }
}

/// The default debug formatter for [`Enum`] types.
///
/// # Example
/// ```
/// use bevy_reflect::Reflect;
/// #[derive(Reflect)]
/// enum MyEnum {
///   A,
///   B (usize),
///   C {value: i32}
/// }
///
/// let my_enum: &dyn Reflect = &MyEnum::B(123);
/// println!("{:#?}", my_enum);
///
/// // Output:
///
/// // B (
/// //   123,
/// // )
/// ```
#[inline]
pub fn enum_debug(dyn_enum: &dyn Enum, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match dyn_enum.variant_type() {
        VariantType::Unit => f.write_str(dyn_enum.variant_name()),
        VariantType::Tuple => {
            let mut debug = f.debug_tuple(dyn_enum.variant_name());
            for field in dyn_enum.iter_fields() {
                debug.field(&field.value() as &dyn Debug);
            }
            debug.finish()
        }
        VariantType::Struct => {
            let mut debug = f.debug_struct(dyn_enum.variant_name());
            for field in dyn_enum.iter_fields() {
                debug.field(field.name().unwrap(), &field.value() as &dyn Debug);
            }
            debug.finish()
        }
    }
}