Skip to content

Commit 71be08a

Browse files
authored
bevy_reflect: Reflect &'static str (#11686)
# 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>`
1 parent 08654ad commit 71be08a

File tree

1 file changed

+113
-0
lines changed
  • crates/bevy_reflect/src/impls

1 file changed

+113
-0
lines changed

crates/bevy_reflect/src/impls/std.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,10 @@ impl Reflect for Cow<'static, str> {
11111111
Some(false)
11121112
}
11131113
}
1114+
1115+
fn debug(&self, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
1116+
fmt::Debug::fmt(self, f)
1117+
}
11141118
}
11151119

11161120
impl Typed for Cow<'static, str> {
@@ -1305,6 +1309,108 @@ impl<T: FromReflect + Clone + TypePath> FromReflect for Cow<'static, [T]> {
13051309
}
13061310
}
13071311

1312+
impl Reflect for &'static str {
1313+
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1314+
Some(<Self as Typed>::type_info())
1315+
}
1316+
1317+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
1318+
self
1319+
}
1320+
1321+
fn as_any(&self) -> &dyn Any {
1322+
self
1323+
}
1324+
1325+
fn as_any_mut(&mut self) -> &mut dyn Any {
1326+
self
1327+
}
1328+
1329+
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
1330+
self
1331+
}
1332+
1333+
fn as_reflect(&self) -> &dyn Reflect {
1334+
self
1335+
}
1336+
1337+
fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
1338+
self
1339+
}
1340+
1341+
fn apply(&mut self, value: &dyn Reflect) {
1342+
let value = value.as_any();
1343+
if let Some(&value) = value.downcast_ref::<Self>() {
1344+
*self = value;
1345+
} else {
1346+
panic!("Value is not a {}.", Self::type_path());
1347+
}
1348+
}
1349+
1350+
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
1351+
*self = value.take()?;
1352+
Ok(())
1353+
}
1354+
1355+
fn reflect_ref(&self) -> ReflectRef {
1356+
ReflectRef::Value(self)
1357+
}
1358+
1359+
fn reflect_mut(&mut self) -> ReflectMut {
1360+
ReflectMut::Value(self)
1361+
}
1362+
1363+
fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1364+
ReflectOwned::Value(self)
1365+
}
1366+
1367+
fn clone_value(&self) -> Box<dyn Reflect> {
1368+
Box::new(*self)
1369+
}
1370+
1371+
fn reflect_hash(&self) -> Option<u64> {
1372+
let mut hasher = reflect_hasher();
1373+
Hash::hash(&std::any::Any::type_id(self), &mut hasher);
1374+
Hash::hash(self, &mut hasher);
1375+
Some(hasher.finish())
1376+
}
1377+
1378+
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool> {
1379+
let value = value.as_any();
1380+
if let Some(value) = value.downcast_ref::<Self>() {
1381+
Some(std::cmp::PartialEq::eq(self, value))
1382+
} else {
1383+
Some(false)
1384+
}
1385+
}
1386+
1387+
fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1388+
fmt::Debug::fmt(&self, f)
1389+
}
1390+
}
1391+
1392+
impl Typed for &'static str {
1393+
fn type_info() -> &'static TypeInfo {
1394+
static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
1395+
CELL.get_or_set(|| TypeInfo::Value(ValueInfo::new::<Self>()))
1396+
}
1397+
}
1398+
1399+
impl GetTypeRegistration for &'static str {
1400+
fn get_type_registration() -> TypeRegistration {
1401+
let mut registration = TypeRegistration::of::<Self>();
1402+
registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
1403+
registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
1404+
registration
1405+
}
1406+
}
1407+
1408+
impl FromReflect for &'static str {
1409+
fn from_reflect(reflect: &dyn crate::Reflect) -> Option<Self> {
1410+
reflect.as_any().downcast_ref::<Self>().copied()
1411+
}
1412+
}
1413+
13081414
impl Reflect for &'static Path {
13091415
fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
13101416
Some(<Self as Typed>::type_info())
@@ -1735,4 +1841,11 @@ mod tests {
17351841
let output = <&'static Path as FromReflect>::from_reflect(&path).unwrap();
17361842
assert_eq!(path, output);
17371843
}
1844+
1845+
#[test]
1846+
fn static_str_should_from_reflect() {
1847+
let expected = "Hello, World!";
1848+
let output = <&'static str as FromReflect>::from_reflect(&expected).unwrap();
1849+
assert_eq!(expected, output);
1850+
}
17381851
}

0 commit comments

Comments
 (0)