diff --git a/crates/bevy_reflect/Cargo.toml b/crates/bevy_reflect/Cargo.toml index 2736045723..d7b193beed 100644 --- a/crates/bevy_reflect/Cargo.toml +++ b/crates/bevy_reflect/Cargo.toml @@ -11,14 +11,16 @@ readme = "README.md" [features] default = [] -# Provides Bevy-related reflection implementations -bevy = ["glam", "smallvec", "bevy_math"] +# When enabled, provides Bevy-related reflection implementations +bevy = ["glam", "smallvec", "bevy_math", "smol_str"] # When enabled, allows documentation comments to be accessed via reflection documentation = ["bevy_reflect_derive/documentation"] [dependencies] # bevy -bevy_math = { path = "../bevy_math", version = "0.11.0-dev", features = ["serialize"], optional = true } +bevy_math = { path = "../bevy_math", version = "0.11.0-dev", features = [ + "serialize", +], optional = true } bevy_reflect_derive = { path = "bevy_reflect_derive", version = "0.11.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.11.0-dev" } bevy_ptr = { path = "../bevy_ptr", version = "0.11.0-dev" } @@ -30,8 +32,13 @@ parking_lot = "0.12.1" thiserror = "1.0" once_cell = "1.11" serde = "1" -smallvec = { version = "1.6", features = ["serde", "union", "const_generics"], optional = true } +smallvec = { version = "1.6", features = [ + "serde", + "union", + "const_generics", +], optional = true } glam = { version = "0.24", features = ["serde"], optional = true } +smol_str = { version = "0.2.0", optional = true } [dev-dependencies] ron = "0.8.0" diff --git a/crates/bevy_reflect/src/impls/smol_str.rs b/crates/bevy_reflect/src/impls/smol_str.rs new file mode 100644 index 0000000000..3dd617a06c --- /dev/null +++ b/crates/bevy_reflect/src/impls/smol_str.rs @@ -0,0 +1,28 @@ +use crate::std_traits::ReflectDefault; +use crate::{self as bevy_reflect}; +use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value}; + +impl_reflect_value!(::smol_str::SmolStr(Debug, Hash, PartialEq, Default)); +impl_from_reflect_value!(::smol_str::SmolStr); + +#[cfg(test)] +mod tests { + use crate::{FromReflect, Reflect}; + use smol_str::SmolStr; + + #[test] + fn should_partial_eq_smolstr() { + let a: &dyn Reflect = &SmolStr::new("A"); + let a2: &dyn Reflect = &SmolStr::new("A"); + let b: &dyn Reflect = &SmolStr::new("B"); + assert_eq!(Some(true), a.reflect_partial_eq(a2)); + assert_eq!(Some(false), a.reflect_partial_eq(b)); + } + + #[test] + fn smolstr_should_from_reflect() { + let smolstr = SmolStr::new("hello_world.rs"); + let output = ::from_reflect(&smolstr); + assert_eq!(Some(smolstr), output); + } +} diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 1322142796..bda0fd48a5 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -459,6 +459,9 @@ mod impls { mod rect; #[cfg(feature = "smallvec")] mod smallvec; + #[cfg(feature = "smol_str")] + mod smol_str; + mod std; #[cfg(feature = "glam")]