bevy_reflect 0.20.0

Dynamically interact with Rust types
Documentation
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
//! Code for displaying reflected values in a consistent, human-readable format.
//!
//! Primarily useful for interactive workflows,
//! such as inspecting reflected values in a text-based format,
//! or logging them to the console.
//! Because the output is pure text, it is particularly useful for automated inspection,
//! println!-debugging, and as output for screen readers.
//!
//! Be mindful: these [`Display`] implementations return the full type path for each contained type.
//! While useful for detailed inspection, it can be unhelpfully verbose, especially for nested values.
//! To recursively collapse all contained type paths to their "short names" (i.e. with no crate/module paths),
//! use [`disqualified::ShortName::from`] on the returned [`String`](alloc::string::String),
//! then use the [`Display`] implementation on [`ShortName`](disqualified::ShortName) for more concise output.
//!
//! These implementations are stored in their own module rather than beside the types themselves
//! to help ensure consistency and reduce clutter.
//!
//! Note that these implementations are for the trait objects (`dyn Struct`, etc.) rather than for every `T: Struct`.
//! This is deliberate: it reduces the compile time and binary costs
//! associated with generating this code for every type that implements `Reflect`.
//! To use them with a concrete type, cast your value to a trait object before formatting it:
//! e.g. `format!("{}", &my_value as &dyn Reflect)`.

use alloc::vec::Vec;
use core::any::TypeId;
use core::fmt::{Display, Formatter, Write};

#[cfg(feature = "functions")]
use crate::func::Function;
use crate::{
    array::Array,
    enums::{Enum, VariantType},
    list::List,
    map::Map,
    set::Set,
    structs::Struct,
    tuple::Tuple,
    tuple_struct::TupleStruct,
    PartialReflect, Reflect, ReflectRef, TypeInfo,
};

/// String-formats a reflected value, detecting cycles via their [`TypeId`].
///
/// If `value`'s [`TypeId`] already appears in `ancestry`, the output is
/// truncated to `{type_path} { ... }` to avoid infinite recursion.
///
/// This catches both direct recursion (a type containing itself)
/// and mutual recursion (type A containing type B containing type A).
///
/// Dynamic types without type info cannot be cycle-checked.
///
/// `indent` is the indentation level at which this value's closing delimiter
/// (or last continuation line for opaque values) should appear.
fn write_value(
    f: &mut Formatter<'_>,
    value: &dyn PartialReflect,
    // A HashSet / BTreeSet has better asymptotic performance for the .contains() check,
    // but a Vec will be faster at low n, which is the common case for reflected values,
    // as most types are not deeply nested.
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    // Dynamic types without type info cannot be cycle-checked.
    let Some(type_info) = value.get_represented_type_info() else {
        return write_reflect_ref(f, &value.reflect_ref(), ancestry, indent);
    };

    let type_id = type_info.type_id();
    let type_path = type_info.type_path();

    if ancestry.contains(&type_id) {
        return write!(f, "{type_path} {{ ... }}");
    }

    ancestry.push(type_id);
    let result = write_reflect_ref(f, &value.reflect_ref(), ancestry, indent);
    ancestry.pop();
    result
}

/// Writes a [`ReflectRef`], dispatching to the type-specific writer.
fn write_reflect_ref(
    f: &mut Formatter<'_>,
    reflect_ref: &ReflectRef<'_>,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    match *reflect_ref {
        ReflectRef::Struct(value) => write_struct(f, value, ancestry, indent),
        ReflectRef::TupleStruct(value) => write_tuple_struct(f, value, ancestry, indent),
        ReflectRef::Tuple(value) => write_tuple(f, value, ancestry, indent),
        ReflectRef::List(value) => write_list(f, value, ancestry, indent),
        ReflectRef::Array(value) => write_array(f, value, ancestry, indent),
        ReflectRef::Map(value) => write_map(f, value, ancestry, indent),
        ReflectRef::Set(value) => write_set(f, value, ancestry, indent),
        ReflectRef::Enum(value) => write_enum(f, value, ancestry, indent),
        ReflectRef::Opaque(value) => write_opaque(f, value, indent),
        #[cfg(feature = "functions")]
        ReflectRef::Function(function) => write_function(f, function),
    }
}

