From 1d491541d8fcd71d97ffe8b35ee91a9d45891fc2 Mon Sep 17 00:00:00 2001 From: Elbert Ronnie Date: Wed, 1 Feb 2023 02:29:52 +0530 Subject: [PATCH 1/3] Add constructor `new` to `ArrayIter` --- crates/bevy_reflect/src/array.rs | 19 +++++++++++++------ crates/bevy_reflect/src/impls/smallvec.rs | 5 +---- crates/bevy_reflect/src/impls/std.rs | 10 ++-------- crates/bevy_reflect/src/list.rs | 5 +---- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 438a135c040a3..0418d41bd907a 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -270,10 +270,7 @@ impl Array for DynamicArray { #[inline] fn iter(&self) -> ArrayIter { - ArrayIter { - array: self, - index: 0, - } + ArrayIter::new(self, 0) } #[inline] @@ -303,8 +300,18 @@ impl Typed for DynamicArray { /// An iterator over an [`Array`]. pub struct ArrayIter<'a> { - pub(crate) array: &'a dyn Array, - pub(crate) index: usize, + array: &'a dyn Array, + index: usize, +} + +impl<'a> ArrayIter<'a> { + /// Creates a new [`ArrayIter`]. + /// + /// It is an [`Iterator`] for [`Array`] whose elements start from `index`. + #[inline] + pub fn new(array: &'a dyn Array, index: usize) -> ArrayIter { + ArrayIter { array, index } + } } impl<'a> Iterator for ArrayIter<'a> { diff --git a/crates/bevy_reflect/src/impls/smallvec.rs b/crates/bevy_reflect/src/impls/smallvec.rs index 4a4c64e4ff9dd..6204c87ed89e1 100644 --- a/crates/bevy_reflect/src/impls/smallvec.rs +++ b/crates/bevy_reflect/src/impls/smallvec.rs @@ -32,10 +32,7 @@ where } fn iter(&self) -> ArrayIter { - ArrayIter { - array: self, - index: 0, - } + ArrayIter::new(self, 0) } fn drain(self: Box) -> Vec> { diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 9b7bbf8ff77fa..872e19963dbc7 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -198,10 +198,7 @@ macro_rules! impl_reflect_for_veclike { #[inline] fn iter(&self) -> ArrayIter { - ArrayIter { - array: self, - index: 0, - } + ArrayIter::new(self, 0) } #[inline] @@ -552,10 +549,7 @@ impl Array for [T; N] { #[inline] fn iter(&self) -> ArrayIter { - ArrayIter { - array: self, - index: 0, - } + ArrayIter::new(self, 0) } #[inline] diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index 2d67a62f24140..6fe074227cbbf 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -176,10 +176,7 @@ impl Array for DynamicList { } fn iter(&self) -> ArrayIter { - ArrayIter { - array: self, - index: 0, - } + ArrayIter::new(self, 0) } fn drain(self: Box) -> Vec> { From 92d7552171bb1c5cbd67d7277a324b15ff3fb58c Mon Sep 17 00:00:00 2001 From: Elbert Ronnie Date: Wed, 1 Feb 2023 03:16:25 +0530 Subject: [PATCH 2/3] Add `const` and hardcode `index` to be 0 --- crates/bevy_reflect/src/array.rs | 6 +++--- crates/bevy_reflect/src/impls/smallvec.rs | 2 +- crates/bevy_reflect/src/impls/std.rs | 4 ++-- crates/bevy_reflect/src/list.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 0418d41bd907a..23b47c23c424d 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -270,7 +270,7 @@ impl Array for DynamicArray { #[inline] fn iter(&self) -> ArrayIter { - ArrayIter::new(self, 0) + ArrayIter::new(self) } #[inline] @@ -309,8 +309,8 @@ impl<'a> ArrayIter<'a> { /// /// It is an [`Iterator`] for [`Array`] whose elements start from `index`. #[inline] - pub fn new(array: &'a dyn Array, index: usize) -> ArrayIter { - ArrayIter { array, index } + pub const fn new(array: &'a dyn Array) -> ArrayIter { + ArrayIter { array, index: 0 } } } diff --git a/crates/bevy_reflect/src/impls/smallvec.rs b/crates/bevy_reflect/src/impls/smallvec.rs index 6204c87ed89e1..c911653bd695c 100644 --- a/crates/bevy_reflect/src/impls/smallvec.rs +++ b/crates/bevy_reflect/src/impls/smallvec.rs @@ -32,7 +32,7 @@ where } fn iter(&self) -> ArrayIter { - ArrayIter::new(self, 0) + ArrayIter::new(self) } fn drain(self: Box) -> Vec> { diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 872e19963dbc7..7e53a46a7ac53 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -198,7 +198,7 @@ macro_rules! impl_reflect_for_veclike { #[inline] fn iter(&self) -> ArrayIter { - ArrayIter::new(self, 0) + ArrayIter::new(self) } #[inline] @@ -549,7 +549,7 @@ impl Array for [T; N] { #[inline] fn iter(&self) -> ArrayIter { - ArrayIter::new(self, 0) + ArrayIter::new(self) } #[inline] diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index 6fe074227cbbf..4940cb4828701 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -176,7 +176,7 @@ impl Array for DynamicList { } fn iter(&self) -> ArrayIter { - ArrayIter::new(self, 0) + ArrayIter::new(self) } fn drain(self: Box) -> Vec> { From 5f2fcc7ee2b3a440356717c301f3de53a367d8d0 Mon Sep 17 00:00:00 2001 From: Elbert Ronnie <103196773+elbertronnie@users.noreply.github.com> Date: Wed, 1 Feb 2023 03:57:57 +0530 Subject: [PATCH 3/3] Remove extra docs Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> --- crates/bevy_reflect/src/array.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/bevy_reflect/src/array.rs b/crates/bevy_reflect/src/array.rs index 23b47c23c424d..005e6f6b7e497 100644 --- a/crates/bevy_reflect/src/array.rs +++ b/crates/bevy_reflect/src/array.rs @@ -306,8 +306,6 @@ pub struct ArrayIter<'a> { impl<'a> ArrayIter<'a> { /// Creates a new [`ArrayIter`]. - /// - /// It is an [`Iterator`] for [`Array`] whose elements start from `index`. #[inline] pub const fn new(array: &'a dyn Array) -> ArrayIter { ArrayIter { array, index: 0 }