bevy_mod_scripting_bindings 0.19.0

Core traits and structures required for smoothly interfacing with other languages in a generic way
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
//! Various utility functions for working with reflection types.

use std::{
    any::{Any, TypeId},
    cmp::max,
};

use bevy_reflect::{PartialReflect, Reflect, ReflectFromReflect, ReflectMut, ReflectRef, TypeInfo};

use crate::{ReflectReference, WorldGuard, error::InteropError};

/// Extension trait for [`PartialReflect`] providing additional functionality for working with specific types.
pub trait PartialReflectExt {
    /// Try to get a reference to the given key in an underyling map, if the type is a map.
    fn try_map_get(
        &self,
        key: &dyn PartialReflect,
    ) -> Result<Option<&dyn PartialReflect>, InteropError>;

    /// Try to remove the value at the given key, if the type supports removing with the given key.
    fn try_remove_boxed(
        &mut self,
        key: Box<dyn PartialReflect>,
    ) -> Result<Option<Box<dyn PartialReflect>>, InteropError>;

    /// Convert index keys to 0-indexed keys if this type is an index key.
    fn convert_to_0_indexed_key(&mut self);

    /// Try to create a new instance of the concrete type from a possibly untyped reference.
    fn from_reflect_or_clone(
        reflect: &dyn PartialReflect,
        world: WorldGuard,
    ) -> Result<Box<dyn PartialReflect>, InteropError>;

    /// Allocate a new boxed reflect reference from a boxed reflect.
    fn allocate(boxed: Box<dyn Reflect>, world: WorldGuard) -> ReflectReference;

    /// Check if the represented type is from the given crate and has the given type identifier,
    /// returns false if not representing any type or if the type is not from the given crate or does not have the given type identifier.
    fn is_type(&self, crate_name: Option<&str>, type_ident: &str) -> bool;

    /// Equivalent to [`PartialReflectExt::is_type`] but returns an appropriate error if the type is not the expected one.  
    fn expect_type(&self, crate_name: Option<&str>, type_ident: &str) -> Result<(), InteropError>;

    /// If the type is an option, returns either the inner value or None if the option is None.
    /// Errors if the type is not an option.
    fn as_option(&self) -> Result<Option<&dyn PartialReflect>, InteropError>;

    /// Similar to [`PartialReflectExt::as_option`] but for mutable references.
    fn as_option_mut(&mut self) -> Result<Option<&mut dyn PartialReflect>, InteropError>;

    /// If the type is an iterable list-like type, returns an iterator over the elements.
    fn as_list(&self) -> Result<impl Iterator<Item = &dyn PartialReflect>, InteropError>;

    /// If the type is a usize, returns the value as a usize otherwise throws a convenient error
    fn as_usize(&self) -> Result<usize, InteropError>;

    /// If the type is an iterable list-like type, sets the elements of the list to the elements of the other list-like type.
    /// This acts as a set operation, so the left list will have the same length as the right list after this operation.
    fn set_as_list<
        F: Fn(&mut dyn PartialReflect, &dyn PartialReflect) -> Result<(), InteropError>,
    >(
        &mut self,
        other: Box<dyn PartialReflect>,
        apply: F,
    ) -> Result<(), InteropError>;

    /// Inserts into the type at the given key, if the type supports inserting with the given key
    fn try_insert_boxed(
        &mut self,
        index: Box<dyn PartialReflect>,
        value: Box<dyn PartialReflect>,
    ) -> Result<(), InteropError>;

    /// Tries to insert the given value into the type, if the type is a container type.
    /// The insertion will happen at the natural `end` of the container.
    /// For lists, this is the length of the list.
    /// For sets, this will simply insert the value into the set.
    /// For maps, there is no natural `end`, so the push will error out
    fn try_push_boxed(&mut self, value: Box<dyn PartialReflect>) -> Result<(), InteropError>;

    /// If the type has a natural last element to pop, pops the last element and returns it as a boxed value.
    fn try_pop_boxed(&mut self) -> Result<Box<dyn PartialReflect>, InteropError>;