/// Writes a reflected [`Struct`] value.
fn write_struct(
    f: &mut Formatter<'_>,
    value: &dyn Struct,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    let type_name = display_type_name(value.get_represented_type_info(), "<Unknown Struct>");

    if value.field_len() == 0 {
        return write!(f, "{type_name} {{}}");
    }

    write_delimited_block(f, type_name, " ", '{', '}', indent, |fmt, entry_indent| {
        for i in 0..value.field_len() {
            let field_name = value.name_at(i).unwrap_or("<Unknown Field>");
            write_indent(fmt, entry_indent)?;
            fmt.write_str(field_name)?;
            fmt.write_str(": ")?;
            match value.field_at(i) {
                Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                None => fmt.write_str("<None>")?,
            }
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`TupleStruct`] value.
fn write_tuple_struct(
    f: &mut Formatter<'_>,
    value: &dyn TupleStruct,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    let type_name = display_type_name(value.get_represented_type_info(), "<Unknown TupleStruct>");

    if value.field_len() == 0 {
        return write!(f, "{type_name}()");
    }

    write_delimited_block(f, type_name, "", '(', ')', indent, |fmt, entry_indent| {
        for i in 0..value.field_len() {
            write_indent(fmt, entry_indent)?;
            match value.field(i) {
                Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                None => fmt.write_str("<None>")?,
            }
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`Tuple`] value.
fn write_tuple(
    f: &mut Formatter<'_>,
    value: &dyn Tuple,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    if value.field_len() == 0 {
        return f.write_str("()");
    }

    write_delimited_block(f, "", "", '(', ')', indent, |fmt, entry_indent| {
        for i in 0..value.field_len() {
            write_indent(fmt, entry_indent)?;
            match value.field(i) {
                Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                None => fmt.write_str("<None>")?,
            }
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`List`] value.
fn write_list(
    f: &mut Formatter<'_>,
    value: &dyn List,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    if value.is_empty() {
        return f.write_str("[]");
    }

    write_delimited_block(f, "", "", '[', ']', indent, |fmt, entry_indent| {
        for i in 0..value.len() {
            write_indent(fmt, entry_indent)?;
            match value.get(i) {
                Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                None => fmt.write_str("<None>")?,
            }
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`Array`] value.
fn write_array(
    f: &mut Formatter<'_>,
    value: &dyn Array,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    if value.is_empty() {
        return f.write_str("[]");
    }

    write_delimited_block(f, "", "", '[', ']', indent, |fmt, entry_indent| {
        for i in 0..value.len() {
            write_indent(fmt, entry_indent)?;
            match value.get(i) {
                Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                None => fmt.write_str("<None>")?,
            }
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`Map`] value.
fn write_map(
    f: &mut Formatter<'_>,
    value: &dyn Map,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    if value.is_empty() {
        return f.write_str("{}");
    }

    write_delimited_block(f, "", "", '{', '}', indent, |fmt, entry_indent| {
        for (key, key_value) in value.iter() {
            write_indent(fmt, entry_indent)?;
            write_value(fmt, key, ancestry, entry_indent)?;
            fmt.write_str(": ")?;
            write_value(fmt, key_value, ancestry, entry_indent)?;
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`Set`] value.
fn write_set(
    f: &mut Formatter<'_>,
    value: &dyn Set,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    if value.is_empty() {
        return f.write_str("{}");
    }

    write_delimited_block(f, "", "", '{', '}', indent, |fmt, entry_indent| {
        for element in value.iter() {
            write_indent(fmt, entry_indent)?;
            write_value(fmt, element, ancestry, entry_indent)?;
            fmt.write_str(",\n")?;
        }
        Ok(())
    })
}

/// Writes a reflected [`Enum`] value.
fn write_enum(
    f: &mut Formatter<'_>,
    value: &dyn Enum,
    ancestry: &mut Vec<TypeId>,
    indent: u32,
) -> core::fmt::Result {
    let type_name = display_type_name(value.get_represented_type_info(), "<Unknown Enum>");
    let variant = value.variant_name();
    let variant = if variant.is_empty() {
        "<unnamed>"
    } else {
        variant
    };
    let qualified = alloc::format!("{type_name}::{variant}");

    match value.variant_type() {
        VariantType::Struct => {
            if value.field_len() == 0 {
                return write!(f, "{qualified} {{}}");
            }

            write_delimited_block(f, &qualified, " ", '{', '}', indent, |fmt, entry_indent| {
                for i in 0..value.field_len() {
                    let field_name = value.name_at(i).unwrap_or("<Unknown Field>");
                    write_indent(fmt, entry_indent)?;
                    fmt.write_str(field_name)?;
                    fmt.write_str(": ")?;
                    match value.field_at(i) {
                        Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                        None => fmt.write_str("<None>")?,
                    }
                    fmt.write_str(",\n")?;
                }
                Ok(())
            })
        }
        VariantType::Tuple => {
            if value.field_len() == 0 {
                return write!(f, "{qualified}()");
            }

            write_delimited_block(f, &qualified, "", '(', ')', indent, |fmt, entry_indent| {
                for i in 0..value.field_len() {
                    write_indent(fmt, entry_indent)?;
                    match value.field_at(i) {
                        Some(child) => write_value(fmt, child, ancestry, entry_indent)?,
                        None => fmt.write_str("<None>")?,
                    }
                    fmt.write_str(",\n")?;
                }
                Ok(())
            })
        }
        VariantType::Unit => write!(f, "{qualified}"),
    }
}

/// Writes a reflected [`Function`] value.
///
/// This method does not need cycle tracking as functions cannot contain other reflected values.
#[cfg(feature = "functions")]
fn write_function(f: &mut Formatter<'_>, func: &dyn Function) -> core::fmt::Result {
    let pretty = func
        .info()
        .pretty_printer()
        .include_fn_token()
        .include_name();
    // TODO: PrettyPrintFunctionInfo implements Debug but not Display
    // so we just use the Debug formatting for now.
    write!(f, "{pretty:?}")
}

impl Display for dyn Struct {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_struct(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn TupleStruct {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_tuple_struct(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn Tuple {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_tuple(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn List {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_list(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn Array {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_array(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn Map {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_map(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn Set {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_set(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn Enum {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_enum(f, self, &mut Vec::new(), 0)
    }
}

#[cfg(feature = "functions")]
impl Display for dyn Function {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_function(f, self)
    }
}

impl Display for ReflectRef<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let mut ancestry = Vec::new();
        write_reflect_ref(f, self, &mut ancestry, 0)
    }
}

impl Display for dyn PartialReflect {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write_value(f, self, &mut Vec::new(), 0)
    }
}

impl Display for dyn Reflect {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        Display::fmt(self as &dyn PartialReflect, f)
    }
}

/// Display name for a reflected type, using its full type path.
///
/// Uses `fallback` when no type information is available (e.g. for dynamic values).
fn display_type_name(type_info: Option<&TypeInfo>, fallback: &'static str) -> &'static str {
    match type_info {
        Some(info) => info.type_path(),
        None => fallback,
    }
}

/// Writes a two-space indent for each level of indentation requested to the formatter.
fn write_indent(f: &mut Formatter<'_>, level: u32) -> core::fmt::Result {
    for _ in 0..level {
        f.write_str("  ")?;
    }
    Ok(())
}

/// Writes a delimited, indented block to `f`.
///
/// The `prefix{separator}{open}` is written first, then the closure writes
/// entries (which should use `entry_indent` for indentation), and finally
/// the closing `close` character is written at `indent`.
///
/// The caller must handle the empty case itself; this helper always emits
/// a multi-line block.
fn write_delimited_block(
    f: &mut Formatter<'_>,
    prefix: &str,
    separator: &str,
    open: char,
    close: char,
    indent: u32,
    write_entries: impl FnOnce(&mut Formatter<'_>, u32) -> core::fmt::Result,
) -> core::fmt::Result {
    let entry_indent = indent + 1;
    writeln!(f, "{prefix}{separator}{open}")?;
    write_entries(f, entry_indent)?;
    write_indent(f, indent)?;
    f.write_char(close)
}

/// Writes an opaque reflected value by delegating to its [`Debug`] implementation.
///
/// Single-line debug output is written directly. Multi-line debug output has
/// continuation lines indented one level deeper than `indent` (i.e. at the
/// same visual level as entries inside a container whose closing delimiter
/// sits at `indent`).
///
/// Leading whitespace is trimmed.
fn write_opaque(
    f: &mut Formatter<'_>,
    value: &dyn PartialReflect,
    indent: u32,
) -> core::fmt::Result {
    let debug = alloc::format!("{value:?}");
    let trimmed = debug.trim();

    // Fast path for single-line debug output
    if trimmed.len() == debug.len() && !trimmed.contains('\n') {
        return f.write_str(&debug);
    }

    // Multi-line: indent continuation lines to `indent + 1`
    // (one level deeper than the enclosing container closing delimiter).
    for (i, line) in trimmed.lines().enumerate() {
        if i > 0 {
            f.write_char('\n')?;
        }
        if line.trim().is_empty() {
            continue;
        }
        if i > 0 {
            write_indent(f, indent + 1)?;
        }
        // The first line (i == 0) is inline (no extra indent).
        f.write_str(line)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        array::DynamicArray, map::DynamicMap, set::DynamicSet, structs::DynamicStruct,
        tuple::DynamicTuple, tuple_struct::DynamicTupleStruct, Reflect,
    };
    use alloc::{
        boxed::Box,
        collections::BTreeMap,
        format,
        string::{String, ToString},
        vec,
    };
    use bevy_platform::collections::HashSet;
    use disqualified::ShortName;

    #[derive(Reflect, PartialEq, Eq, Hash)]
    #[reflect(Hash)]
    struct Inner {
        a: u32,
        b: String,
    }

    #[derive(Reflect)]
    struct Outer {
        name: String,
        inner: Inner,
        list: Vec<i32>,
    }

    #[derive(Reflect)]
    struct GraphNode {
        value: u32,
        children: Vec<GraphNode>,
    }

    #[derive(Reflect)]
    struct EmptyStruct {}

    #[derive(Reflect)]
    struct MutuallyRecursiveA {
        items: Vec<MutuallyRecursiveB>,
    }

    #[derive(Reflect)]
    struct MutuallyRecursiveB {
        items: Vec<MutuallyRecursiveA>,
    }

    #[derive(Reflect)]
    struct Newtype(u32);

    #[derive(Reflect)]
    struct EmptyTupleStruct();

    #[derive(Reflect)]
    enum MyEnum {
        Unit,
        Tuple(u32, String),
        Struct { x: i32, inner: Inner },
        EmptyTuple(),
        EmptyStruct {},
    }

    #[test]
    fn opaque_values_use_debug() {
        assert_eq!(format!("{}", &42u32 as &dyn PartialReflect), "42");
        assert_eq!(
            format!("{}", &"hi".to_string() as &dyn PartialReflect),
            "\"hi\""
        );
        assert_eq!(format!("{}", &true as &dyn PartialReflect), "true");
    }

    #[test]
    fn flat_struct() {
        let value = Inner {
            a: 1,
            b: "two".to_string(),
        };
        assert_eq!(
            format!("{}", &value as &dyn PartialReflect),
            "bevy_reflect::display::tests::Inner {\n  a: 1,\n  b: \"two\",\n}"
        );
    }

    #[test]
    fn empty_containers_collapse_to_one_line() {
        assert_eq!(
            format!("{}", &EmptyStruct {} as &dyn PartialReflect),
            "bevy_reflect::display::tests::EmptyStruct {}"
        );
        assert_eq!(
            format!("{}", &EmptyTupleStruct() as &dyn PartialReflect),
            "bevy_reflect::display::tests::EmptyTupleStruct()"
        );
        assert_eq!(
            format!("{}", &Vec::<i32>::new() as &dyn PartialReflect),
            "[]"
        );
        assert_eq!(format!("{}", &[0i32; 0] as &dyn PartialReflect), "[]");
        assert_eq!(format!("{}", &() as &dyn PartialReflect), "()");
        assert_eq!(
            format!("{}", &BTreeMap::<u32, u32>::new() as &dyn PartialReflect),
            "{}"
        );
        assert_eq!(
            format!("{}", &HashSet::<u32>::new() as &dyn PartialReflect),
            "{}"
        );
    }

    #[test]
    fn empty_enum_variants() {
        assert_eq!(
            format!("{}", &MyEnum::EmptyTuple() as &dyn PartialReflect),
            "bevy_reflect::display::tests::MyEnum::EmptyTuple()"
        );
        assert_eq!(
            format!("{}", &MyEnum::EmptyStruct {} as &dyn PartialReflect),
            "bevy_reflect::display::tests::MyEnum::EmptyStruct {}"
        );
    }

    #[test]
    fn newtype_struct() {
        assert_eq!(
            format!("{}", &Newtype(5) as &dyn PartialReflect),
            "bevy_reflect::display::tests::Newtype(\n  5,\n)"
        );
    }

    #[test]
    fn list_of_scalars() {
        assert_eq!(
            format!("{}", &vec![1, 2, 3] as &dyn PartialReflect),
            "[\n  1,\n  2,\n  3,\n]"
        );
    }

    #[test]
    fn map_entries() {
        let mut map = BTreeMap::new();
        map.insert(1u32, "one".to_string());
        map.insert(2u32, "two".to_string());
        assert_eq!(
            format!("{}", &map as &dyn PartialReflect),
            "{\n  1: \"one\",\n  2: \"two\",\n}"
        );
    }

    #[test]
    fn enum_variants() {
        assert_eq!(
            format!("{}", &MyEnum::Unit as &dyn PartialReflect),
            "bevy_reflect::display::tests::MyEnum::Unit"
        );
        assert_eq!(
            format!(
                "{}",
                &MyEnum::Tuple(7, "t".to_string()) as &dyn PartialReflect
            ),
            "bevy_reflect::display::tests::MyEnum::Tuple(\n  7,\n  \"t\",\n)"
        );
    }

    #[test]
    fn nested_values_are_indented_per_level() {
        let value = Outer {
            name: "hello".to_string(),
            inner: Inner {
                a: 1,
                b: "two".to_string(),
            },
            list: vec![10, 20],
        };

        let expected = "\
bevy_reflect::display::tests::Outer {
  name: \"hello\",
  inner: bevy_reflect::display::tests::Inner {
    a: 1,
    b: \"two\",
  },
  list: [
    10,
    20,
  ],
}";
        assert_eq!(format!("{}", &value as &dyn PartialReflect), expected);
    }

    #[test]
    fn deeply_nested_enum_struct_variant() {
        let value = MyEnum::Struct {
            x: -1,
            inner: Inner {
                a: 2,
                b: "q".to_string(),
            },
        };

        let expected = "\
bevy_reflect::display::tests::MyEnum::Struct {
  x: -1,
  inner: bevy_reflect::display::tests::Inner {
    a: 2,
    b: \"q\",
  },
}";
        assert_eq!(format!("{}", &value as &dyn PartialReflect), expected);
    }

    #[test]
    fn type_name_uses_full_path() {
        let value = Inner {
            a: 1,
            b: "two".to_string(),
        };
        assert_eq!(
            format!("{}", &value as &dyn PartialReflect),
            "bevy_reflect::display::tests::Inner {\n  a: 1,\n  b: \"two\",\n}"
        );
    }

    #[test]
    fn shortname_collapses_type_paths() {
        let value = Outer {
            name: "hello".to_string(),
            inner: Inner {
                a: 1,
                b: "two".to_string(),
            },
            list: vec![10, 20],
        };

        let full = format!("{}", &value as &dyn PartialReflect);
        // Verify the full version actually has paths to collapse
        assert!(full.contains("bevy_reflect::display::tests::Outer"));
        assert!(full.contains("bevy_reflect::display::tests::Inner"));

        let short = ShortName::from(full.as_str()).to_string();
        assert_eq!(
            short,
            concat!(
                "Outer {\n",
                "  name: \"hello\",\n",
                "  inner: Inner {\n",
                "    a: 1,\n",
                "    b: \"two\",\n",
                "  },\n",
                "  list: [\n",
                "    10,\n",
                "    20,\n",
                "  ],\n",
                "}"
            )
        );
    }

    #[derive(Reflect, Clone)]
    #[reflect(opaque, Debug)]
    struct TrailingNewlineDebug;

    impl core::fmt::Debug for TrailingNewlineDebug {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            writeln!(f, "value")
        }
    }

    #[derive(Reflect)]
    struct HoldsTrailingNewline {
        op: TrailingNewlineDebug,
    }

    #[test]
    fn opaque_debug_trailing_newline_does_not_orphan_comma() {
        assert_eq!(
            format!("{}", &TrailingNewlineDebug as &dyn PartialReflect),
            "value"
        );
        assert_eq!(
            format!(
                "{}",
                &HoldsTrailingNewline {
                    op: TrailingNewlineDebug
                } as &dyn PartialReflect
            ),
            "bevy_reflect::display::tests::HoldsTrailingNewline {\n  op: value,\n}"
        );
    }

    #[derive(Reflect, Clone)]
    #[reflect(opaque, Debug)]
    struct TrailingNewlineThenSpaces;

    impl core::fmt::Debug for TrailingNewlineThenSpaces {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "value\n  ")
        }
    }

    #[derive(Reflect)]
    struct HoldsTrailingNewlineThenSpaces {
        op: TrailingNewlineThenSpaces,
    }

    #[test]
    fn opaque_debug_trailing_whitespace_does_not_orphan_comma() {
        assert_eq!(
            format!("{}", &TrailingNewlineThenSpaces as &dyn PartialReflect),
            "value"
        );
        assert_eq!(
            format!(
                "{}",
                &HoldsTrailingNewlineThenSpaces {
                    op: TrailingNewlineThenSpaces
                } as &dyn PartialReflect
            ),
            "bevy_reflect::display::tests::HoldsTrailingNewlineThenSpaces {\n  op: value,\n}"
        );
    }

    #[derive(Reflect, Clone)]
    #[reflect(opaque, Debug)]
    struct MultiLineDebug;

    impl core::fmt::Debug for MultiLineDebug {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "line1\nline2")
        }
    }

    #[derive(Reflect)]
    struct HoldsMultiLine {
        op: MultiLineDebug,
    }

    #[test]
    fn multi_line_opaque_value_indents_continuation_lines() {
        assert_eq!(
            format!("{}", &MultiLineDebug as &dyn PartialReflect),
            "line1\n  line2"
        );
        assert_eq!(
            format!(
                "{}",
                &HoldsMultiLine { op: MultiLineDebug } as &dyn PartialReflect
            ),
            "bevy_reflect::display::tests::HoldsMultiLine {\n  op: line1\n    line2,\n}"
        );
    }

    #[derive(Reflect, Clone)]
    #[reflect(opaque, Debug)]
    struct BlankInteriorLineDebug;

    impl core::fmt::Debug for BlankInteriorLineDebug {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "line1\n   \nline2")
        }
    }

    #[derive(Reflect)]
    struct HoldsBlankInterior {
        op: BlankInteriorLineDebug,
    }

    #[test]
    fn opaque_debug_blank_interior_line_carries_no_trailing_whitespace() {
        assert_eq!(
            format!("{}", &BlankInteriorLineDebug as &dyn PartialReflect),
            "line1\n\n  line2"
        );
        assert_eq!(
            format!(
                "{}",
                &HoldsBlankInterior {
                    op: BlankInteriorLineDebug
                } as &dyn PartialReflect
            ),
            "bevy_reflect::display::tests::HoldsBlankInterior {\n  op: line1\n\n    line2,\n}"
        );
        let formatted = format!(
            "{}",
            &HoldsBlankInterior {
                op: BlankInteriorLineDebug
            } as &dyn PartialReflect
        );
        for line in formatted.lines() {
            assert_eq!(
                line.trim_end(),
                line,
                "line has trailing whitespace: {line:?}"
            );
        }
    }

    #[derive(Reflect, Clone)]
    #[reflect(opaque, Debug)]
    struct LeadingNewlineDebug;

    impl core::fmt::Debug for LeadingNewlineDebug {
        fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
            write!(f, "\nvalue")
        }
    }

    #[derive(Reflect)]
    struct HoldsLeadingNewline {
        op: LeadingNewlineDebug,
    }

    #[test]
    fn opaque_debug_leading_newline_does_not_leave_trailing_whitespace() {
        assert_eq!(
            format!("{}", &LeadingNewlineDebug as &dyn PartialReflect),
            "value"
        );
        assert_eq!(
            format!(
                "{}",
                &HoldsLeadingNewline {
                    op: LeadingNewlineDebug
                } as &dyn PartialReflect
            ),
            "bevy_reflect::display::tests::HoldsLeadingNewline {\n  op: value,\n}"
        );
    }

    #[test]
    fn multi_line_struct_as_list_element() {
        let value = vec![
            Inner {
                a: 1,
                b: "x".to_string(),
            },
            Inner {
                a: 2,
                b: "y".to_string(),
            },
        ];
        let expected = "\
[
  bevy_reflect::display::tests::Inner {
    a: 1,
    b: \"x\",
  },
  bevy_reflect::display::tests::Inner {
    a: 2,
    b: \"y\",
  },
]";
        assert_eq!(format!("{}", &value as &dyn PartialReflect), expected);
    }

    #[test]
    fn multi_line_struct_as_map_value() {
        let mut map = BTreeMap::new();
        map.insert(
            1u32,
            Inner {
                a: 10,
                b: "x".to_string(),
            },
        );
        let expected = "\
{
  1: bevy_reflect::display::tests::Inner {
    a: 10,
    b: \"x\",
  },
}";
        assert_eq!(format!("{}", &map as &dyn PartialReflect), expected);
    }

    #[test]
    fn map_with_struct_key() {
        let mut map = DynamicMap::default();
        map.insert(
            Inner {
                a: 1,
                b: "key".to_string(),
            },
            "value".to_string(),
        );
        assert_eq!(
            format!("{}", &map as &dyn PartialReflect),
            "{\n  bevy_reflect::display::tests::Inner {\n    a: 1,\n    b: \"key\",\n  }: \"value\",\n}"
        );
    }

    #[test]
    fn array_of_scalars() {
        let array = DynamicArray::new(
            vec![
                Box::new(1u32) as Box<dyn PartialReflect>,
                Box::new(2u32),
                Box::new(3u32),
            ]
            .into_boxed_slice(),
        );
        assert_eq!(
            format!("{}", &array as &dyn PartialReflect),
            "[\n  1,\n  2,\n  3,\n]"
        );
    }

    #[test]
    fn set_with_one_element() {
        let mut set = DynamicSet::default();
        set.insert(42u32);
        assert_eq!(format!("{}", &set as &dyn PartialReflect), "{\n  42,\n}");
    }

    #[test]
    fn set_with_struct_element() {
        let mut set = DynamicSet::default();
        set.insert(Inner {
            a: 10,
            b: "x".to_string(),
        });
        assert_eq!(
            format!("{}", &set as &dyn PartialReflect),
            "{\n  bevy_reflect::display::tests::Inner {\n    a: 10,\n    b: \"x\",\n  },\n}"
        );
    }

    #[test]
    fn dynamic_struct_fallback_name() {
        let mut dyn_struct = DynamicStruct::default();
        dyn_struct.insert("x", 1u32);
        dyn_struct.insert("y", 2u32);
        assert_eq!(
            format!("{}", &dyn_struct as &dyn PartialReflect),
            "<Unknown Struct> {\n  x: 1,\n  y: 2,\n}"
        );
    }

    #[test]
    fn dynamic_tuple_displays() {
        let mut dyn_tuple = DynamicTuple::default();
        dyn_tuple.insert(42u32);
        dyn_tuple.insert("hi".to_string());
        assert_eq!(
            format!("{}", &dyn_tuple as &dyn PartialReflect),
            "(\n  42,\n  \"hi\",\n)"
        );
    }

    #[test]
    fn dynamic_tuple_struct_fallback_name() {
        let mut dyn_tuple_struct = DynamicTupleStruct::default();
        dyn_tuple_struct.insert(42u32);
        dyn_tuple_struct.insert("hi".to_string());
        assert_eq!(
            format!("{}", &dyn_tuple_struct as &dyn PartialReflect),
            "<Unknown TupleStruct>(\n  42,\n  \"hi\",\n)"
        );
    }

    #[test]
    fn reflect_ref_displays() {
        let value = Inner {
            a: 1,
            b: "two".to_string(),
        };
        let reflect_ref = value.reflect_ref();
        assert_eq!(
            format!("{reflect_ref}"),
            "bevy_reflect::display::tests::Inner {\n  a: 1,\n  b: \"two\",\n}"
        );
    }

    #[test]
    fn cycle_detection_truncates_on_repeated_type_path() {
        let child = GraphNode {
            value: 2,
            children: Vec::new(),
        };
        let parent = GraphNode {
            value: 1,
            children: vec![child],
        };
        assert_eq!(
            format!("{}", &parent as &dyn PartialReflect),
            concat!(
                "bevy_reflect::display::tests::GraphNode {\n",
                "  value: 1,\n",
                "  children: [\n",
                "    bevy_reflect::display::tests::GraphNode { ... },\n",
                "  ],\n",
                "}"
            )
        );
    }

    #[test]
    fn cross_type_cycle_detection_truncates() {
        let inner = MutuallyRecursiveA { items: Vec::new() };
        let b = MutuallyRecursiveB { items: vec![inner] };
        let outer = MutuallyRecursiveA { items: vec![b] };
        assert_eq!(
            format!("{}", &outer as &dyn PartialReflect),
            concat!(
                "bevy_reflect::display::tests::MutuallyRecursiveA {\n",
                "  items: [\n",
                "    bevy_reflect::display::tests::MutuallyRecursiveB {\n",
                "      items: [\n",
                "        bevy_reflect::display::tests::MutuallyRecursiveA { ... },\n",
                "      ],\n",
                "    },\n",
                "  ],\n",
                "}"
            )
        );
    }

    #[cfg(feature = "functions")]
    mod functions {
        use super::*;
        use crate::func::IntoFunction;

        #[test]
        fn named_function() {
            fn greet(name: &String) -> String {
                format!("Hello, {name}!")
            }
            let function = greet.into_function();
            assert_eq!(
                format!("{}", &function as &dyn PartialReflect),
                "fn bevy_reflect::display::tests::functions::named_function::greet(_: &alloc::string::String) -> alloc::string::String"
            );
        }

        #[test]
        fn anonymous_function() {
            let function = (|a: i32, b: i32| a + b).into_function();
            assert_eq!(
                format!("{}", &function as &dyn PartialReflect),
                "fn _(_: i32, _: i32) -> i32"
            );
        }

        #[test]
        fn overloaded_function() {
            fn add_i32(a: i32, b: i32) -> i32 {
                a + b
            }
            fn add_f32(a: f32, b: f32) -> f32 {
                a + b
            }
            let function = add_i32
                .into_function()
                .with_overload(add_f32)
                .with_name("add");
            assert_eq!(
                format!("{}", &function as &dyn PartialReflect),
                "fn add {(_: i32, _: i32) -> i32, (_: f32, _: f32) -> f32}"
            );
        }
    }
}