Skip to content

Commit

Permalink
Merge pull request #2 from CleanCut/implement-gettyperegistration
Browse files Browse the repository at this point in the history
impl GetTypeRegistration and FromType for Vec<T>
  • Loading branch information
CleanCut authored Mar 9, 2021
2 parents fc4e3b7 + 364ba51 commit 49b84e5
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
map_partial_eq, serde::Serializable, DynamicMap, List, ListIter, Map, MapIter, Reflect,
ReflectDeserialize, ReflectMut, ReflectRef,
map_partial_eq, serde::Serializable, DynamicMap, FromType, GetTypeRegistration, List, ListIter,
Map, MapIter, Reflect, ReflectDeserialize, ReflectMut, ReflectRef, TypeRegistration,
};

use bevy_reflect_derive::impl_reflect_value;
Expand Down Expand Up @@ -112,6 +112,14 @@ impl<T: Reflect> Reflect for Vec<T> {
}
}

impl<T: Reflect + for<'de> Deserialize<'de>> GetTypeRegistration for Vec<T> {
fn get_type_registration() -> TypeRegistration {
let mut registration = TypeRegistration::of::<Vec<T>>();
registration.insert::<ReflectDeserialize>(FromType::<Vec<T>>::from_type());
registration
}
}

impl<K: Reflect + Clone + Eq + Hash, V: Reflect + Clone> Map for HashMap<K, V> {
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
key.downcast_ref::<K>()
Expand Down Expand Up @@ -207,6 +215,18 @@ impl<K: Reflect + Clone + Eq + Hash, V: Reflect + Clone> Reflect for HashMap<K,
}
}

impl<K, V> GetTypeRegistration for HashMap<K, V>
where
K: Reflect + Clone + Eq + Hash + for<'de> Deserialize<'de>,
V: Reflect + Clone + for<'de> Deserialize<'de>,
{
fn get_type_registration() -> TypeRegistration {
let mut registration = TypeRegistration::of::<HashMap<K, V>>();
registration.insert::<ReflectDeserialize>(FromType::<HashMap<K, V>>::from_type());
registration
}
}

impl Reflect for Cow<'static, str> {
fn type_name(&self) -> &str {
std::any::type_name::<Self>()
Expand Down Expand Up @@ -266,3 +286,11 @@ impl Reflect for Cow<'static, str> {
Some(Serializable::Borrowed(self))
}
}

impl GetTypeRegistration for Cow<'static, str> {
fn get_type_registration() -> TypeRegistration {
let mut registration = TypeRegistration::of::<Cow<'static, str>>();
registration.insert::<ReflectDeserialize>(FromType::<Cow<'static, str>>::from_type());
registration
}
}

0 comments on commit 49b84e5

Please sign in to comment.