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§
sourcefn get(&self, key: &dyn PartialReflect) -> Option<&dyn PartialReflect>
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
.
sourcefn get_mut(
&mut self,
key: &dyn PartialReflect,
) -> Option<&mut dyn PartialReflect>
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
.
sourcefn get_at(
&self,
index: usize,
) -> Option<(&dyn PartialReflect, &dyn PartialReflect)>
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.
sourcefn get_at_mut(
&mut self,
index: usize,
) -> Option<(&dyn PartialReflect, &mut dyn PartialReflect)>
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.
sourcefn drain(&mut self) -> Vec<(Box<dyn PartialReflect>, Box<dyn PartialReflect>)>
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.
sourcefn clone_dynamic(&self) -> DynamicMap
fn clone_dynamic(&self) -> DynamicMap
Clones the map, producing a DynamicMap
.
sourcefn insert_boxed(
&mut self,
key: Box<dyn PartialReflect>,
value: Box<dyn PartialReflect>,
) -> Option<Box<dyn PartialReflect>>
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.
sourcefn remove(
&mut self,
key: &dyn PartialReflect,
) -> Option<Box<dyn PartialReflect>>
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§
sourcefn get_represented_map_info(&self) -> Option<&'static MapInfo>
fn get_represented_map_info(&self) -> Option<&'static MapInfo>
Will return None
if TypeInfo
is not available.