Skip to content

Commit fa5ca4c

Browse files
NathanSWardcart
authored andcommitted
Add pop method for List trait. (bevyengine#5797)
# Objective - The reflection `List` trait does not have a `pop` function. - Popping elements off a list is a common use case and is almost always supported by `List`-like types. ## Solution - Add the `pop()` method to the `List` trait and add the appropriate implementations of this function. ## Migration Guide - Any custom type that implements the `List` trait will now need to implement the `pop` method. Co-authored-by: Carter Anderson <mcanders1@gmail.com>
1 parent 0ce0ff7 commit fa5ca4c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

crates/bevy_reflect/src/impls/smallvec.rs

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ where
5454
});
5555
SmallVec::push(self, value);
5656
}
57+
58+
fn pop(&mut self) -> Option<Box<dyn Reflect>> {
59+
self.pop().map(|value| Box::new(value) as Box<dyn Reflect>)
60+
}
5761
}
5862

5963
impl<T: smallvec::Array + Send + Sync + 'static> Reflect for SmallVec<T>

crates/bevy_reflect/src/impls/std.rs

+4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ impl<T: FromReflect> List for Vec<T> {
137137
});
138138
Vec::push(self, value);
139139
}
140+
141+
fn pop(&mut self) -> Option<Box<dyn Reflect>> {
142+
self.pop().map(|value| Box::new(value) as Box<dyn Reflect>)
143+
}
140144
}
141145

142146
impl<T: FromReflect> Reflect for Vec<T> {

crates/bevy_reflect/src/list.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ pub trait List: Reflect + Array {
1515
/// Appends an element to the list.
1616
fn push(&mut self, value: Box<dyn Reflect>);
1717

18+
/// Removes the last element from the list (highest index in the array) and returns it, or [`None`] if it is empty.
19+
fn pop(&mut self) -> Option<Box<dyn Reflect>>;
20+
1821
/// Clones the list, producing a [`DynamicList`].
1922
fn clone_dynamic(&self) -> DynamicList {
2023
DynamicList {
@@ -151,6 +154,10 @@ impl List for DynamicList {
151154
DynamicList::push_box(self, value);
152155
}
153156

157+
fn pop(&mut self) -> Option<Box<dyn Reflect>> {
158+
self.values.pop()
159+
}
160+
154161
fn clone_dynamic(&self) -> DynamicList {
155162
DynamicList {
156163
name: self.name.clone(),

0 commit comments

Comments
 (0)