bevy_reflect

Trait List

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

    // Provided methods
    fn push(&mut self, value: Box<dyn PartialReflect>) { ... }
    fn pop(&mut self) -> Option<Box<dyn PartialReflect>> { ... }
    fn is_empty(&self) -> bool { ... }
    fn clone_dynamic(&self) -> DynamicList { ... }
    fn get_represented_list_info(&self) -> Option<&'static ListInfo> { ... }
}
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::{PartialReflect, 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 PartialReflect> = foo.pop().unwrap();
assert_eq!(last_field.try_downcast_ref::<u32>(), Some(&789));

Required Methods§

source

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

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

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 PartialReflect>)

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

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(&mut self) -> Vec<Box<dyn PartialReflect>>

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

After calling this function, self will be empty. The order of items in the returned Vec will match the order of items in self.

Provided Methods§

source

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

Appends an element to the back of the list.

source

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

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.

source

fn get_represented_list_info(&self) -> Option<&'static ListInfo>

Will return None if TypeInfo is not available.

Implementations on Foreign Types§

source§

impl<T: SmallArray + TypePath + Send + Sync> List for SmallVec<T>
where T::Item: FromReflect + MaybeTyped + TypePath,

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

fn drain(&mut self) -> Vec<Box<dyn PartialReflect>>

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

fn drain(&mut self) -> Vec<Box<dyn PartialReflect>>

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

fn drain(&mut self) -> Vec<Box<dyn PartialReflect>>

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

fn drain(&mut self) -> Vec<Box<dyn PartialReflect>>

Implementors§