Skip to content

Commit

Permalink
Add pop method for List trait. (bevyengine#5797)
Browse files Browse the repository at this point in the history
# 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 <[email protected]>
  • Loading branch information
2 people authored and ItsDoot committed Feb 1, 2023
1 parent 0ce0ff7 commit fa5ca4c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/impls/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ where
});
SmallVec::push(self, value);
}

fn pop(&mut self) -> Option<Box<dyn Reflect>> {
self.pop().map(|value| Box::new(value) as Box<dyn Reflect>)
}
}

impl<T: smallvec::Array + Send + Sync + 'static> Reflect for SmallVec<T>
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ impl<T: FromReflect> List for Vec<T> {
});
Vec::push(self, value);
}

fn pop(&mut self) -> Option<Box<dyn Reflect>> {
self.pop().map(|value| Box::new(value) as Box<dyn Reflect>)
}
}

impl<T: FromReflect> Reflect for Vec<T> {
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub trait List: Reflect + Array {
/// Appends an element to the list.
fn push(&mut self, value: Box<dyn Reflect>);

/// Removes the last element from the list (highest index in the array) and returns it, or [`None`] if it is empty.
fn pop(&mut self) -> Option<Box<dyn Reflect>>;

/// Clones the list, producing a [`DynamicList`].
fn clone_dynamic(&self) -> DynamicList {
DynamicList {
Expand Down Expand Up @@ -151,6 +154,10 @@ impl List for DynamicList {
DynamicList::push_box(self, value);
}

fn pop(&mut self) -> Option<Box<dyn Reflect>> {
self.values.pop()
}

fn clone_dynamic(&self) -> DynamicList {
DynamicList {
name: self.name.clone(),
Expand Down

0 comments on commit fa5ca4c

Please sign in to comment.