Skip to content

Commit 7161e9c

Browse files
authored
Regression fix: Reintroduce sorting/reordering methods on Children (#18476)
# Objective Bevy 0.15 used to have methods on `Children` for sorting and reordering them. This is very important, because in certain situations, the order of children matters. For example, in the context of UI nodes. These methods are missing/omitted/forgotten in the current version, after the Relationships rework. Without them, it is impossible for me to upgrade `iyes_perf_ui` to Bevy 0.16. ## Solution Reintroduce the methods. This PR simply copy-pastes them from Bevy 0.15.
1 parent 8d9f948 commit 7161e9c

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

crates/bevy_ecs/src/hierarchy.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,96 @@ impl FromWorld for ChildOf {
139139
#[doc(alias = "IsParent")]
140140
pub struct Children(Vec<Entity>);
141141

142+
impl Children {
143+
/// Swaps the child at `a_index` with the child at `b_index`.
144+
#[inline]
145+
pub fn swap(&mut self, a_index: usize, b_index: usize) {
146+
self.0.swap(a_index, b_index);
147+
}
148+
149+
/// Sorts children [stably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
150+
/// in place using the provided comparator function.
151+
///
152+
/// For the underlying implementation, see [`slice::sort_by`].
153+
///
154+
/// For the unstable version, see [`sort_unstable_by`](Children::sort_unstable_by).
155+
///
156+
/// See also [`sort_by_key`](Children::sort_by_key), [`sort_by_cached_key`](Children::sort_by_cached_key).
157+
#[inline]
158+
pub fn sort_by<F>(&mut self, compare: F)
159+
where
160+
F: FnMut(&Entity, &Entity) -> core::cmp::Ordering,
161+
{
162+
self.0.sort_by(compare);
163+
}
164+
165+
/// Sorts children [stably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
166+
/// in place using the provided key extraction function.
167+
///
168+
/// For the underlying implementation, see [`slice::sort_by_key`].
169+
///
170+
/// For the unstable version, see [`sort_unstable_by_key`](Children::sort_unstable_by_key).
171+
///
172+
/// See also [`sort_by`](Children::sort_by), [`sort_by_cached_key`](Children::sort_by_cached_key).
173+
#[inline]
174+
pub fn sort_by_key<K, F>(&mut self, compare: F)
175+
where
176+
F: FnMut(&Entity) -> K,
177+
K: Ord,
178+
{
179+
self.0.sort_by_key(compare);
180+
}
181+
182+
/// Sorts children [stably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
183+
/// in place using the provided key extraction function. Only evaluates each key at most
184+
/// once per sort, caching the intermediate results in memory.
185+
///
186+
/// For the underlying implementation, see [`slice::sort_by_cached_key`].
187+
///
188+
/// See also [`sort_by`](Children::sort_by), [`sort_by_key`](Children::sort_by_key).
189+
#[inline]
190+
pub fn sort_by_cached_key<K, F>(&mut self, compare: F)
191+
where
192+
F: FnMut(&Entity) -> K,
193+
K: Ord,
194+
{
195+
self.0.sort_by_cached_key(compare);
196+
}
197+
198+
/// Sorts children [unstably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
199+
/// in place using the provided comparator function.
200+
///
201+
/// For the underlying implementation, see [`slice::sort_unstable_by`].
202+
///
203+
/// For the stable version, see [`sort_by`](Children::sort_by).
204+
///
205+
/// See also [`sort_unstable_by_key`](Children::sort_unstable_by_key).
206+
#[inline]
207+
pub fn sort_unstable_by<F>(&mut self, compare: F)
208+
where
209+
F: FnMut(&Entity, &Entity) -> core::cmp::Ordering,
210+
{
211+
self.0.sort_unstable_by(compare);
212+
}
213+
214+
/// Sorts children [unstably](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability)
215+
/// in place using the provided key extraction function.
216+
///
217+
/// For the underlying implementation, see [`slice::sort_unstable_by_key`].
218+
///
219+
/// For the stable version, see [`sort_by_key`](Children::sort_by_key).
220+
///
221+
/// See also [`sort_unstable_by`](Children::sort_unstable_by).
222+
#[inline]
223+
pub fn sort_unstable_by_key<K, F>(&mut self, compare: F)
224+
where
225+
F: FnMut(&Entity) -> K,
226+
K: Ord,
227+
{
228+
self.0.sort_unstable_by_key(compare);
229+
}
230+
}
231+
142232
impl<'a> IntoIterator for &'a Children {
143233
type Item = <Self::IntoIter as Iterator>::Item;
144234

0 commit comments

Comments
 (0)