Path: blob/main/crates/bevy_reflect/src/impls/std/path.rs
9358 views
use crate::{1error::ReflectCloneError,2kind::{ReflectKind, ReflectMut, ReflectOwned, ReflectRef},3prelude::*,4reflect::ApplyError,5type_info::{OpaqueInfo, TypeInfo, Typed},6type_path::DynamicTypePath,7type_registry::{8FromType, GetTypeRegistration, ReflectDeserialize, ReflectFromPtr, ReflectSerialize,9TypeRegistration,10},11utility::{reflect_hasher, NonGenericTypeInfoCell},12};13use alloc::borrow::Cow;14use bevy_platform::prelude::*;15use bevy_reflect_derive::{impl_reflect_opaque, impl_type_path};16use core::any::Any;17use core::fmt;18use core::hash::{Hash, Hasher};19use std::path::Path;2021impl_reflect_opaque!(::std::path::PathBuf(22Clone,23Debug,24Hash,25PartialEq,26PartialOrd,27Serialize,28Deserialize,29Default30));3132impl PartialReflect for &'static Path {33fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {34Some(<Self as Typed>::type_info())35}3637#[inline]38fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {39self40}4142fn as_partial_reflect(&self) -> &dyn PartialReflect {43self44}4546fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {47self48}4950fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {51Ok(self)52}5354fn try_as_reflect(&self) -> Option<&dyn Reflect> {55Some(self)56}5758fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {59Some(self)60}6162fn reflect_kind(&self) -> ReflectKind {63ReflectKind::Opaque64}6566fn reflect_ref(&self) -> ReflectRef<'_> {67ReflectRef::Opaque(self)68}6970fn reflect_mut(&mut self) -> ReflectMut<'_> {71ReflectMut::Opaque(self)72}7374fn reflect_owned(self: Box<Self>) -> ReflectOwned {75ReflectOwned::Opaque(self)76}7778fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {79Ok(Box::new(*self))80}8182fn reflect_hash(&self) -> Option<u64> {83let mut hasher = reflect_hasher();84Hash::hash(&Any::type_id(self), &mut hasher);85Hash::hash(self, &mut hasher);86Some(hasher.finish())87}8889fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {90if let Some(value) = value.try_downcast_ref::<Self>() {91Some(PartialEq::eq(self, value))92} else {93Some(false)94}95}9697fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {98if let Some(value) = value.try_downcast_ref::<Self>() {99PartialOrd::partial_cmp(self, value)100} else {101None102}103}104105fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {106if let Some(value) = value.try_downcast_ref::<Self>() {107self.clone_from(value);108Ok(())109} else {110Err(ApplyError::MismatchedTypes {111from_type: value.reflect_type_path().into(),112to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),113})114}115}116}117118impl Reflect for &'static Path {119fn into_any(self: Box<Self>) -> Box<dyn Any> {120self121}122123fn as_any(&self) -> &dyn Any {124self125}126127fn as_any_mut(&mut self) -> &mut dyn Any {128self129}130131fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {132self133}134135fn as_reflect(&self) -> &dyn Reflect {136self137}138139fn as_reflect_mut(&mut self) -> &mut dyn Reflect {140self141}142143fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {144*self = value.take()?;145Ok(())146}147}148149impl Typed for &'static Path {150fn type_info() -> &'static TypeInfo {151static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();152CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))153}154}155156impl GetTypeRegistration for &'static Path {157fn get_type_registration() -> TypeRegistration {158let mut registration = TypeRegistration::of::<Self>();159registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());160registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());161registration162}163}164165impl FromReflect for &'static Path {166fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {167reflect.try_downcast_ref::<Self>().copied()168}169}170171impl PartialReflect for Cow<'static, Path> {172fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {173Some(<Self as Typed>::type_info())174}175176#[inline]177fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {178self179}180181fn as_partial_reflect(&self) -> &dyn PartialReflect {182self183}184185fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {186self187}188189fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {190Ok(self)191}192193fn try_as_reflect(&self) -> Option<&dyn Reflect> {194Some(self)195}196197fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {198Some(self)199}200201fn reflect_kind(&self) -> ReflectKind {202ReflectKind::Opaque203}204205fn reflect_ref(&self) -> ReflectRef<'_> {206ReflectRef::Opaque(self)207}208209fn reflect_mut(&mut self) -> ReflectMut<'_> {210ReflectMut::Opaque(self)211}212213fn reflect_owned(self: Box<Self>) -> ReflectOwned {214ReflectOwned::Opaque(self)215}216217fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {218Ok(Box::new(self.clone()))219}220221fn reflect_hash(&self) -> Option<u64> {222let mut hasher = reflect_hasher();223Hash::hash(&Any::type_id(self), &mut hasher);224Hash::hash(self, &mut hasher);225Some(hasher.finish())226}227228fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {229if let Some(value) = value.try_downcast_ref::<Self>() {230Some(PartialEq::eq(self, value))231} else {232Some(false)233}234}235236fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {237if let Some(value) = value.try_downcast_ref::<Self>() {238PartialOrd::partial_cmp(self, value)239} else {240None241}242}243244fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {245fmt::Debug::fmt(&self, f)246}247248fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {249if let Some(value) = value.try_downcast_ref::<Self>() {250self.clone_from(value);251Ok(())252} else {253Err(ApplyError::MismatchedTypes {254from_type: value.reflect_type_path().into(),255to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),256})257}258}259}260261impl Reflect for Cow<'static, Path> {262fn into_any(self: Box<Self>) -> Box<dyn Any> {263self264}265266fn as_any(&self) -> &dyn Any {267self268}269270fn as_any_mut(&mut self) -> &mut dyn Any {271self272}273274fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {275self276}277278fn as_reflect(&self) -> &dyn Reflect {279self280}281282fn as_reflect_mut(&mut self) -> &mut dyn Reflect {283self284}285286fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {287*self = value.take()?;288Ok(())289}290}291292impl Typed for Cow<'static, Path> {293fn type_info() -> &'static TypeInfo {294static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();295CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))296}297}298299impl_type_path!(::std::path::Path);300301impl FromReflect for Cow<'static, Path> {302fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {303Some(reflect.try_downcast_ref::<Self>()?.clone())304}305}306307impl GetTypeRegistration for Cow<'static, Path> {308fn get_type_registration() -> TypeRegistration {309let mut registration = TypeRegistration::of::<Self>();310registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());311registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());312registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());313registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());314registration315}316}317318#[cfg(feature = "functions")]319crate::func::macros::impl_function_traits!(Cow<'static, Path>);320321322