Add pop method for List trait. (#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>
This commit is contained in:
Nathan Ward 2022-08-30 21:06:32 +00:00
parent e9661bea1a
commit bb2303a654
3 changed files with 15 additions and 0 deletions

View file

@ -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>

View file

@ -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> {

View file

@ -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 {
@ -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(),