bevy_reflect

Trait Map

Source
pub trait Map: Reflect {
    // Required methods
    fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect>;
    fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect>;
    fn get_at(&self, index: usize) -> Option<(&dyn Reflect, &dyn Reflect)>;
    fn get_at_mut(
        &mut self,
        index: usize,
    ) -> Option<(&dyn Reflect, &mut dyn Reflect)>;
    fn len(&self) -> usize;
    fn iter(&self) -> MapIter<'_> ;
    fn drain(self: Box<Self>) -> Vec<(Box<dyn Reflect>, Box<dyn Reflect>)>;
    fn clone_dynamic(&self) -> DynamicMap;
    fn insert_boxed(
        &mut self,
        key: Box<dyn Reflect>,
        value: Box<dyn Reflect>,
    ) -> Option<Box<dyn Reflect>>;
    fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>>;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

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

Maps contain zero or more entries of a key and its associated value, and correspond to types like HashMap. The order of these entries is not guaranteed by this trait.

§Hashing

All keys are expected to return a valid hash value from Reflect::reflect_hash. If using the #[derive(Reflect)] macro, this can be done by adding #[reflect(Hash)] to the entire struct or enum. This is true even for manual implementors who do not use the hashed value, as it is still relied on by DynamicMap.

§Example

use bevy_reflect::{Reflect, Map};
use bevy_utils::HashMap;


let foo: &mut dyn Map = &mut HashMap::<u32, bool>::new();
foo.insert_boxed(Box::new(123_u32), Box::new(true));
assert_eq!(foo.len(), 1);

let field: &dyn Reflect = foo.get(&123_u32).unwrap();
assert_eq!(field.downcast_ref::<bool>(), Some(&true));

Required Methods§

Source

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

Returns a reference to the value associated with the given key.

If no value is associated with key, returns None.

Source

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

Returns a mutable reference to the value associated with the given key.

If no value is associated with key, returns None.

Source

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

Returns the key-value pair at index by reference, or None if out of bounds.

Source

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

Returns the key-value pair at index by reference where the value is a mutable reference, or None if out of bounds.

Source

fn len(&self) -> usize

Returns the number of elements in the map.

Source

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

Returns an iterator over the key-value pairs of the map.

Source

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

Drain the key-value pairs of this map to get a vector of owned values.

Source

fn clone_dynamic(&self) -> DynamicMap

Clones the map, producing a DynamicMap.

Source

fn insert_boxed( &mut self, key: Box<dyn Reflect>, value: Box<dyn Reflect>, ) -> Option<Box<dyn Reflect>>

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned. If the map did have this key present, the value is updated, and the old value is returned.

Source

fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>>

Removes an entry from the map.

If the map did not have this key present, None is returned. If the map did have this key present, the removed value is returned.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the list contains no elements.

Implementations on Foreign Types§

Source§

impl<K, V> Map for BTreeMap<K, V>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn len(&self) -> usize

Source§

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

Source§

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

Source§

fn clone_dynamic(&self) -> DynamicMap

Source§

fn insert_boxed( &mut self, key: Box<dyn Reflect>, value: Box<dyn Reflect>, ) -> Option<Box<dyn Reflect>>

Source§

fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>>

Source§

impl<K, V, S> Map for HashMap<K, V, S>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn len(&self) -> usize

Source§

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

Source§

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

Source§

fn clone_dynamic(&self) -> DynamicMap

Source§

fn insert_boxed( &mut self, key: Box<dyn Reflect>, value: Box<dyn Reflect>, ) -> Option<Box<dyn Reflect>>

Source§

fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>>

Source§

impl<K, V, S> Map for HashMap<K, V, S>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

fn len(&self) -> usize

Source§

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

Source§

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

Source§

fn clone_dynamic(&self) -> DynamicMap

Source§

fn insert_boxed( &mut self, key: Box<dyn Reflect>, value: Box<dyn Reflect>, ) -> Option<Box<dyn Reflect>>

Source§

fn remove(&mut self, key: &dyn Reflect) -> Option<Box<dyn Reflect>>

Implementors§