bevy_reflect

Trait Map

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

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn get_represented_map_info(&self) -> Option<&'static MapInfo> { ... }
}
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 and BTreeMap. The order of these entries is not guaranteed by this trait.

§Hashing and equality

All keys are expected to return a valid hash value from PartialReflect::reflect_hash and be comparable using PartialReflect::reflect_partial_eq. If using the #[derive(Reflect)] macro, this can be done by adding #[reflect(Hash, PartialEq)] to the entire struct or enum. The ordering is expected to be total, that is as if the reflected type implements the Eq trait. This is true even for manual implementors who do not hash or compare values, as it is still relied on by DynamicMap.

§Example

use bevy_reflect::{PartialReflect, 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 PartialReflect = foo.get(&123_u32).unwrap();
assert_eq!(field.try_downcast_ref::<bool>(), Some(&true));

Required Methods§

source

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

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 PartialReflect, ) -> Option<&mut dyn PartialReflect>

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 PartialReflect, &dyn PartialReflect)>

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 PartialReflect, &mut dyn PartialReflect)>

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

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

After calling this function, self will be empty.

source

fn clone_dynamic(&self) -> DynamicMap

Clones the map, producing a DynamicMap.

source

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

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 PartialReflect, ) -> Option<Box<dyn PartialReflect>>

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.

source

fn get_represented_map_info(&self) -> Option<&'static MapInfo>

Will return None if TypeInfo is not available.

Implementations on Foreign Types§

source§

impl<K, V> Map for BTreeMap<K, V>
where K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord, V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

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

source§

fn clone_dynamic(&self) -> DynamicMap

source§

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

source§

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

source§

impl<K, V, S> Map for HashMap<K, V, S>
where K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash, V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration, S: TypePath + BuildHasher + Send + Sync,

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

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

source§

fn clone_dynamic(&self) -> DynamicMap

source§

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

source§

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

source§

impl<K, V, S> Map for HashMap<K, V, S>
where K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash, V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration, S: TypePath + BuildHasher + Send + Sync,

source§

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

source§

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

source§

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

source§

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

source§

fn len(&self) -> usize

source§

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

source§

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

source§

fn clone_dynamic(&self) -> DynamicMap

source§

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

source§

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

Implementors§