    /// If the type is a container type, empties the contents
    fn try_clear(&mut self) -> Result<(), InteropError>;

    /// If the type is a container type, returns the type id of the elements in the container.
    /// For maps, this is the type id of the values.
    fn element_type_id(&self) -> Option<TypeId>;

    /// If the type is a container type, returns the type id of the keys in the container.
    /// For lists and arrays, this is the type id of usize.
    /// For maps, this is the type id of the keys.
    fn key_type_id(&self) -> Option<TypeId>;

    /// Tries to construct the concrete underlying type from a possibly untyped reference
    fn from_reflect(
        reflect: &dyn PartialReflect,
        world: WorldGuard,
    ) -> Result<Box<dyn Reflect>, InteropError>;
}

impl<T: PartialReflect + ?Sized> PartialReflectExt for T {
    fn is_type(&self, crate_name: Option<&str>, type_ident: &str) -> bool {
        self.get_represented_type_info().is_some_and(|v| {
            let table = v.type_path_table();
            table.crate_name() == crate_name && table.ident() == Some(type_ident)
        })
    }

    fn expect_type(&self, crate_name: Option<&str>, type_ident: &str) -> Result<(), InteropError> {
        if !self.is_type(crate_name, type_ident) {
            return Err(InteropError::string_type_mismatch(
                type_ident.to_owned(),
                self.get_represented_type_info().map(|ti| ti.type_id()),
            ));
        }
        Ok(())
    }

    fn as_option(&self) -> Result<Option<&dyn PartialReflect>, InteropError> {
        if let ReflectRef::Enum(e) = self.reflect_ref()
            && e.is_type(Some("core"), "Option")
        {
            if let Some(field) = e.field_at(0) {
                return Ok(Some(field));
            } else {
                return Ok(None);
            }
        }

        Err(InteropError::string_type_mismatch(
            "Option<T>".to_owned(),
            self.get_represented_type_info().map(|ti| ti.type_id()),
        ))
    }

    fn as_option_mut(&mut self) -> Result<Option<&mut dyn PartialReflect>, InteropError> {
        let type_info = self.get_represented_type_info().map(|ti| ti.type_path());
        match self.reflect_mut() {
            ReflectMut::Enum(e) => {
                if let Some(field) = e.field_at_mut(0) {
                    Ok(Some(field))
                } else {
                    Ok(None)
                }
            }
            _ => Err(InteropError::string_type_mismatch(
                "Option<T>".to_owned(),
                type_info.map(|t| t.type_id()),
            )),
        }
    }

    fn as_list(&self) -> Result<impl Iterator<Item = &dyn PartialReflect>, InteropError> {
        if let ReflectRef::List(l) = self.reflect_ref() {
            Ok(l.iter())
        } else {
            Err(InteropError::string_type_mismatch(
                "List<T>".to_owned(),
                self.get_represented_type_info().map(|ti| ti.type_id()),
            ))
        }
    }

    fn set_as_list<
        F: Fn(&mut dyn PartialReflect, &dyn PartialReflect) -> Result<(), InteropError>,
    >(
        &mut self,
        mut other: Box<dyn PartialReflect>,
        apply: F,
    ) -> Result<(), InteropError> {
        match (self.reflect_mut(), other.reflect_mut()) {
            (ReflectMut::List(l), ReflectMut::List(r)) => {
                let to_be_inserted_elems = max(r.len() as isize - l.len() as isize, 0) as usize;
                let apply_range = 0..(r.len() - to_be_inserted_elems);

                // remove in reverse order
                (r.len()..l.len()).rev().for_each(|i| {
                    l.remove(i);
                });

                // pop then insert in reverse order of popping (last elem -> first elem to insert)
                let to_insert = (0..to_be_inserted_elems)
                    .rev()
                    .map_while(|_| r.pop())
                    .collect::<Vec<_>>();

                to_insert.into_iter().rev().for_each(|e| {
                    l.push(e);
                });

                // at this point l is at least as long as r

                // apply to existing elements in the list
                for i in apply_range {
                    let left = l.get_mut(i);
                    let right = r.get(i);
                    match (left, right) {
                        (Some(left), Some(right)) => apply(left, right)?,
                        _ => return Err(InteropError::invariant("set_as_list failed")),
                    };
                }

                Ok(())
            }
            (ReflectMut::List(_), _) => Err(InteropError::string_type_mismatch(
                "List<T>".to_owned(),
                other.get_represented_type_info().map(|ti| ti.type_id()),
            )),
            (_, _) => Err(InteropError::string_type_mismatch(
                "List<T>".to_owned(),
                self.get_represented_type_info().map(|ti| ti.type_id()),
            )),
        }
    }

