Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bevy_reflect: implement Reflect for SmolStr #8771

Merged
merged 9 commits into from
Jun 8, 2023
Merged
15 changes: 11 additions & 4 deletions crates/bevy_reflect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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"
Expand Down
28 changes: 28 additions & 0 deletions crates/bevy_reflect/src/impls/smol_str.rs
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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 = <SmolStr as FromReflect>::from_reflect(&smolstr);
assert_eq!(Some(smolstr), output);
}
}
3 changes: 3 additions & 0 deletions crates/bevy_reflect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down