mirror_mirror/foreign_impls/
boxed.rs

1use alloc::borrow::Cow;
2use alloc::boxed::Box;
3use core::any::Any;
4use core::fmt;
5
6use crate::reflect_debug;
7use crate::type_info::graph::NodeId;
8use crate::type_info::graph::TypeGraph;
9use crate::DescribeType;
10use crate::FromReflect;
11use crate::Reflect;
12use crate::ReflectMut;
13use crate::ReflectOwned;
14use crate::ReflectRef;
15use crate::TypeDescriptor;
16use crate::Value;
17
18impl<T> DescribeType for Box<T>
19where
20    T: DescribeType,
21{
22    fn build(graph: &mut TypeGraph) -> NodeId {
23        T::build(graph)
24    }
25}
26
27impl<T> Reflect for Box<T>
28where
29    T: Reflect + DescribeType,
30{
31    fn type_descriptor(&self) -> Cow<'static, TypeDescriptor> {
32        <T as DescribeType>::type_descriptor()
33    }
34
35    fn as_any(&self) -> &dyn Any {
36        <T as Reflect>::as_any(self)
37    }
38
39    fn as_any_mut(&mut self) -> &mut dyn Any {
40        <T as Reflect>::as_any_mut(self)
41    }
42
43    fn as_reflect(&self) -> &dyn Reflect {
44        <T as Reflect>::as_reflect(self)
45    }
46
47    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
48        <T as Reflect>::as_reflect_mut(self)
49    }
50
51    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
52        <T as Reflect>::reflect_owned(*self)
53    }
54
55    fn reflect_ref(&self) -> ReflectRef<'_> {
56        <T as Reflect>::reflect_ref(self)
57    }
58
59    fn reflect_mut(&mut self) -> ReflectMut<'_> {
60        <T as Reflect>::reflect_mut(self)
61    }
62
63    fn patch(&mut self, value: &dyn Reflect) {
64        <T as Reflect>::patch(self, value)
65    }
66
67    fn to_value(&self) -> Value {
68        <T as Reflect>::to_value(self)
69    }
70
71    fn clone_reflect(&self) -> Box<dyn Reflect> {
72        <T as Reflect>::clone_reflect(self)
73    }
74
75    fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        reflect_debug(self, f)
77    }
78}
79
80impl<T> FromReflect for Box<T>
81where
82    T: FromReflect + DescribeType,
83{
84    fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
85        Some(Box::new(T::from_reflect(reflect)?))
86    }
87}
88
89impl<T> From<Box<T>> for Value
90where
91    T: Into<Value>,
92{
93    fn from(boxed: Box<T>) -> Self {
94        (*boxed).into()
95    }
96}