Path: blob/main/crates/bevy_reflect/src/impls/indexmap.rs
9341 views
use crate::{1set::{Set, SetInfo},2utility::GenericTypeInfoCell,3FromReflect, FromType, Generics, GetTypeRegistration, PartialReflect, Reflect,4ReflectCloneError, ReflectFromPtr, ReflectMut, ReflectOwned, ReflectRef, TypeInfo,5TypeParamInfo, TypePath, TypeRegistration,6};7use bevy_platform::prelude::{Box, Vec};8use bevy_reflect::{9map::{DynamicMap, Map, MapInfo},10MaybeTyped, ReflectFromReflect, ReflectKind, TypeRegistry, Typed,11};12use bevy_reflect_derive::impl_type_path;13use core::{any::Any, hash::BuildHasher, hash::Hash};14use indexmap::{IndexMap, IndexSet};1516impl<K, V, S> Map for IndexMap<K, V, S>17where18K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,19V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,20S: TypePath + BuildHasher + Default + Send + Sync,21{22fn get(&self, key: &dyn PartialReflect) -> Option<&dyn PartialReflect> {23key.try_downcast_ref::<K>()24.and_then(|key| Self::get(self, key))25.map(|value| value as &dyn PartialReflect)26}2728fn get_mut(&mut self, key: &dyn PartialReflect) -> Option<&mut dyn PartialReflect> {29key.try_downcast_ref::<K>()30.and_then(move |key| Self::get_mut(self, key))31.map(|value| value as &mut dyn PartialReflect)32}3334fn len(&self) -> usize {35Self::len(self)36}3738fn iter(&self) -> Box<dyn Iterator<Item = (&dyn PartialReflect, &dyn PartialReflect)> + '_> {39Box::new(40self.iter()41.map(|(k, v)| (k as &dyn PartialReflect, v as &dyn PartialReflect)),42)43}4445fn drain(&mut self) -> Vec<(Box<dyn PartialReflect>, Box<dyn PartialReflect>)> {46self.drain(..)47.map(|(key, value)| {48(49Box::new(key) as Box<dyn PartialReflect>,50Box::new(value) as Box<dyn PartialReflect>,51)52})53.collect()54}5556fn retain(&mut self, f: &mut dyn FnMut(&dyn PartialReflect, &mut dyn PartialReflect) -> bool) {57self.retain(move |key, value| f(key, value));58}5960fn to_dynamic_map(&self) -> DynamicMap {61let mut dynamic_map = DynamicMap::default();62dynamic_map.set_represented_type(PartialReflect::get_represented_type_info(self));63for (k, v) in self {64let key = K::from_reflect(k).unwrap_or_else(|| {65panic!(66"Attempted to clone invalid key of type {}.",67k.reflect_type_path()68)69});70dynamic_map.insert_boxed(Box::new(key), v.to_dynamic());71}72dynamic_map73}7475fn insert_boxed(76&mut self,77key: Box<dyn PartialReflect>,78value: Box<dyn PartialReflect>,79) -> Option<Box<dyn PartialReflect>> {80let key = K::take_from_reflect(key).unwrap_or_else(|key| {81panic!(82"Attempted to insert invalid key of type {}.",83key.reflect_type_path()84)85});86let value = V::take_from_reflect(value).unwrap_or_else(|value| {87panic!(88"Attempted to insert invalid value of type {}.",89value.reflect_type_path()90)91});92self.insert(key, value)93.map(|old_value| Box::new(old_value) as Box<dyn PartialReflect>)94}9596fn remove(&mut self, key: &dyn PartialReflect) -> Option<Box<dyn PartialReflect>> {97let mut from_reflect = None;98key.try_downcast_ref::<K>()99.or_else(|| {100from_reflect = K::from_reflect(key);101from_reflect.as_ref()102})103.and_then(|key| self.shift_remove(key))104.map(|value| Box::new(value) as Box<dyn PartialReflect>)105}106}107108impl<K, V, S> PartialReflect for IndexMap<K, V, S>109where110K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,111V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,112S: TypePath + BuildHasher + Default + Send + Sync,113{114fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {115Some(<Self as Typed>::type_info())116}117118#[inline]119fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {120self121}122123fn as_partial_reflect(&self) -> &dyn PartialReflect {124self125}126127fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {128self129}130131#[inline]132fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {133Ok(self)134}135136fn try_as_reflect(&self) -> Option<&dyn Reflect> {137Some(self)138}139140fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {141Some(self)142}143144fn apply(&mut self, value: &dyn PartialReflect) {145crate::map::map_apply(self, value);146}147148fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), crate::ApplyError> {149crate::map::map_try_apply(self, value)150}151152fn reflect_kind(&self) -> ReflectKind {153ReflectKind::Map154}155156fn reflect_ref(&self) -> ReflectRef<'_> {157ReflectRef::Map(self)158}159160fn reflect_mut(&mut self) -> ReflectMut<'_> {161ReflectMut::Map(self)162}163164fn reflect_owned(self: Box<Self>) -> ReflectOwned {165ReflectOwned::Map(self)166}167168fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {169let mut map = Self::with_capacity_and_hasher(self.len(), S::default());170for (key, value) in self.iter() {171let key = key.reflect_clone_and_take()?;172let value = value.reflect_clone_and_take()?;173map.insert(key, value);174}175176Ok(Box::new(map))177}178179fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {180crate::map::map_partial_eq(self, value)181}182}183184impl<K, V, S> Reflect for IndexMap<K, V, S>185where186K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,187V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,188S: TypePath + BuildHasher + Default + Send + Sync,189{190fn into_any(self: Box<Self>) -> Box<dyn Any> {191self192}193194fn as_any(&self) -> &dyn Any {195self196}197198fn as_any_mut(&mut self) -> &mut dyn Any {199self200}201202fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {203self204}205206fn as_reflect(&self) -> &dyn Reflect {207self208}209210fn as_reflect_mut(&mut self) -> &mut dyn Reflect {211self212}213214fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {215*self = value.take()?;216Ok(())217}218}219220impl<K, V, S> Typed for IndexMap<K, V, S>221where222K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,223V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,224S: TypePath + BuildHasher + Default + Send + Sync,225{226fn type_info() -> &'static TypeInfo {227static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();228CELL.get_or_insert::<Self, _>(|| {229TypeInfo::Map(230MapInfo::new::<Self, K, V>().with_generics(Generics::from_iter([231TypeParamInfo::new::<K>("K"),232TypeParamInfo::new::<V>("V"),233])),234)235})236}237}238239impl<K, V, S> FromReflect for IndexMap<K, V, S>240where241K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,242V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,243S: TypePath + BuildHasher + Default + Send + Sync,244{245fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {246let ref_map = reflect.reflect_ref().as_map().ok()?;247248let mut new_map = Self::with_capacity_and_hasher(ref_map.len(), S::default());249250for (key, value) in ref_map.iter() {251let new_key = K::from_reflect(key)?;252let new_value = V::from_reflect(value)?;253new_map.insert(new_key, new_value);254}255256Some(new_map)257}258}259260impl<K, V, S> GetTypeRegistration for IndexMap<K, V, S>261where262K: Hash + Eq + FromReflect + MaybeTyped + TypePath + GetTypeRegistration,263V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,264S: TypePath + BuildHasher + Send + Sync + Default,265{266fn get_type_registration() -> TypeRegistration {267let mut registration = TypeRegistration::of::<Self>();268registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());269registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());270registration271}272273fn register_type_dependencies(registry: &mut TypeRegistry) {274registry.register::<K>();275registry.register::<V>();276}277}278279impl_type_path!(::indexmap::IndexMap<K, V, S>);280281impl<T, S> Set for IndexSet<T, S>282where283T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,284S: TypePath + BuildHasher + Default + Send + Sync,285{286fn get(&self, value: &dyn PartialReflect) -> Option<&dyn PartialReflect> {287value288.try_downcast_ref::<T>()289.and_then(|value| Self::get(self, value))290.map(|value| value as &dyn PartialReflect)291}292293fn len(&self) -> usize {294self.len()295}296297fn iter(&self) -> Box<dyn Iterator<Item = &dyn PartialReflect> + '_> {298let iter = self.iter().map(|v| v as &dyn PartialReflect);299Box::new(iter)300}301302fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {303self.drain(..)304.map(|value| Box::new(value) as Box<dyn PartialReflect>)305.collect()306}307308fn retain(&mut self, f: &mut dyn FnMut(&dyn PartialReflect) -> bool) {309self.retain(move |value| f(value));310}311312fn insert_boxed(&mut self, value: Box<dyn PartialReflect>) -> bool {313let value = T::take_from_reflect(value).unwrap_or_else(|value| {314panic!(315"Attempted to insert invalid value of type {}.",316value.reflect_type_path()317)318});319self.insert(value)320}321322fn remove(&mut self, value: &dyn PartialReflect) -> bool {323let mut from_reflect = None;324value325.try_downcast_ref::<T>()326.or_else(|| {327from_reflect = T::from_reflect(value);328from_reflect.as_ref()329})330.is_some_and(|value| self.shift_remove(value))331}332333fn contains(&self, value: &dyn PartialReflect) -> bool {334let mut from_reflect = None;335value336.try_downcast_ref::<T>()337.or_else(|| {338from_reflect = T::from_reflect(value);339from_reflect.as_ref()340})341.is_some_and(|value| self.contains(value))342}343}344345impl<T, S> PartialReflect for IndexSet<T, S>346where347T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,348S: TypePath + BuildHasher + Default + Send + Sync,349{350fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {351Some(<Self as Typed>::type_info())352}353354#[inline]355fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {356self357}358359fn as_partial_reflect(&self) -> &dyn PartialReflect {360self361}362363fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {364self365}366367#[inline]368fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {369Ok(self)370}371372fn try_as_reflect(&self) -> Option<&dyn Reflect> {373Some(self)374}375376fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {377Some(self)378}379380fn apply(&mut self, value: &dyn PartialReflect) {381crate::set::set_apply(self, value);382}383384fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), crate::ApplyError> {385crate::set::set_try_apply(self, value)386}387388fn reflect_kind(&self) -> ReflectKind {389ReflectKind::Set390}391392fn reflect_ref(&self) -> ReflectRef<'_> {393ReflectRef::Set(self)394}395396fn reflect_mut(&mut self) -> ReflectMut<'_> {397ReflectMut::Set(self)398}399400fn reflect_owned(self: Box<Self>) -> ReflectOwned {401ReflectOwned::Set(self)402}403404fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {405Ok(Box::new(406self.iter()407.map(PartialReflect::reflect_clone_and_take)408.collect::<Result<Self, ReflectCloneError>>()?,409))410}411412fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {413crate::set::set_partial_eq(self, value)414}415}416417impl<T, S> Reflect for IndexSet<T, S>418where419T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,420S: TypePath + BuildHasher + Default + Send + Sync,421{422fn into_any(self: Box<Self>) -> Box<dyn Any> {423self424}425426fn as_any(&self) -> &dyn Any {427self428}429430fn as_any_mut(&mut self) -> &mut dyn Any {431self432}433434fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {435self436}437438fn as_reflect(&self) -> &dyn Reflect {439self440}441442fn as_reflect_mut(&mut self) -> &mut dyn Reflect {443self444}445446fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {447*self = value.take()?;448Ok(())449}450}451452impl<T, S> Typed for IndexSet<T, S>453where454T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,455S: TypePath + BuildHasher + Default + Send + Sync,456{457fn type_info() -> &'static TypeInfo {458static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();459CELL.get_or_insert::<Self, _>(|| {460TypeInfo::Set(461SetInfo::new::<Self, T>()462.with_generics(Generics::from_iter([TypeParamInfo::new::<T>("T")])),463)464})465}466}467468impl<T, S> FromReflect for IndexSet<T, S>469where470T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,471S: TypePath + BuildHasher + Default + Send + Sync,472{473fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {474let ref_set = reflect.reflect_ref().as_set().ok()?;475476let mut new_set = Self::with_capacity_and_hasher(ref_set.len(), S::default());477478for field in ref_set.iter() {479new_set.insert(T::from_reflect(field)?);480}481482Some(new_set)483}484}485486impl<T, S> GetTypeRegistration for IndexSet<T, S>487where488T: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,489S: TypePath + BuildHasher + Default + Send + Sync,490{491fn get_type_registration() -> TypeRegistration {492let mut registration = TypeRegistration::of::<Self>();493registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());494registration495}496}497498impl_type_path!(::indexmap::IndexSet<T, S>);499500501