    fn as_usize(&self) -> Result<usize, InteropError> {
        self.as_partial_reflect()
            .try_downcast_ref::<usize>()
            .copied()
            .ok_or_else(|| {
                InteropError::type_mismatch(
                    TypeId::of::<usize>(),
                    self.get_represented_type_info().map(|ti| ti.type_id()),
                )
            })
    }

    fn try_insert_boxed(
        &mut self,
        key: Box<dyn PartialReflect>,
        value: Box<dyn PartialReflect>,
    ) -> Result<(), InteropError> {
        match self.reflect_mut() {
            ReflectMut::List(l) => {
                l.insert(key.as_usize()?, value);
                Ok(())
            }
            ReflectMut::Map(m) => {
                m.insert_boxed(key, value);
                Ok(())
            }
            ReflectMut::Set(s) => {
                s.insert_boxed(value);
                Ok(())
            }
            _ => Err(InteropError::unsupported_operation(
                self.get_represented_type_info().map(|ti| ti.type_id()),
                Some(value),
                "insert".to_owned(),
            )),
        }
    }

    fn try_push_boxed(&mut self, value: Box<dyn PartialReflect>) -> Result<(), InteropError> {
        match self.reflect_mut() {
            ReflectMut::List(l) => {
                l.push(value);
                Ok(())
            }
            ReflectMut::Set(s) => {
                s.insert_boxed(value);
                Ok(())
            }
            _ => Err(InteropError::unsupported_operation(
                self.get_represented_type_info().map(|ti| ti.type_id()),
                Some(value),
                "push".to_owned(),
            )),
        }
    }

    fn convert_to_0_indexed_key(&mut self) {
        if let Some(usize) = self
            .try_as_reflect_mut()
            .and_then(|r| r.downcast_mut::<usize>())
        {
            *usize = usize.saturating_sub(1);
        }
    }

    fn try_clear(&mut self) -> Result<(), InteropError> {
        match self.reflect_mut() {
            ReflectMut::List(l) => {
                let _ = l.drain();
                Ok(())
            }
            ReflectMut::Map(m) => {
                let _ = m.drain();
                Ok(())
            }
            ReflectMut::Set(s) => {
                let _ = s.drain();
                Ok(())
            }
            _ => Err(InteropError::unsupported_operation(
                self.get_represented_type_info().map(|ti| ti.type_id()),
                None,
                "clear".to_owned(),
            )),
        }
    }

    fn try_map_get(
        &self,
        key: &dyn PartialReflect,
    ) -> Result<Option<&dyn PartialReflect>, InteropError> {
        match self.reflect_ref() {
            ReflectRef::Map(m) => Ok(m.get(key)),
            _ => Err(InteropError::unsupported_operation(
                self.get_represented_type_info().map(|ti| ti.type_id()),
                None,
                "map_get".to_owned(),
            )),
        }
    }

    fn try_pop_boxed(&mut self) -> Result<Box<dyn PartialReflect>, InteropError> {
        match self.reflect_mut() {
            ReflectMut::List(l) => l.pop().ok_or_else(|| {
                InteropError::unsupported_operation(
                    self.get_represented_type_info().map(|ti| ti.type_id()),
                    None,
                    "pop from empty list".to_owned(),
                )
            }),
            _ => Err(InteropError::unsupported_operation(
                self.get_represented_type_info().map(|ti| ti.type_id()),
                None,
                "pop".to_owned(),
            )),
        }
    }

