From 8556409646a5598bb6bd4ff9cea07c120c68d776 Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Tue, 9 Jul 2024 19:12:44 +0200 Subject: [PATCH 1/8] =?UTF-8?q?DynamicArray::from=5Fvec=20=E2=86=92=20from?= =?UTF-8?q?=5Fvalues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bevy_reflect/src/array.rs | 5 +++++ crates/bevy_reflect/src/lib.rs | 2 +- examples/reflection/dynamic_types.rs | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 6db72df19354e..474acc791a52f 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -192,7 +192,12 @@ impl DynamicArray { } } + #[deprecated(since = "0.15.0", note = "use from_values")] pub fn from_vec(values: Vec) -> Self { + Self::from_values(values) + } + + pub fn from_values(values: impl IntoIterator) -> Self { Self { represented_type: None, values: values diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index dcd77bb214aed..2cd26d1f2e562 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -1088,7 +1088,7 @@ mod tests { }); foo_patch.insert("g", composite); - let array = DynamicArray::from_vec(vec![2u32, 2u32]); + let array = DynamicArray::from_values([2u32, 2u32]); foo_patch.insert("h", array); foo.apply(&foo_patch); diff --git a/examples/reflection/dynamic_types.rs b/examples/reflection/dynamic_types.rs index cdf9112745896..6430f57296233 100644 --- a/examples/reflection/dynamic_types.rs +++ b/examples/reflection/dynamic_types.rs @@ -167,7 +167,7 @@ fn main() { // 2. `DynamicArray` { - let dynamic_array = DynamicArray::from_vec(vec![1u32, 2u32, 3u32]); + let dynamic_array = DynamicArray::from_values([1u32, 2u32, 3u32]); let mut my_array = [0u32; 3]; my_array.apply(&dynamic_array); From 5f052bdec41436b779f221e0d27e4b889d064f56 Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Tue, 9 Jul 2024 21:34:49 +0200 Subject: [PATCH 2/8] FromIterator/IntoIterator for dynamic types --- crates/bevy_reflect/src/array.rs | 55 +++++++++++++++++++------ crates/bevy_reflect/src/lib.rs | 2 +- crates/bevy_reflect/src/list.rs | 30 ++++++++++++++ crates/bevy_reflect/src/map.rs | 29 +++++++++++++ crates/bevy_reflect/src/struct_trait.rs | 38 +++++++++++++++++ crates/bevy_reflect/src/tuple.rs | 39 ++++++++++++++++++ crates/bevy_reflect/src/tuple_struct.rs | 39 ++++++++++++++++++ examples/reflection/dynamic_types.rs | 2 +- 8 files changed, 219 insertions(+), 15 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 474acc791a52f..f29401dcfd972 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -192,20 +192,9 @@ impl DynamicArray { } } - #[deprecated(since = "0.15.0", note = "use from_values")] + #[deprecated(since = "0.15.0", note = "use from_iter")] pub fn from_vec(values: Vec) -> Self { - Self::from_values(values) - } - - pub fn from_values(values: impl IntoIterator) -> Self { - Self { - represented_type: None, - values: values - .into_iter() - .map(|field| Box::new(field) as Box) - .collect::>() - .into_boxed_slice(), - } + Self::from_iter(values) } /// Sets the [type] to be represented by this `DynamicArray`. @@ -363,6 +352,46 @@ impl Array for DynamicArray { } } +impl FromIterator> for DynamicArray { + fn from_iter>>(values: I) -> Self { + Self { + represented_type: None, + values: values.into_iter().collect::>().into_boxed_slice(), + } + } +} + +impl FromIterator for DynamicArray { + fn from_iter>(values: I) -> Self { + Self { + represented_type: None, + values: values + .into_iter() + .map(|field| Box::new(field) as Box) + .collect::>() + .into_boxed_slice(), + } + } +} + +impl IntoIterator for DynamicArray { + type Item = Box; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + Box::into_iter(self.values) + } +} + +impl<'a> IntoIterator for &'a DynamicArray { + type Item = &'a dyn Reflect; + type IntoIter = ArrayIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + impl_type_path!((in bevy_reflect) DynamicArray); impl_function_traits!(DynamicArray); /// An iterator over an [`Array`]. diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 2cd26d1f2e562..8d08bdaf803b6 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -1088,7 +1088,7 @@ mod tests { }); foo_patch.insert("g", composite); - let array = DynamicArray::from_values([2u32, 2u32]); + let array = DynamicArray::from_iter([2u32, 2u32]); foo_patch.insert("h", array); foo.apply(&foo_patch); diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index c73baab3299b9..c7ffd5ba37dd2 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -378,6 +378,27 @@ impl Debug for DynamicList { } } +impl FromIterator> for DynamicList { + fn from_iter>>(values: I) -> Self { + Self { + represented_type: None, + values: values.into_iter().collect(), + } + } +} + +impl FromIterator for DynamicList { + fn from_iter>(values: I) -> Self { + Self { + represented_type: None, + values: values + .into_iter() + .map(|field| Box::new(field) as Box) + .collect(), + } + } +} + impl IntoIterator for DynamicList { type Item = Box; type IntoIter = std::vec::IntoIter; @@ -387,6 +408,15 @@ impl IntoIterator for DynamicList { } } +impl<'a> IntoIterator for &'a DynamicList { + type Item = &'a dyn Reflect; + type IntoIter = ListIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + /// An iterator over an [`List`]. pub struct ListIter<'a> { list: &'a dyn List, diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index 844be9a2476be..1910325f25fe0 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -455,6 +455,26 @@ impl<'a> Iterator for MapIter<'a> { } } +impl FromIterator<(Box, Box)> for DynamicMap { + fn from_iter, Box)>>(items: I) -> Self { + let mut dynamic_map = Self::default(); + for (key, value) in items.into_iter() { + dynamic_map.insert_boxed(key, value); + } + dynamic_map + } +} + +impl FromIterator<(K, V)> for DynamicMap { + fn from_iter>(items: I) -> Self { + let mut map = Self::default(); + for (key, value) in items.into_iter() { + map.insert(key, value); + } + map + } +} + impl IntoIterator for DynamicMap { type Item = (Box, Box); type IntoIter = std::vec::IntoIter; @@ -464,6 +484,15 @@ impl IntoIterator for DynamicMap { } } +impl<'a> IntoIterator for &'a DynamicMap { + type Item = (&'a dyn Reflect, &'a dyn Reflect); + type IntoIter = MapIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.iter() + } +} + impl<'a> ExactSizeIterator for MapIter<'a> {} /// Compares a [`Map`] with a [`Reflect`] value. diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index 92cba76baa6aa..5551097c99942 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -508,6 +508,44 @@ impl Debug for DynamicStruct { } } +impl FromIterator<(String, Box)> for DynamicStruct { + fn from_iter)>>(fields: I) -> Self { + let mut dynamic_struct = Self::default(); + for (name, value) in fields.into_iter() { + dynamic_struct.insert_boxed(name, value); + } + dynamic_struct + } +} + +impl FromIterator<(String, V)> for DynamicStruct { + fn from_iter>(fields: I) -> Self { + let mut dynamic_struct = Self::default(); + for (name, value) in fields.into_iter() { + dynamic_struct.insert(name, value); + } + dynamic_struct + } +} + +impl IntoIterator for DynamicStruct { + type Item = Box; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.fields.into_iter() + } +} + +impl<'a> IntoIterator for &'a DynamicStruct { + type Item = &'a dyn Reflect; + type IntoIter = FieldIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_fields() + } +} + /// Compares a [`Struct`] with a [`Reflect`] value. /// /// Returns true if and only if all of the following are true: diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 4e96d50f20351..68e0aff1f43d2 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -391,6 +391,45 @@ impl Reflect for DynamicTuple { impl_type_path!((in bevy_reflect) DynamicTuple); +impl FromIterator> for DynamicTuple { + fn from_iter>>(fields: I) -> Self { + Self { + represented_type: None, + fields: fields.into_iter().collect(), + } + } +} + +impl FromIterator for DynamicTuple { + fn from_iter>(values: I) -> Self { + Self { + represented_type: None, + fields: values + .into_iter() + .map(|field| Box::new(field) as Box) + .collect(), + } + } +} + +impl IntoIterator for DynamicTuple { + type Item = Box; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.fields.into_iter() + } +} + +impl<'a> IntoIterator for &'a DynamicTuple { + type Item = &'a dyn Reflect; + type IntoIter = TupleFieldIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_fields() + } +} + /// Applies the elements of `b` to the corresponding elements of `a`. /// /// # Panics diff --git a/crates/bevy_reflect/src/tuple_struct.rs b/crates/bevy_reflect/src/tuple_struct.rs index 87c87fb3760de..d5839850af23e 100644 --- a/crates/bevy_reflect/src/tuple_struct.rs +++ b/crates/bevy_reflect/src/tuple_struct.rs @@ -426,6 +426,45 @@ impl From for DynamicTupleStruct { } } +impl FromIterator> for DynamicTupleStruct { + fn from_iter>>(fields: I) -> Self { + Self { + represented_type: None, + fields: fields.into_iter().collect(), + } + } +} + +impl FromIterator for DynamicTupleStruct { + fn from_iter>(fields: I) -> Self { + Self { + represented_type: None, + fields: fields + .into_iter() + .map(|field| Box::new(field) as Box) + .collect(), + } + } +} + +impl IntoIterator for DynamicTupleStruct { + type Item = Box; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.fields.into_iter() + } +} + +impl<'a> IntoIterator for &'a DynamicTupleStruct { + type Item = &'a dyn Reflect; + type IntoIter = TupleStructFieldIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_fields() + } +} + /// Compares a [`TupleStruct`] with a [`Reflect`] value. /// /// Returns true if and only if all of the following are true: diff --git a/examples/reflection/dynamic_types.rs b/examples/reflection/dynamic_types.rs index 6430f57296233..f0449e4fa8a76 100644 --- a/examples/reflection/dynamic_types.rs +++ b/examples/reflection/dynamic_types.rs @@ -167,7 +167,7 @@ fn main() { // 2. `DynamicArray` { - let dynamic_array = DynamicArray::from_values([1u32, 2u32, 3u32]); + let dynamic_array = DynamicArray::from_iter([1u32, 2u32, 3u32]); let mut my_array = [0u32; 3]; my_array.apply(&dynamic_array); From de5c14b482b083c9d0e6b4bac679f2aa31e5858a Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Tue, 9 Jul 2024 21:58:25 +0200 Subject: [PATCH 3/8] doc --- crates/bevy_reflect/src/struct_trait.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index 5551097c99942..a496583e62542 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -509,6 +509,8 @@ impl Debug for DynamicStruct { } impl FromIterator<(String, Box)> for DynamicStruct { + /// Create a dynmic struct that doesn't represent a type from the + /// field name, field value pairs. fn from_iter)>>(fields: I) -> Self { let mut dynamic_struct = Self::default(); for (name, value) in fields.into_iter() { @@ -519,6 +521,8 @@ impl FromIterator<(String, Box)> for DynamicStruct { } impl FromIterator<(String, V)> for DynamicStruct { + /// Create a dynmic struct that doesn't represent a type from the + /// field name, field value pairs. fn from_iter>(fields: I) -> Self { let mut dynamic_struct = Self::default(); for (name, value) in fields.into_iter() { From 7e19001951f9dc2e6f01708e0c13fa36163c5bd3 Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Tue, 9 Jul 2024 22:54:46 +0200 Subject: [PATCH 4/8] typo --- crates/bevy_reflect/src/array.rs | 2 +- crates/bevy_reflect/src/struct_trait.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index f29401dcfd972..10a8ef8e5509f 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -379,7 +379,7 @@ impl IntoIterator for DynamicArray { type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { - Box::into_iter(self.values) + Box::<[_]>::into_iter(self.values) } } diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index a496583e62542..1e950697fd929 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -509,7 +509,7 @@ impl Debug for DynamicStruct { } impl FromIterator<(String, Box)> for DynamicStruct { - /// Create a dynmic struct that doesn't represent a type from the + /// Create a dynamic struct that doesn't represent a type from the /// field name, field value pairs. fn from_iter)>>(fields: I) -> Self { let mut dynamic_struct = Self::default(); @@ -521,7 +521,7 @@ impl FromIterator<(String, Box)> for DynamicStruct { } impl FromIterator<(String, V)> for DynamicStruct { - /// Create a dynmic struct that doesn't represent a type from the + /// Create a dynamic struct that doesn't represent a type from the /// field name, field value pairs. fn from_iter>(fields: I) -> Self { let mut dynamic_struct = Self::default(); From 6aaad7153aad9e01bd8b2f7589c5675bcc14861a Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Wed, 10 Jul 2024 11:04:59 +0200 Subject: [PATCH 5/8] fix compile err; remove homogeneous fromiterator for heterogeneous types --- crates/bevy_reflect/src/array.rs | 2 +- crates/bevy_reflect/src/struct_trait.rs | 12 ------------ crates/bevy_reflect/src/tuple.rs | 12 ------------ crates/bevy_reflect/src/tuple_struct.rs | 12 ------------ 4 files changed, 1 insertion(+), 37 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 10a8ef8e5509f..a7bc60f5eb808 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -379,7 +379,7 @@ impl IntoIterator for DynamicArray { type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { - Box::<[_]>::into_iter(self.values) + self.values.into_vec().into_iter() } } diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index 1e950697fd929..0827cd9d55d39 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -520,18 +520,6 @@ impl FromIterator<(String, Box)> for DynamicStruct { } } -impl FromIterator<(String, V)> for DynamicStruct { - /// Create a dynamic struct that doesn't represent a type from the - /// field name, field value pairs. - fn from_iter>(fields: I) -> Self { - let mut dynamic_struct = Self::default(); - for (name, value) in fields.into_iter() { - dynamic_struct.insert(name, value); - } - dynamic_struct - } -} - impl IntoIterator for DynamicStruct { type Item = Box; type IntoIter = std::vec::IntoIter; diff --git a/crates/bevy_reflect/src/tuple.rs b/crates/bevy_reflect/src/tuple.rs index 68e0aff1f43d2..bc3438f32ef6c 100644 --- a/crates/bevy_reflect/src/tuple.rs +++ b/crates/bevy_reflect/src/tuple.rs @@ -400,18 +400,6 @@ impl FromIterator> for DynamicTuple { } } -impl FromIterator for DynamicTuple { - fn from_iter>(values: I) -> Self { - Self { - represented_type: None, - fields: values - .into_iter() - .map(|field| Box::new(field) as Box) - .collect(), - } - } -} - impl IntoIterator for DynamicTuple { type Item = Box; type IntoIter = std::vec::IntoIter; diff --git a/crates/bevy_reflect/src/tuple_struct.rs b/crates/bevy_reflect/src/tuple_struct.rs index d5839850af23e..098d0b682192a 100644 --- a/crates/bevy_reflect/src/tuple_struct.rs +++ b/crates/bevy_reflect/src/tuple_struct.rs @@ -435,18 +435,6 @@ impl FromIterator> for DynamicTupleStruct { } } -impl FromIterator for DynamicTupleStruct { - fn from_iter>(fields: I) -> Self { - Self { - represented_type: None, - fields: fields - .into_iter() - .map(|field| Box::new(field) as Box) - .collect(), - } - } -} - impl IntoIterator for DynamicTupleStruct { type Item = Box; type IntoIter = std::vec::IntoIter; From 735e62facf8178cb160dbe4a74300d39abf2dc8a Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Wed, 10 Jul 2024 18:24:29 +0200 Subject: [PATCH 6/8] moo --- crates/bevy_reflect/src/struct_trait.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index 0827cd9d55d39..b36bf3748807d 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -508,10 +508,13 @@ impl Debug for DynamicStruct { } } -impl FromIterator<(String, Box)> for DynamicStruct { +impl<'a, N> FromIterator<(N, Box)> for DynamicStruct +where + N: Into>, +{ /// Create a dynamic struct that doesn't represent a type from the /// field name, field value pairs. - fn from_iter)>>(fields: I) -> Self { + fn from_iter)>>(fields: I) -> Self { let mut dynamic_struct = Self::default(); for (name, value) in fields.into_iter() { dynamic_struct.insert_boxed(name, value); From 7438a0d1b5cc54b059c51867e104178d5ac0e673 Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Fri, 12 Jul 2024 19:38:06 +0200 Subject: [PATCH 7/8] style suggestions --- crates/bevy_reflect/src/array.rs | 12 ++++-------- crates/bevy_reflect/src/list.rs | 11 ++++------- crates/bevy_reflect/src/map.rs | 6 +++--- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index a7bc60f5eb808..f94029bf9c0fa 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -363,14 +363,10 @@ impl FromIterator> for DynamicArray { impl FromIterator for DynamicArray { fn from_iter>(values: I) -> Self { - Self { - represented_type: None, - values: values - .into_iter() - .map(|field| Box::new(field) as Box) - .collect::>() - .into_boxed_slice(), - } + values + .into_iter() + .map(|value| Box::new(value).into_reflect()) + .collect() } } diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index c7ffd5ba37dd2..e1b91316b5092 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -389,13 +389,10 @@ impl FromIterator> for DynamicList { impl FromIterator for DynamicList { fn from_iter>(values: I) -> Self { - Self { - represented_type: None, - values: values - .into_iter() - .map(|field| Box::new(field) as Box) - .collect(), - } + values + .into_iter() + .map(|field| Box::new(field).into_reflect()) + .collect() } } diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index 1910325f25fe0..c22b39307639a 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -457,11 +457,11 @@ impl<'a> Iterator for MapIter<'a> { impl FromIterator<(Box, Box)> for DynamicMap { fn from_iter, Box)>>(items: I) -> Self { - let mut dynamic_map = Self::default(); + let mut map = Self::default(); for (key, value) in items.into_iter() { - dynamic_map.insert_boxed(key, value); + map.insert_boxed(key, value); } - dynamic_map + map } } From 7584e5c267ab4a8dda86d84f414a209f3d8f027f Mon Sep 17 00:00:00 2001 From: Vincent Junge Date: Sun, 14 Jul 2024 23:00:31 +0200 Subject: [PATCH 8/8] consistent from_iter in example --- examples/reflection/dynamic_types.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/examples/reflection/dynamic_types.rs b/examples/reflection/dynamic_types.rs index f0449e4fa8a76..420481e8df2f6 100644 --- a/examples/reflection/dynamic_types.rs +++ b/examples/reflection/dynamic_types.rs @@ -135,10 +135,7 @@ fn main() { // Lastly, while dynamic types are commonly generated via reflection methods like // `Reflect::clone_value` or via the reflection deserializers, // you can also construct them manually. - let mut my_dynamic_list = DynamicList::default(); - my_dynamic_list.push(1u32); - my_dynamic_list.push(2u32); - my_dynamic_list.push(3u32); + let mut my_dynamic_list = DynamicList::from_iter([1u32, 2u32, 3u32]); // This is useful when you just need to apply some subset of changes to a type. let mut my_list: Vec = Vec::new(); @@ -176,10 +173,7 @@ fn main() { // 3. `DynamicList` { - let mut dynamic_list = DynamicList::default(); - dynamic_list.push(1u32); - dynamic_list.push(2u32); - dynamic_list.push(3u32); + let dynamic_list = DynamicList::from_iter([1u32, 2u32, 3u32]); let mut my_list: Vec = Vec::new(); my_list.apply(&dynamic_list); @@ -188,10 +182,7 @@ fn main() { // 4. `DynamicMap` { - let mut dynamic_map = DynamicMap::default(); - dynamic_map.insert("x", 1u32); - dynamic_map.insert("y", 2u32); - dynamic_map.insert("z", 3u32); + let dynamic_map = DynamicMap::from_iter([("x", 1u32), ("y", 2u32), ("z", 3u32)]); let mut my_map: HashMap<&str, u32> = HashMap::new(); my_map.apply(&dynamic_map);