mirror_mirror/foreign_impls/
vec.rs1use alloc::boxed::Box;
2use alloc::vec::Vec;
3use core::any::Any;
4
5use crate::array::Array;
6use crate::iter::ValueIterMut;
7use crate::type_info::graph::ListNode;
8use crate::type_info::graph::NodeId;
9use crate::type_info::graph::TypeGraph;
10use crate::DescribeType;
11use crate::FromReflect;
12use crate::List;
13use crate::Reflect;
14use crate::ReflectMut;
15use crate::ReflectOwned;
16use crate::ReflectRef;
17use crate::Value;
18
19impl<T> List for Vec<T>
20where
21 T: FromReflect + DescribeType,
22{
23 fn push(&mut self, value: &dyn Reflect) {
24 if let Some(value) = T::from_reflect(value) {
25 Vec::push(self, value);
26 }
27 }
28
29 fn pop(&mut self) -> Option<Box<dyn Reflect>> {
30 let value = Vec::pop(self)?;
31 Some(Box::new(value))
32 }
33
34 fn try_remove(&mut self, index: usize) -> Option<Box<dyn Reflect>> {
35 if index < self.len() {
36 let value = Vec::remove(self, index);
37 Some(Box::new(value))
38 } else {
39 None
40 }
41 }
42}
43
44impl<T> Array for Vec<T>
45where
46 T: FromReflect + DescribeType,
47{
48 fn get(&self, index: usize) -> Option<&dyn Reflect> {
49 self.as_slice().get(index).map(|value| value.as_reflect())
50 }
51
52 fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect> {
53 self.as_mut_slice()
54 .get_mut(index)
55 .map(|value| value.as_reflect_mut())
56 }
57
58 fn len(&self) -> usize {
59 Vec::len(self)
60 }
61
62 fn is_empty(&self) -> bool {
63 Vec::is_empty(self)
64 }
65
66 fn iter(&self) -> crate::array::Iter<'_> {
67 crate::array::Iter::new(self)
68 }
69
70 fn iter_mut(&mut self) -> ValueIterMut<'_> {
71 let iter = self
72 .as_mut_slice()
73 .iter_mut()
74 .map(|value| value.as_reflect_mut());
75 Box::new(iter)
76 }
77}
78
79impl<T> DescribeType for Vec<T>
80where
81 T: DescribeType,
82{
83 fn build(graph: &mut TypeGraph) -> NodeId {
84 graph.get_or_build_node_with::<Self, _>(|graph| ListNode::new::<Self, T>(graph))
85 }
86}
87
88impl<T> Reflect for Vec<T>
89where
90 T: FromReflect + DescribeType,
91{
92 trivial_reflect_methods!();
93
94 fn patch(&mut self, value: &dyn Reflect) {
95 if let Some(list) = value.reflect_ref().as_list() {
96 for (idx, new_value) in list.iter().enumerate() {
97 if let Some(value) = self.get_mut(idx) {
98 value.patch(new_value);
99 }
100 }
101 }
102 }
103
104 fn to_value(&self) -> Value {
105 let data = self.iter().map(Reflect::to_value).collect();
106 Value::List(data)
107 }
108
109 fn clone_reflect(&self) -> Box<dyn Reflect> {
110 let value = self.to_value();
111 Box::new(Self::from_reflect(&value).unwrap())
112 }
113
114 fn debug(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
115 f.debug_list().entries(self.iter()).finish()
116 }
117
118 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
119 ReflectOwned::List(self)
120 }
121
122 fn reflect_ref(&self) -> ReflectRef<'_> {
123 ReflectRef::List(self)
124 }
125
126 fn reflect_mut(&mut self) -> ReflectMut<'_> {
127 ReflectMut::List(self)
128 }
129}
130
131impl<T> FromReflect for Vec<T>
132where
133 T: FromReflect + DescribeType,
134{
135 fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
136 let list = reflect.reflect_ref().as_list()?;
137 let mut out = Vec::new();
138 for value in list.iter() {
139 out.push(T::from_reflect(value)?);
140 }
141 Some(out)
142 }
143}
144
145impl<T> From<Vec<T>> for Value
146where
147 T: Reflect,
148{
149 fn from(list: Vec<T>) -> Self {
150 let list = list
151 .into_iter()
152 .map(|value| value.to_value())
153 .collect::<Vec<_>>();
154 Value::List(list)
155 }
156}