Skip to content

Commit

Permalink
reflect: remove manual Reflect impls which could be handled by macros (
Browse files Browse the repository at this point in the history
…#12596)

# Objective

* Adopted #12025 to fix merge conflicts
* In some cases we used manual impls for certain types, though they are
(at least, now) unnecessary.

## Solution

* Use macros and reflecting-by-value to avoid this clutter.
* Though there were linker issues with Reflect and the CowArc in
AssetPath (see #9747), I
checked these are resolved by using #[reflect_value].

---------

Co-authored-by: soqb <[email protected]>
Co-authored-by: Alice Cecile <[email protected]>
Co-authored-by: James Liu <[email protected]>
  • Loading branch information
4 people authored Mar 23, 2024
1 parent 037f9d4 commit fdf2ea7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 488 deletions.
142 changes: 4 additions & 138 deletions crates/bevy_asset/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use crate::io::AssetSourceId;
use bevy_reflect::{
std_traits::ReflectDefault, utility::NonGenericTypeInfoCell, FromReflect, FromType,
GetTypeRegistration, Reflect, ReflectDeserialize, ReflectFromPtr, ReflectFromReflect,
ReflectKind, ReflectMut, ReflectOwned, ReflectRef, ReflectSerialize, TypeInfo, TypePath,
TypeRegistration, Typed, ValueInfo,
};
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_utils::CowArc;
use serde::{de::Visitor, Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
hash::{Hash, Hasher},
hash::Hash,
ops::Deref,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -52,7 +47,8 @@ use thiserror::Error;
/// This means that the common case of `asset_server.load("my_scene.scn")` when it creates and
/// clones internal owned [`AssetPaths`](AssetPath).
/// This also means that you should use [`AssetPath::parse`] in cases where `&str` is the explicit type.
#[derive(Eq, PartialEq, Hash, Clone, Default)]
#[derive(Eq, PartialEq, Hash, Clone, Default, Reflect)]
#[reflect_value(Debug, PartialEq, Hash, Serialize, Deserialize)]
pub struct AssetPath<'a> {
source: AssetSourceId<'a>,
path: CowArc<'a, Path>,
Expand Down Expand Up @@ -572,136 +568,6 @@ impl<'de> Visitor<'de> for AssetPathVisitor {
}
}

// NOTE: We manually implement "reflect value" because deriving Reflect on `AssetPath` breaks dynamic linking
// See https://github.com/bevyengine/bevy/issues/9747
// NOTE: This could use `impl_reflect_value` if it supported static lifetimes.

impl GetTypeRegistration for AssetPath<'static> {
fn get_type_registration() -> TypeRegistration {
let mut registration = TypeRegistration::of::<Self>();
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
registration.insert::<ReflectDefault>(FromType::<Self>::from_type());
registration
}
}

impl TypePath for AssetPath<'static> {
fn type_path() -> &'static str {
"bevy_asset::path::AssetPath<'static>"
}
fn short_type_path() -> &'static str {
"AssetPath<'static>"
}
fn type_ident() -> Option<&'static str> {
Some("AssetPath<'static>")
}
fn crate_name() -> Option<&'static str> {
None
}
fn module_path() -> Option<&'static str> {
None
}
}
impl Typed for AssetPath<'static> {
fn type_info() -> &'static TypeInfo {
static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
CELL.get_or_set(|| {
let info = ValueInfo::new::<Self>();
TypeInfo::Value(info)
})
}
}
impl Reflect for AssetPath<'static> {
#[inline]
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Some(<Self as Typed>::type_info())
}
#[inline]
fn into_any(self: Box<Self>) -> Box<dyn core::any::Any> {
self
}
#[inline]
fn as_any(&self) -> &dyn core::any::Any {
self
}
#[inline]
fn as_any_mut(&mut self) -> &mut dyn core::any::Any {
self
}
#[inline]
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}
#[inline]
fn as_reflect(&self) -> &dyn Reflect {
self
}
#[inline]
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}
#[inline]
fn apply(&mut self, value: &dyn Reflect) {
let value = Reflect::as_any(value);
if let Some(value) = value.downcast_ref::<Self>() {
*self = value.clone();
} else {
panic!("Value is not {}.", std::any::type_name::<Self>());
}
}
#[inline]
fn set(
&mut self,
value: Box<dyn bevy_reflect::Reflect>,
) -> Result<(), Box<dyn bevy_reflect::Reflect>> {
*self = <dyn bevy_reflect::Reflect>::take(value)?;
Ok(())
}
fn reflect_kind(&self) -> ReflectKind {
ReflectKind::Value
}
fn reflect_ref(&self) -> ReflectRef {
ReflectRef::Value(self)
}
fn reflect_mut(&mut self) -> ReflectMut {
ReflectMut::Value(self)
}
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
ReflectOwned::Value(self)
}
#[inline]
fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(self.clone())
}
fn reflect_hash(&self) -> Option<u64> {
let mut hasher = bevy_reflect::utility::reflect_hasher();
Hash::hash(&::core::any::Any::type_id(self), &mut hasher);
Hash::hash(self, &mut hasher);
Some(Hasher::finish(&hasher))
}
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
let value = <dyn Reflect>::as_any(value);
if let Some(value) = <dyn core::any::Any>::downcast_ref::<Self>(value) {
Some(PartialEq::eq(self, value))
} else {
Some(false)
}
}
fn debug(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
::core::fmt::Debug::fmt(self, f)
}
}
impl FromReflect for AssetPath<'static> {
fn from_reflect(reflect: &dyn Reflect) -> Option<Self> {
Some(Clone::clone(<dyn core::any::Any>::downcast_ref::<
AssetPath<'static>,
>(<dyn Reflect>::as_any(reflect))?))
}
}

/// Normalizes the path by collapsing all occurrences of '.' and '..' dot-segments where possible
/// as per [RFC 1808](https://datatracker.ietf.org/doc/html/rfc1808)
pub(crate) fn normalize_path(path: &Path) -> PathBuf {
Expand Down
Loading

0 comments on commit fdf2ea7

Please sign in to comment.