Path: blob/main/crates/bevy_reflect/src/impls/std/path.rs
6600 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,26Serialize,27Deserialize,28Default29));3031impl PartialReflect for &'static Path {32fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {33Some(<Self as Typed>::type_info())34}3536#[inline]37fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {38self39}4041fn as_partial_reflect(&self) -> &dyn PartialReflect {42self43}4445fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {46self47}4849fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {50Ok(self)51}5253fn try_as_reflect(&self) -> Option<&dyn Reflect> {54Some(self)55}5657fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {58Some(self)59}6061fn reflect_kind(&self) -> ReflectKind {62ReflectKind::Opaque63}6465fn reflect_ref(&self) -> ReflectRef<'_> {66ReflectRef::Opaque(self)67}6869fn reflect_mut(&mut self) -> ReflectMut<'_> {70ReflectMut::Opaque(self)71}7273fn reflect_owned(self: Box<Self>) -> ReflectOwned {74ReflectOwned::Opaque(self)75}7677fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {78Ok(Box::new(*self))79}8081fn reflect_hash(&self) -> Option<u64> {82let mut hasher = reflect_hasher();83Hash::hash(&Any::type_id(self), &mut hasher);84Hash::hash(self, &mut hasher);85Some(hasher.finish())86}8788fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {89if let Some(value) = value.try_downcast_ref::<Self>() {90Some(PartialEq::eq(self, value))91} else {92Some(false)93}94}9596fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {97if let Some(value) = value.try_downcast_ref::<Self>() {98self.clone_from(value);99Ok(())100} else {101Err(ApplyError::MismatchedTypes {102from_type: value.reflect_type_path().into(),103to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),104})105}106}107}108109impl Reflect for &'static Path {110fn into_any(self: Box<Self>) -> Box<dyn Any> {111self112}113114fn as_any(&self) -> &dyn Any {115self116}117118fn as_any_mut(&mut self) -> &mut dyn Any {119self120}121122fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {123self124}125126fn as_reflect(&self) -> &dyn Reflect {127self128}129130fn as_reflect_mut(&mut self) -> &mut dyn Reflect {131self132}133134fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {135*self = value.take()?;136Ok(())137}138}139140impl Typed for &'static Path {141fn type_info() -> &'static TypeInfo {142static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();143CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))144}145}146147impl GetTypeRegistration for &'static Path {148fn get_type_registration() -> TypeRegistration {149let mut registration = TypeRegistration::of::<Self>();150registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());151registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());152registration153}154}155156impl FromReflect for &'static Path {157fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {158reflect.try_downcast_ref::<Self>().copied()159}160}161162impl PartialReflect for Cow<'static, Path> {163fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {164Some(<Self as Typed>::type_info())165}166167#[inline]168fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {169self170}171172fn as_partial_reflect(&self) -> &dyn PartialReflect {173self174}175176fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {177self178}179180fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {181Ok(self)182}183184fn try_as_reflect(&self) -> Option<&dyn Reflect> {185Some(self)186}187188fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {189Some(self)190}191192fn reflect_kind(&self) -> ReflectKind {193ReflectKind::Opaque194}195196fn reflect_ref(&self) -> ReflectRef<'_> {197ReflectRef::Opaque(self)198}199200fn reflect_mut(&mut self) -> ReflectMut<'_> {201ReflectMut::Opaque(self)202}203204fn reflect_owned(self: Box<Self>) -> ReflectOwned {205ReflectOwned::Opaque(self)206}207208fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {209Ok(Box::new(self.clone()))210}211212fn reflect_hash(&self) -> Option<u64> {213let mut hasher = reflect_hasher();214Hash::hash(&Any::type_id(self), &mut hasher);215Hash::hash(self, &mut hasher);216Some(hasher.finish())217}218219fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {220if let Some(value) = value.try_downcast_ref::<Self>() {221Some(PartialEq::eq(self, value))222} else {223Some(false)224}225}226227fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {228fmt::Debug::fmt(&self, f)229}230231fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {232if let Some(value) = value.try_downcast_ref::<Self>() {233self.clone_from(value);234Ok(())235} else {236Err(ApplyError::MismatchedTypes {237from_type: value.reflect_type_path().into(),238to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),239})240}241}242}243244impl Reflect for Cow<'static, Path> {245fn into_any(self: Box<Self>) -> Box<dyn Any> {246self247}248249fn as_any(&self) -> &dyn Any {250self251}252253fn as_any_mut(&mut self) -> &mut dyn Any {254self255}256257fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {258self259}260261fn as_reflect(&self) -> &dyn Reflect {262self263}264265fn as_reflect_mut(&mut self) -> &mut dyn Reflect {266self267}268269fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {270*self = value.take()?;271Ok(())272}273}274275impl Typed for Cow<'static, Path> {276fn type_info() -> &'static TypeInfo {277static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();278CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))279}280}281282impl_type_path!(::std::path::Path);283284impl FromReflect for Cow<'static, Path> {285fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {286Some(reflect.try_downcast_ref::<Self>()?.clone())287}288}289290impl GetTypeRegistration for Cow<'static, Path> {291fn get_type_registration() -> TypeRegistration {292let mut registration = TypeRegistration::of::<Self>();293registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());294registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());295registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());296registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());297registration298}299}300301#[cfg(feature = "functions")]302crate::func::macros::impl_function_traits!(Cow<'static, Path>);303304305