diff --git a/Cargo.toml b/Cargo.toml index 3952be3f..bcee343a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "bench", + "examples/bevy-reflect", "examples/bytes", "examples/diesel", "examples/macros", diff --git a/compact_str/Cargo.toml b/compact_str/Cargo.toml index 8d9ea804..2fed1a8c 100644 --- a/compact_str/Cargo.toml +++ b/compact_str/Cargo.toml @@ -30,6 +30,7 @@ sqlx-mysql = ["sqlx", "sqlx/mysql"] sqlx-postgres = ["sqlx", "sqlx/postgres"] sqlx-sqlite = ["sqlx", "sqlx/sqlite"] zeroize = ["dep:zeroize"] +bevy-reflect = ["dep:bevy_reflect", "serde"] [dependencies] arbitrary = { version = "1", optional = true, default-features = false } @@ -44,7 +45,7 @@ serde = { version = "1", optional = true, default-features = false, features = [ smallvec = { version = "1", optional = true, features = ["union"] } sqlx = { version = "0.8", optional = true, default-features = false } zeroize = { version = "1", optional = true, default-features = false } - +bevy_reflect = { version = "0.17.1", optional = true } castaway = { version = "0.2.3", default-features = false, features = ["alloc"] } cfg-if = "1" itoa = "1" diff --git a/compact_str/src/features/bevy_reflect.rs b/compact_str/src/features/bevy_reflect.rs new file mode 100644 index 00000000..8454fe58 --- /dev/null +++ b/compact_str/src/features/bevy_reflect.rs @@ -0,0 +1,66 @@ +use crate::CompactString; +use bevy_reflect::{ + impl_reflect_opaque, std_traits::ReflectDefault, ReflectDeserialize, ReflectSerialize, +}; + +impl_reflect_opaque!((in crate::CompactString)CompactString( + Clone, + Debug, + Hash, + PartialEq, + Default, + Serialize, + Deserialize, +)); + +#[cfg(test)] +mod tests { + use crate::CompactString; + use bevy_reflect::{FromReflect, PartialReflect, Reflect}; + + #[derive(Debug, Reflect, Eq, PartialEq)] + struct MyTestComponentStruct { + pub value: CompactString, + } + #[derive(Debug, Reflect, Eq, PartialEq)] + struct MyTestComponentTuple(pub CompactString); + + #[test] + fn should_partial_eq_compactstring() { + let a: &dyn PartialReflect = &CompactString::new("A"); + let a2: &dyn PartialReflect = &CompactString::new("A"); + let b: &dyn PartialReflect = &CompactString::new("B"); + assert_eq!(Some(true), a.reflect_partial_eq(a2)); + assert_eq!(Some(false), a.reflect_partial_eq(b)); + } + + #[test] + fn compactstring_should_from_reflect() { + let string = CompactString::new("hello_world.rs"); + let output = ::from_reflect(&string); + assert_eq!(Some(string), output); + } + + #[test] + fn compactstring_heap_should_from_reflect() { + let string = CompactString::new("abc".repeat(100)); + let output = ::from_reflect(&string); + assert_eq!(Some(string), output); + } + + #[test] + fn struct_with_compactstring_should_from_reflect() { + let string = CompactString::new("hello_world.rs"); + let my_struct = MyTestComponentStruct { value: string }; + let output = ::from_reflect(&my_struct); + assert_eq!(Some(my_struct), output); + } + + #[test] + fn tuple_with_compactstring_should_from_reflect() { + let string = CompactString::new("hello_world.rs"); + let my_struct = MyTestComponentTuple(string); + let output = ::from_reflect(&my_struct); + assert_eq!(Some(my_struct), output); + } +} diff --git a/compact_str/src/features/mod.rs b/compact_str/src/features/mod.rs index 1953d2fe..1c179ea2 100644 --- a/compact_str/src/features/mod.rs +++ b/compact_str/src/features/mod.rs @@ -24,3 +24,5 @@ mod smallvec; mod sqlx; #[cfg(feature = "zeroize")] mod zeroize; +#[cfg(feature = "bevy-reflect")] +mod bevy_reflect; \ No newline at end of file diff --git a/examples/bevy-reflect/Cargo.toml b/examples/bevy-reflect/Cargo.toml new file mode 100644 index 00000000..665af290 --- /dev/null +++ b/examples/bevy-reflect/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "example-bevy" +version = "0.1.0" +edition = "2021" + +[dependencies] +compact_str = { version = "0.9.0", features = ["bevy-reflect"], path="../../compact_str" } +bevy_ecs = "0.17.1" +bevy_reflect = "0.17.1" diff --git a/examples/bevy-reflect/src/main.rs b/examples/bevy-reflect/src/main.rs new file mode 100644 index 00000000..0711e274 --- /dev/null +++ b/examples/bevy-reflect/src/main.rs @@ -0,0 +1,21 @@ +use bevy_ecs::component::Component; +use bevy_ecs::prelude::Entity; +use bevy_ecs::world::World; +use bevy_reflect::Reflect; +use compact_str::CompactString; + +#[derive(Component, Reflect, Debug)] +pub struct Thing(pub CompactString); + +pub fn main() { + let mut world = World::default(); + + let thing1 = Thing(CompactString::new("Hello, world!")); + let thing2 = Thing(CompactString::new("Goodbye, world!")); + world.spawn(thing1); + world.spawn(thing2); + + for thing in world.query::<(Entity, &Thing)>().iter(&world) { + println!("{:?}", thing); + } +}