Path: blob/main/crates/bevy_reflect/src/impls/alloc/borrow.rs
6600 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 debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {89fmt::Debug::fmt(self, f)90}9192fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {93if let Some(value) = value.try_downcast_ref::<Self>() {94self.clone_from(value);95} else {96return Err(ApplyError::MismatchedTypes {97from_type: value.reflect_type_path().into(),98// If we invoke the reflect_type_path on self directly the borrow checker complains that the lifetime of self must outlive 'static99to_type: Self::type_path().into(),100});101}102Ok(())103}104}105106impl_full_reflect!(for Cow<'static, str>);107108impl Typed for Cow<'static, str> {109fn type_info() -> &'static TypeInfo {110static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();111CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))112}113}114115impl GetTypeRegistration for Cow<'static, str> {116fn get_type_registration() -> TypeRegistration {117let mut registration = TypeRegistration::of::<Cow<'static, str>>();118registration.insert::<ReflectDeserialize>(FromType::<Cow<'static, str>>::from_type());119registration.insert::<ReflectFromPtr>(FromType::<Cow<'static, str>>::from_type());120registration.insert::<ReflectFromReflect>(FromType::<Cow<'static, str>>::from_type());121registration.insert::<ReflectSerialize>(FromType::<Cow<'static, str>>::from_type());122registration123}124}125126impl FromReflect for Cow<'static, str> {127fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {128Some(reflect.try_downcast_ref::<Cow<'static, str>>()?.clone())129}130}131132#[cfg(feature = "functions")]133crate::func::macros::impl_function_traits!(Cow<'static, str>);134135impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> List136for Cow<'static, [T]>137{138fn get(&self, index: usize) -> Option<&dyn PartialReflect> {139self.as_ref().get(index).map(|x| x as &dyn PartialReflect)140}141142fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {143self.to_mut()144.get_mut(index)145.map(|x| x as &mut dyn PartialReflect)146}147148fn insert(&mut self, index: usize, element: Box<dyn PartialReflect>) {149let value = T::take_from_reflect(element).unwrap_or_else(|value| {150panic!(151"Attempted to insert invalid value of type {}.",152value.reflect_type_path()153);154});155self.to_mut().insert(index, value);156}157158fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {159Box::new(self.to_mut().remove(index))160}161162fn push(&mut self, value: Box<dyn PartialReflect>) {163let value = T::take_from_reflect(value).unwrap_or_else(|value| {164panic!(165"Attempted to push invalid value of type {}.",166value.reflect_type_path()167)168});169self.to_mut().push(value);170}171172fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {173self.to_mut()174.pop()175.map(|value| Box::new(value) as Box<dyn PartialReflect>)176}177178fn len(&self) -> usize {179self.as_ref().len()180}181182fn iter(&self) -> ListIter<'_> {183ListIter::new(self)184}185186fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {187self.to_mut()188.drain(..)189.map(|value| Box::new(value) as Box<dyn PartialReflect>)190.collect()191}192}193194impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> PartialReflect195for Cow<'static, [T]>196{197fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {198Some(<Self as Typed>::type_info())199}200201#[inline]202fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {203self204}205206fn as_partial_reflect(&self) -> &dyn PartialReflect {207self208}209210fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {211self212}213214fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {215Ok(self)216}217218fn try_as_reflect(&self) -> Option<&dyn Reflect> {219Some(self)220}221222fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {223Some(self)224}225226fn reflect_kind(&self) -> ReflectKind {227ReflectKind::List228}229230fn reflect_ref(&self) -> ReflectRef<'_> {231ReflectRef::List(self)232}233234fn reflect_mut(&mut self) -> ReflectMut<'_> {235ReflectMut::List(self)236}237238fn reflect_owned(self: Box<Self>) -> ReflectOwned {239ReflectOwned::List(self)240}241242fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {243Ok(Box::new(self.clone()))244}245246fn reflect_hash(&self) -> Option<u64> {247crate::list_hash(self)248}249250fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {251crate::list_partial_eq(self, value)252}253254fn apply(&mut self, value: &dyn PartialReflect) {255crate::list_apply(self, value);256}257258fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {259crate::list_try_apply(self, value)260}261}262263impl_full_reflect!(264<T> for Cow<'static, [T]>265where266T: FromReflect + Clone + MaybeTyped + TypePath + GetTypeRegistration,267);268269impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> Typed270for Cow<'static, [T]>271{272fn type_info() -> &'static TypeInfo {273static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();274CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))275}276}277278impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> GetTypeRegistration279for Cow<'static, [T]>280{281fn get_type_registration() -> TypeRegistration {282TypeRegistration::of::<Cow<'static, [T]>>()283}284285fn register_type_dependencies(registry: &mut TypeRegistry) {286registry.register::<T>();287}288}289290impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> FromReflect291for Cow<'static, [T]>292{293fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {294let ref_list = reflect.reflect_ref().as_list().ok()?;295296let mut temp_vec = Vec::with_capacity(ref_list.len());297298for field in ref_list.iter() {299temp_vec.push(T::from_reflect(field)?);300}301302Some(temp_vec.into())303}304}305306#[cfg(feature = "functions")]307crate::func::macros::impl_function_traits!(Cow<'static, [T]>; <T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration>);308309310