Skip to content

Commit

Permalink
bevy_reflect: Reflect &'static str (bevyengine#11686)
Browse files Browse the repository at this point in the history
# Objective

`&'static str` doesn't implement `Reflect`. I don't think this was
intentionally excluded.

## Solution

Make `&'static str` implement `Reflect`.

---

## Changelog

- Implement `Reflect` and friends for `&'static str`
- Add missing `Reflect::debug` implementation for `Cow<'static, str>`
  • Loading branch information
MrGVSV authored and tjamaan committed Feb 6, 2024
1 parent 3434510 commit 99f2ac4
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,10 @@ impl Reflect for Cow<'static, str> {
Some(false)
}
}

fn debug(&self, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
fmt::Debug::fmt(self, f)
}
}

impl Typed for Cow<'static, str> {
Expand Down Expand Up @@ -1305,6 +1309,108 @@ impl<T: FromReflect + Clone + TypePath> FromReflect for Cow<'static, [T]> {
}
}

impl Reflect for &'static str {
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Some(<Self as Typed>::type_info())
}

fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}

fn as_any(&self) -> &dyn Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn Any {
self
}

fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
self
}

fn as_reflect(&self) -> &dyn Reflect {
self
}

fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
self
}

fn apply(&mut self, value: &dyn Reflect) {
let value = value.as_any();
if let Some(&value) = value.downcast_ref::<Self>() {
*self = value;
} else {
panic!("Value is not a {}.", Self::type_path());
}
}

fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
*self = value.take()?;
Ok(())
}

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)
}

fn clone_value(&self) -> Box<dyn Reflect> {
Box::new(*self)
}

fn reflect_hash(&self) -> Option<u64> {
let mut hasher = reflect_hasher();
Hash::hash(&std::any::Any::type_id(self), &mut hasher);
Hash::hash(self, &mut hasher);
Some(hasher.finish())
}

fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
let value = value.as_any();
if let Some(value) = value.downcast_ref::<Self>() {
Some(std::cmp::PartialEq::eq(self, value))
} else {
Some(false)
}
}

fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self, f)
}
}

impl Typed for &'static str {
fn type_info() -> &'static TypeInfo {
static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
CELL.get_or_set(|| TypeInfo::Value(ValueInfo::new::<Self>()))
}
}

impl GetTypeRegistration for &'static str {
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
}
}

impl FromReflect for &'static str {
fn from_reflect(reflect: &dyn crate::Reflect) -> Option<Self> {
reflect.as_any().downcast_ref::<Self>().copied()
}
}

impl Reflect for &'static Path {
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
Some(<Self as Typed>::type_info())
Expand Down Expand Up @@ -1735,4 +1841,11 @@ mod tests {
let output = <&'static Path as FromReflect>::from_reflect(&path).unwrap();
assert_eq!(path, output);
}

#[test]
fn static_str_should_from_reflect() {
let expected = "Hello, World!";
let output = <&'static str as FromReflect>::from_reflect(&expected).unwrap();
assert_eq!(expected, output);
}
}

0 comments on commit 99f2ac4

Please sign in to comment.