Path: blob/main/crates/bevy_reflect/src/impls/alloc/borrow.rs
9353 views
use crate::{1error::ReflectCloneError,2kind::{ReflectKind, ReflectMut, ReflectOwned, ReflectRef},3list::{List, ListInfo, ListIter},4prelude::*,5reflect::{impl_full_reflect, ApplyError},6type_info::{MaybeTyped, OpaqueInfo, TypeInfo, Typed},7type_registry::{8FromType, GetTypeRegistration, ReflectDeserialize, ReflectFromPtr, ReflectSerialize,9TypeRegistration, TypeRegistry,10},11utility::{reflect_hasher, GenericTypeInfoCell, NonGenericTypeInfoCell},12};13use alloc::borrow::Cow;14use alloc::vec::Vec;15use bevy_platform::prelude::*;16use bevy_reflect_derive::impl_type_path;17use core::any::Any;18use core::fmt;19use core::hash::{Hash, Hasher};2021impl_type_path!(::alloc::borrow::Cow<'a: 'static, T: ToOwned + ?Sized>);2223impl PartialReflect for Cow<'static, str> {24fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {25Some(<Self as Typed>::type_info())26}2728#[inline]29fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {30self31}3233fn as_partial_reflect(&self) -> &dyn PartialReflect {34self35}3637fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {38self39}4041fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {42Ok(self)43}4445fn try_as_reflect(&self) -> Option<&dyn Reflect> {46Some(self)47}4849fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {50Some(self)51}5253fn reflect_kind(&self) -> ReflectKind {54ReflectKind::Opaque55}5657fn reflect_ref(&self) -> ReflectRef<'_> {58ReflectRef::Opaque(self)59}6061fn reflect_mut(&mut self) -> ReflectMut<'_> {62ReflectMut::Opaque(self)63}6465fn reflect_owned(self: Box<Self>) -> ReflectOwned {66ReflectOwned::Opaque(self)67}6869fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {70Ok(Box::new(self.clone()))71}7273fn reflect_hash(&self) -> Option<u64> {74let mut hasher = reflect_hasher();75Hash::hash(&Any::type_id(self), &mut hasher);76Hash::hash(self, &mut hasher);77Some(hasher.finish())78}7980fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {81if let Some(value) = value.try_downcast_ref::<Self>() {82Some(PartialEq::eq(self, value))83} else {84Some(false)85}86}8788fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {89if let Some(value) = value.try_downcast_ref::<Self>() {90PartialOrd::partial_cmp(self, value)91} else {92None93}94}9596fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {97fmt::Debug::fmt(self, f)98}99100fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {101if let Some(value) = value.try_downcast_ref::<Self>() {102self.clone_from(value);103} else {104return Err(ApplyError::MismatchedTypes {105from_type: value.reflect_type_path().into(),106// If we invoke the reflect_type_path on self directly the borrow checker complains that the lifetime of self must outlive 'static107to_type: Self::type_path().into(),108});109}110Ok(())111}112}113114impl_full_reflect!(for Cow<'static, str>);115116impl Typed for Cow<'static, str> {117fn type_info() -> &'static TypeInfo {118static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();119CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))120}121}122123impl GetTypeRegistration for Cow<'static, str> {124fn get_type_registration() -> TypeRegistration {125let mut registration = TypeRegistration::of::<Cow<'static, str>>();126registration.insert::<ReflectDeserialize>(FromType::<Cow<'static, str>>::from_type());127registration.insert::<ReflectFromPtr>(FromType::<Cow<'static, str>>::from_type());128registration.insert::<ReflectFromReflect>(FromType::<Cow<'static, str>>::from_type());129registration.insert::<ReflectSerialize>(FromType::<Cow<'static, str>>::from_type());130registration131}132}133134impl FromReflect for Cow<'static, str> {135fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {136Some(reflect.try_downcast_ref::<Cow<'static, str>>()?.clone())137}138}139140#[cfg(feature = "functions")]141crate::func::macros::impl_function_traits!(Cow<'static, str>);142143impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> List144for Cow<'static, [T]>145{146fn get(&self, index: usize) -> Option<&dyn PartialReflect> {147self.as_ref().get(index).map(|x| x as &dyn PartialReflect)148}149150fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {151self.to_mut()152.get_mut(index)153.map(|x| x as &mut dyn PartialReflect)154}155156fn insert(&mut self, index: usize, element: Box<dyn PartialReflect>) {157let value = T::take_from_reflect(element).unwrap_or_else(|value| {158panic!(159"Attempted to insert invalid value of type {}.",160value.reflect_type_path()161);162});163self.to_mut().insert(index, value);164}165166fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {167Box::new(self.to_mut().remove(index))168}169170fn push(&mut self, value: Box<dyn PartialReflect>) {171let value = T::take_from_reflect(value).unwrap_or_else(|value| {172panic!(173"Attempted to push invalid value of type {}.",174value.reflect_type_path()175)176});177self.to_mut().push(value);178}179180fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {181self.to_mut()182.pop()183.map(|value| Box::new(value) as Box<dyn PartialReflect>)184}185186fn len(&self) -> usize {187self.as_ref().len()188}189190fn iter(&self) -> ListIter<'_> {191ListIter::new(self)192}193194fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {195self.to_mut()196.drain(..)197.map(|value| Box::new(value) as Box<dyn PartialReflect>)198.collect()199}200}201202impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> PartialReflect203for Cow<'static, [T]>204{205fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {206Some(<Self as Typed>::type_info())207}208209#[inline]210fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {211self212}213214fn as_partial_reflect(&self) -> &dyn PartialReflect {215self216}217218fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {219self220}221222fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {223Ok(self)224}225226fn try_as_reflect(&self) -> Option<&dyn Reflect> {227Some(self)228}229230fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {231Some(self)232}233234fn reflect_kind(&self) -> ReflectKind {235ReflectKind::List236}237238fn reflect_ref(&self) -> ReflectRef<'_> {239ReflectRef::List(self)240}241242fn reflect_mut(&mut self) -> ReflectMut<'_> {243ReflectMut::List(self)244}245246fn reflect_owned(self: Box<Self>) -> ReflectOwned {247ReflectOwned::List(self)248}249250fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {251Ok(Box::new(self.clone()))252}253254fn reflect_hash(&self) -> Option<u64> {255crate::list::list_hash(self)256}257258fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {259crate::list::list_partial_eq(self, value)260}261262fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<::core::cmp::Ordering> {263crate::list::list_partial_cmp(self, value)264}265266fn apply(&mut self, value: &dyn PartialReflect) {267crate::list::list_apply(self, value);268}269270fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {271crate::list::list_try_apply(self, value)272}273}274275impl_full_reflect!(276<T> for Cow<'static, [T]>277where278T: FromReflect + Clone + MaybeTyped + TypePath + GetTypeRegistration,279);280281impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> Typed282for Cow<'static, [T]>283{284fn type_info() -> &'static TypeInfo {285static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();286CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))287}288}289290impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> GetTypeRegistration291for Cow<'static, [T]>292{293fn get_type_registration() -> TypeRegistration {294TypeRegistration::of::<Cow<'static, [T]>>()295}296297fn register_type_dependencies(registry: &mut TypeRegistry) {298registry.register::<T>();299}300}301302impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> FromReflect303for Cow<'static, [T]>304{305fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {306let ref_list = reflect.reflect_ref().as_list().ok()?;307308let mut temp_vec = Vec::with_capacity(ref_list.len());309310for field in ref_list.iter() {311temp_vec.push(T::from_reflect(field)?);312}313314Some(temp_vec.into())315}316}317318#[cfg(feature = "functions")]319crate::func::macros::impl_function_traits!(Cow<'static, [T]>; <T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration>);320321322