    fn try_remove_boxed(
        &mut self,
        key: Box<dyn PartialReflect>,
    ) -> Result<Option<Box<dyn PartialReflect>>, InteropError> {
        match self.reflect_mut() {
            ReflectMut::List(l) => Ok(Some(l.remove(key.as_usize()?))),
            ReflectMut::Map(m) => Ok(m.remove(key.as_partial_reflect())),
            ReflectMut::Set(s) => {
                let removed = s.remove(key.as_partial_reflect());
                Ok(removed.then_some(key))
            }
            _ => Err(InteropError::unsupported_operation(
                self.get_represented_type_info().map(|ti| ti.type_id()),
                Some(key),
                "remove".to_owned(),
            )),
        }
    }

    fn element_type_id(&self) -> Option<TypeId> {
        let elem: TypeId = match self.get_represented_type_info()? {
            TypeInfo::List(list_info) => list_info.item_ty().id(),
            TypeInfo::Array(array_info) => array_info.item_ty().id(),
            TypeInfo::Map(map_info) => map_info.value_ty().id(),
            TypeInfo::Set(set_info) => set_info.value_ty().id(),
            _ => return None,
        };
        Some(elem)
    }

    fn key_type_id(&self) -> Option<TypeId> {
        let key: TypeId = match self.get_represented_type_info()? {
            TypeInfo::Map(map_info) => map_info.key_ty().id(),
            TypeInfo::List(_) | TypeInfo::Array(_) => TypeId::of::<usize>(),
            _ => return None,
        };
        Some(key)
    }

    fn from_reflect(
        reflect: &dyn PartialReflect,
        world: WorldGuard,
    ) -> Result<Box<dyn Reflect>, InteropError> {
        let type_info = reflect.get_represented_type_info().ok_or_else(|| {
            InteropError::failed_from_reflect(
                None,
                "tried to construct a concrete type from dynamic type with no type information"
                    .to_owned(),
            )
        })?;
        let type_id = type_info.type_id();

        let type_registry = world.type_registry();
        let type_registry = type_registry.read();

        let from_reflect_type_data: &ReflectFromReflect =
            type_registry.get_type_data(type_id).ok_or_else(|| {
                InteropError::missing_type_data(type_id, "ReflectFromReflect".to_owned())
            })?;
        from_reflect_type_data.from_reflect(reflect).ok_or_else(|| {
            InteropError::failed_from_reflect(
                Some(type_id),
                "from_reflect returned None".to_owned(),
            )
        })
    }

    /// Try creating an owned partial reflect from a reference. Will try using [`ReflectFromReflect`] first, and if that fails, will clone the value using [`PartialReflect::clone_value`].
    fn from_reflect_or_clone(
        reflect: &dyn PartialReflect,
        world: WorldGuard,
    ) -> Result<Box<dyn PartialReflect>, InteropError> {
        // try from reflect
        match <dyn PartialReflect>::from_reflect(reflect, world.clone()) {
            Ok(v) => Ok(v.into_partial_reflect()),
            Err(_) => reflect
                .reflect_clone()
                .map(|v| v.into_partial_reflect())
                .map_err(|e| {
                    InteropError::failed_from_reflect(
                        reflect.get_represented_type_info().map(|ti| ti.type_id()),
                        e.to_string(),
                    )
                }),
        }
    }

    fn allocate(boxed: Box<dyn Reflect>, world: WorldGuard) -> ReflectReference {
        let allocator = world.allocator();
        let mut allocator = allocator.write();
        ReflectReference::new_allocated_boxed(boxed, &mut allocator)
    }
}

