bevy_reflect

Trait List

Source
pub trait List: Reflect {
    // Required methods
    fn get(&self, index: usize) -> Option<&dyn Reflect>;
    fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>;
    fn insert(&mut self, index: usize, element: Box<dyn Reflect>);
    fn remove(&mut self, index: usize) -> Box<dyn Reflect>;
    fn len(&self) -> usize;
    fn iter(&self) -> ListIter<'_> ;
    fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>;

    // Provided methods
    fn push(&mut self, value: Box<dyn Reflect>) { ... }
    fn pop(&mut self) -> Option<Box<dyn Reflect>> { ... }
    fn is_empty(&self) -> bool { ... }
    fn clone_dynamic(&self) -> DynamicList { ... }
}
Expand description

A trait used to power list-like operations via reflection.

This corresponds to types, like Vec, which contain an ordered sequence of elements that implement Reflect.

Unlike the Array trait, implementors of this trait are not expected to maintain a constant length. Methods like insertion and removal explicitly allow for their internal size to change.

push and pop have default implementations, however it will generally be more performant to implement them manually as the default implementation uses a very naive approach to find the correct position.

This trait expects its elements to be ordered linearly from front to back. The front element starts at index 0 with the back element ending at the largest index. This contract above should be upheld by any manual implementors.

Due to the type-erasing nature of the reflection API as a whole, this trait does not make any guarantees that the implementor’s elements are homogeneous (i.e. all the same type).

§Example

use bevy_reflect::{Reflect, List};

let foo: &mut dyn List = &mut vec![123_u32, 456_u32, 789_u32];
assert_eq!(foo.len(), 3);

let last_field: Box<dyn Reflect> = foo.pop().unwrap();
assert_eq!(last_field.downcast_ref::<u32>(), Some(&789));

Required Methods§

Source

fn get(&self, index: usize) -> Option<&dyn Reflect>

Returns a reference to the element at index, or None if out of bounds.

Source

fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>

Returns a mutable reference to the element at index, or None if out of bounds.

Source

fn insert(&mut self, index: usize, element: Box<dyn Reflect>)

Inserts an element at position index within the list, shifting all elements after it towards the back of the list.

§Panics

Panics if index > len.

Source

fn remove(&mut self, index: usize) -> Box<dyn Reflect>

Removes and returns the element at position index within the list, shifting all elements before it towards the front of the list.

§Panics

Panics if index is out of bounds.

Source

fn len(&self) -> usize

Returns the number of elements in the list.

Source

fn iter(&self) -> ListIter<'_>

Returns an iterator over the list.

Source

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>

Drain the elements of this list to get a vector of owned values.

Provided Methods§

Source

fn push(&mut self, value: Box<dyn Reflect>)

Appends an element to the back of the list.

Source

fn pop(&mut self) -> Option<Box<dyn Reflect>>

Removes the back element from the list and returns it, or None if it is empty.

Source

fn is_empty(&self) -> bool

Returns true if the collection contains no elements.

Source

fn clone_dynamic(&self) -> DynamicList

Clones the list, producing a DynamicList.

Implementations on Foreign Types§

Source§

impl<T: Array + TypePath + Send + Sync> List for SmallVec<T>

Available on crate feature smallvec only.
Source§

fn get(&self, index: usize) -> Option<&dyn Reflect>

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>

Source§

fn insert(&mut self, index: usize, value: Box<dyn Reflect>)

Source§

fn remove(&mut self, index: usize) -> Box<dyn Reflect>

Source§

fn push(&mut self, value: Box<dyn Reflect>)

Source§

fn pop(&mut self) -> Option<Box<dyn Reflect>>

Source§

fn len(&self) -> usize

Source§

fn iter(&self) -> ListIter<'_>

Source§

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>

Source§

impl<T: FromReflect + Clone + TypePath + GetTypeRegistration> List for Cow<'static, [T]>

Source§

fn get(&self, index: usize) -> Option<&dyn Reflect>

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>

Source§

fn insert(&mut self, index: usize, element: Box<dyn Reflect>)

Source§

fn remove(&mut self, index: usize) -> Box<dyn Reflect>

Source§

fn push(&mut self, value: Box<dyn Reflect>)

Source§

fn pop(&mut self) -> Option<Box<dyn Reflect>>

Source§

fn len(&self) -> usize

Source§

fn iter(&self) -> ListIter<'_>

Source§

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>

Source§

impl<T: FromReflect + TypePath + GetTypeRegistration> List for VecDeque<T>

Source§

fn get(&self, index: usize) -> Option<&dyn Reflect>

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>

Source§

fn insert(&mut self, index: usize, value: Box<dyn Reflect>)

Source§

fn remove(&mut self, index: usize) -> Box<dyn Reflect>

Source§

fn push(&mut self, value: Box<dyn Reflect>)

Source§

fn pop(&mut self) -> Option<Box<dyn Reflect>>

Source§

fn len(&self) -> usize

Source§

fn iter(&self) -> ListIter<'_>

Source§

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>

Source§

impl<T: FromReflect + TypePath + GetTypeRegistration> List for Vec<T>

Source§

fn get(&self, index: usize) -> Option<&dyn Reflect>

Source§

fn get_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>

Source§

fn insert(&mut self, index: usize, value: Box<dyn Reflect>)

Source§

fn remove(&mut self, index: usize) -> Box<dyn Reflect>

Source§

fn push(&mut self, value: Box<dyn Reflect>)

Source§

fn pop(&mut self) -> Option<Box<dyn Reflect>>

Source§

fn len(&self) -> usize

Source§

fn iter(&self) -> ListIter<'_>

Source§

fn drain(self: Box<Self>) -> Vec<Box<dyn Reflect>>

Implementors§