Skip to content

Commit

Permalink
bevy_reflect: implement Reflect for SmolStr (#8771)
Browse files Browse the repository at this point in the history
# Objective
To upgrade winit's dependency, it's useful to reuse SmolStr, which
replaces/improves the too restrictive Key letter enums.

As Input<Key> is a resource it should implement Reflect through all its
fields.

## Solution

Add smol_str to bevy_reflect supported types, behind a feature flag.

This PR blocks winit's upgrade PR:
#8745.

# Current state

- I'm discovering bevy_reflect, I appreciate all feedbacks, and send me
your nitpicks!
- Lacking more tests

---------

Co-authored-by: Alice Cecile <[email protected]>
Co-authored-by: Gino Valente <[email protected]>
  • Loading branch information
3 people authored Jun 8, 2023
1 parent 0080303 commit b559e9b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
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
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

0 comments on commit b559e9b

Please sign in to comment.