/// Extension trait for TypeInfos providing additional functionality for working with type information.
pub trait TypeInfoExtensions {
    /// Returns the inner type of the set if the type is a set, otherwise None
    fn set_inner_type(&self) -> Option<TypeId>;
    /// Returns true if the type is a result.
    fn is_result(&self) -> bool;
    /// Returns the inner type of the map if the type is a map, otherwise None.
    fn map_inner_types(&self) -> Option<(TypeId, TypeId)>;
    /// Returns the inner type of the list if the type is a list, otherwise None.
    fn list_inner_type(&self) -> Option<TypeId>;
    /// Returns true if the type is a list.
    fn is_list(&self) -> bool;
    /// Returns true if the type is an option.
    fn is_option(&self) -> bool;
    /// Returns the inner type of the option if the type is an option, otherwise None.
    fn option_inner_type(&self) -> Option<TypeId>;
    /// Returns true if the type is the given type from the given crate.
    fn is_type(&self, crate_name: Option<&str>, type_ident: &str) -> bool;
}

impl TypeInfoExtensions for TypeInfo {
    fn is_option(&self) -> bool {
        self.is_type(Some("core"), "Option")
    }

    fn is_result(&self) -> bool {
        self.is_type(Some("core"), "Result")
    }

    fn is_list(&self) -> bool {
        matches!(self, TypeInfo::List(_))
    }

    fn option_inner_type(&self) -> Option<TypeId> {
        if self.is_option() {
            self.generics().first().map(|g| g.type_id())
        } else {
            None
        }
    }

    fn list_inner_type(&self) -> Option<TypeId> {
        Some(self.as_list().ok()?.item_ty().id())
    }

    fn map_inner_types(&self) -> Option<(TypeId, TypeId)> {
        let map = self.as_map().ok()?;
        Some((map.key_ty().id(), map.value_ty().id()))
    }

    fn set_inner_type(&self) -> Option<TypeId> {
        match self {
            TypeInfo::Set(info) => Some(info.value_ty().id()),
            _ => None,
        }
    }

    fn is_type(&self, crate_name: Option<&str>, type_ident: &str) -> bool {
        self.type_path_table().ident() == Some(type_ident)
            && self.type_path_table().crate_name() == crate_name
    }
}

#[cfg(test)]
mod test {
    use bevy_platform::collections::HashMap;
    use bevy_reflect::{DynamicMap, Map};

    use super::*;

    #[test]
    fn test_type_info_is_option() {
        let type_info = Some("hello").get_represented_type_info().unwrap();
        assert!(type_info.is_option());
    }

    #[test]
    fn test_type_info_is_type() {
        let type_info = Some("hello").get_represented_type_info().unwrap();
        assert!(type_info.is_type(Some("core"), "Option"));
    }

    #[test]
    fn test_type_no_crate() {
        assert!(42.is_type(None, "i32"));
        assert!(42.expect_type(None, "i32").is_ok());
    }

    #[test]
    fn test_is_type_with_crate() {
        assert!(Some(42).is_type(Some("core"), "Option"));
        assert!(Some(42).expect_type(Some("core"), "Option").is_ok());
    }

    #[test]
    fn test_is_type_negative() {
        assert!(!Some(42).is_type(Some("std"), "Option"));
    }

    #[test]
    fn test_as_option_some() {
        assert_eq!(
            &42,
            Some(42)
                .as_option()
                .unwrap()
                .unwrap()
                .try_downcast_ref::<i32>()
                .unwrap()
        );
    }

    #[test]
    fn test_as_option_none() {
        #[derive(Reflect)]
        enum Test {
            Unit,
        }

        assert!(None::<i32>.as_option().unwrap().is_none());
        assert!(Test::Unit.as_option().is_err())
    }

    #[test]
    fn test_as_list() {
        let list = vec![1, 2, 3];
        let list_ref: &dyn PartialReflect = &list;
        let iter = list_ref
            .as_list()
            .unwrap()
            .map(|r| *r.try_downcast_ref::<i32>().unwrap())
            .collect::<Vec<_>>();
        assert_eq!(list, iter);
    }

