Skip to main content

compact_str/features/
bevy_reflect.rs

1use crate::CompactString;
2use bevy_reflect::{
3    impl_reflect_opaque, std_traits::ReflectDefault, ReflectDeserialize, ReflectSerialize,
4};
5
6impl_reflect_opaque!((in crate::CompactString)CompactString(
7    Clone,
8    Debug,
9    Hash,
10    PartialEq,
11    Default,
12    Serialize,
13    Deserialize,
14));
15
16#[cfg(test)]
17mod tests {
18    use crate::CompactString;
19    use bevy_reflect::{FromReflect, PartialReflect, Reflect};
20
21    #[derive(Debug, Reflect, Eq, PartialEq)]
22    struct MyTestComponentStruct {
23        pub value: CompactString,
24    }
25    #[derive(Debug, Reflect, Eq, PartialEq)]
26    struct MyTestComponentTuple(pub CompactString);
27
28    #[test]
29    fn should_partial_eq_compactstring() {
30        let a: &dyn PartialReflect = &CompactString::new("A");
31        let a2: &dyn PartialReflect = &CompactString::new("A");
32        let b: &dyn PartialReflect = &CompactString::new("B");
33        assert_eq!(Some(true), a.reflect_partial_eq(a2));
34        assert_eq!(Some(false), a.reflect_partial_eq(b));
35    }
36
37    #[test]
38    fn compactstring_should_from_reflect() {
39        let string = CompactString::new("hello_world.rs");
40        let output = <CompactString as FromReflect>::from_reflect(&string);
41        assert_eq!(Some(string), output);
42    }
43
44    #[test]
45    fn compactstring_heap_should_from_reflect() {
46        let string = CompactString::new("abc".repeat(100));
47        let output = <CompactString as FromReflect>::from_reflect(&string);
48        assert_eq!(Some(string), output);
49    }
50
51    #[test]
52    fn struct_with_compactstring_should_from_reflect() {
53        let string = CompactString::new("hello_world.rs");
54        let my_struct = MyTestComponentStruct { value: string };
55        let output = <MyTestComponentStruct as FromReflect>::from_reflect(&my_struct);
56        assert_eq!(Some(my_struct), output);
57    }
58
59    #[test]
60    fn tuple_with_compactstring_should_from_reflect() {
61        let string = CompactString::new("hello_world.rs");
62        let my_struct = MyTestComponentTuple(string);
63        let output = <MyTestComponentTuple as FromReflect>::from_reflect(&my_struct);
64        assert_eq!(Some(my_struct), output);
65    }
66}