    #[test]
    fn test_set_as_list_equal_length() {
        let mut list = vec![1, 2, 3];
        let other = vec![4, 5, 6];
        let other_ref: Box<dyn PartialReflect> = Box::new(other.clone());
        list.set_as_list(other_ref, |l, r| {
            *l.try_downcast_mut::<i32>().unwrap() = *r.try_downcast_ref::<i32>().unwrap();
            Ok(())
        })
        .unwrap();
        assert_eq!(other, list);
    }

    #[test]
    fn test_set_as_list_shortening() {
        let mut list = vec![1, 2, 3];
        let other = vec![4, 5];
        let other_ref: Box<dyn PartialReflect> = Box::new(other.clone());
        list.set_as_list(other_ref, |l, r| {
            *l.try_downcast_mut::<i32>().unwrap() = *r.try_downcast_ref::<i32>().unwrap();
            Ok(())
        })
        .unwrap();
        assert_eq!(other, list);
    }

    #[test]
    fn test_set_as_list_lengthening() {
        let mut list = vec![1, 2];
        let other = vec![4, 5, 6];
        let other_ref: Box<dyn PartialReflect> = Box::new(other.clone());
        list.set_as_list(other_ref, |l, r| {
            *l.try_downcast_mut::<i32>().unwrap() = *r.try_downcast_ref::<i32>().unwrap();
            Ok(())
        })
        .unwrap();
        assert_eq!(other, list);
    }

    #[test]
    fn test_set_as_list_empty() {
        let mut list = vec![1, 2];
        let other = Vec::<i32>::default();
        let other_ref: Box<dyn PartialReflect> = Box::new(other.clone());
        list.set_as_list(other_ref, |l, r| {
            *l.try_downcast_mut::<i32>().unwrap() = *r.try_downcast_ref::<i32>().unwrap();
            Ok(())
        })
        .unwrap();
        assert_eq!(other, list);
    }

    #[test]
    fn test_set_as_list_targe_empty() {
        let mut list = Vec::<i32>::default();
        let other = vec![1];
        let other_ref: Box<dyn PartialReflect> = Box::new(other.clone());
        list.set_as_list(other_ref, |l, r| {
            *l.try_downcast_mut::<i32>().unwrap() = *r.try_downcast_ref::<i32>().unwrap();
            Ok(())
        })
        .unwrap();
        assert_eq!(other, list);
    }

    #[test]
    fn test_try_insert_vec() {
        let mut list = vec![1, 2, 3];
        let value = 4;
        let value_ref: Box<dyn PartialReflect> = Box::new(value);
        list.try_insert_boxed(Box::new(1usize), value_ref).unwrap();
        assert_eq!(vec![1, 4, 2, 3], list);
    }

    #[test]
    fn test_try_insert_map() {
        let mut map = HashMap::<i32, i32>::default();
        let value = 4;
        let value_ref: Box<dyn PartialReflect> = Box::new(value);
        map.insert(1, 2);
        map.insert(2, 3);
        map.insert(3, 4);
        map.try_insert_boxed(Box::new(1), value_ref).unwrap();
        assert_eq!(4, map[&1]);
    }

    #[test]
    fn test_try_insert_set() {
        let mut set = std::collections::HashSet::<i32>::default();
        let value = 4;
        let value_ref: Box<dyn PartialReflect> = Box::new(value);
        set.insert(1);
        set.insert(2);
        set.insert(3);
        set.try_insert_boxed(Box::new(1), value_ref).unwrap();
        assert!(set.contains(&4));
    }

    #[test]
    fn test_try_insert_dynamic_map_into_map_of_maps() {
        let mut map = HashMap::<i32, HashMap<i32, i32>>::default();
        let value = DynamicMap::from_iter(vec![(1, 2), (2, 3), (3, 4)]);
        let value_ref: Box<dyn PartialReflect> = Box::new(value.to_dynamic_map());
        map.insert(1, HashMap::<i32, i32>::default());
        map.insert(2, HashMap::<i32, i32>::default());
        map.insert(3, HashMap::<i32, i32>::default());
        map.try_insert_boxed(Box::new(1), value_ref).unwrap();
        assert!(value.reflect_partial_eq(&map[&1]).unwrap());
    }
}