From e6142fcfaa3832bb104c667c5eb22b59549bd48f Mon Sep 17 00:00:00 2001 From: TeamDman Date: Fri, 19 Sep 2025 00:25:54 -0400 Subject: [PATCH 01/10] feat: Add bevy-reflect feature --- compact_str/Cargo.toml | 3 +- compact_str/src/features/bevy_reflect.rs | 44 ++++++++++++++++++++++++ compact_str/src/features/mod.rs | 2 ++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 compact_str/src/features/bevy_reflect.rs diff --git a/compact_str/Cargo.toml b/compact_str/Cargo.toml index 8d9ea804..d520a4c2 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.0-rc.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..b2bba527 --- /dev/null +++ b/compact_str/src/features/bevy_reflect.rs @@ -0,0 +1,44 @@ +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 bevy_reflect::{FromReflect, PartialReflect}; + + use crate::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); + } +} 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 From c2b9181013df33cda5ab80e0d28567d1a2adad5a Mon Sep 17 00:00:00 2001 From: TeamDman Date: Fri, 19 Sep 2025 00:34:20 -0400 Subject: [PATCH 02/10] feat: Add bevy example --- Cargo.toml | 1 + examples/bevy/Cargo.toml | 11 +++++++++++ examples/bevy/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 examples/bevy/Cargo.toml create mode 100644 examples/bevy/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 3952be3f..68a07841 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "bench", + "examples/bevy", "examples/bytes", "examples/diesel", "examples/macros", diff --git a/examples/bevy/Cargo.toml b/examples/bevy/Cargo.toml new file mode 100644 index 00000000..c73826f0 --- /dev/null +++ b/examples/bevy/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "example-bevy" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +compact_str = { version = "0.9.0", features = ["bevy-reflect"], path="../../compact_str" } +bevy_ecs = "0.17.0-rc.1" +bevy_reflect = "0.17.0-rc.1" diff --git a/examples/bevy/src/main.rs b/examples/bevy/src/main.rs new file mode 100644 index 00000000..2ec741f1 --- /dev/null +++ b/examples/bevy/src/main.rs @@ -0,0 +1,34 @@ +use bevy_ecs::component::Component; +use bevy_ecs::world::World; +use bevy_reflect::Reflect; +use compact_str::CompactString; +use bevy_ecs::prelude::Entity; + +#[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); + } +} + +// fn main() { +// let word = "hello world!"; + +// // Cursor<&[u8]> is `bytes::Buf` +// let mut buf = Cursor::new(word.as_bytes()); +// // `from_utf8_buf(...)` can fail, if the provided buffer is not valid UTF-8 +// let compact_str = CompactString::from_utf8_buf(&mut buf).expect("valid utf-8"); + +// assert_eq!(compact_str, word); + +// println!("{}", compact_str); +// } From b4720fa4aa03db08f33d8be57b64085088b9656d Mon Sep 17 00:00:00 2001 From: TeamDman Date: Fri, 19 Sep 2025 00:37:49 -0400 Subject: [PATCH 03/10] chore: Clean up bevy-reflect feature --- examples/bevy/Cargo.toml | 11 ----------- examples/bevy/src/main.rs | 34 ---------------------------------- 2 files changed, 45 deletions(-) delete mode 100644 examples/bevy/Cargo.toml delete mode 100644 examples/bevy/src/main.rs diff --git a/examples/bevy/Cargo.toml b/examples/bevy/Cargo.toml deleted file mode 100644 index c73826f0..00000000 --- a/examples/bevy/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "example-bevy" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -compact_str = { version = "0.9.0", features = ["bevy-reflect"], path="../../compact_str" } -bevy_ecs = "0.17.0-rc.1" -bevy_reflect = "0.17.0-rc.1" diff --git a/examples/bevy/src/main.rs b/examples/bevy/src/main.rs deleted file mode 100644 index 2ec741f1..00000000 --- a/examples/bevy/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -use bevy_ecs::component::Component; -use bevy_ecs::world::World; -use bevy_reflect::Reflect; -use compact_str::CompactString; -use bevy_ecs::prelude::Entity; - -#[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); - } -} - -// fn main() { -// let word = "hello world!"; - -// // Cursor<&[u8]> is `bytes::Buf` -// let mut buf = Cursor::new(word.as_bytes()); -// // `from_utf8_buf(...)` can fail, if the provided buffer is not valid UTF-8 -// let compact_str = CompactString::from_utf8_buf(&mut buf).expect("valid utf-8"); - -// assert_eq!(compact_str, word); - -// println!("{}", compact_str); -// } From d538f0303d3ec24fa2165719f440dafb92d9cb03 Mon Sep 17 00:00:00 2001 From: TeamDman Date: Fri, 19 Sep 2025 00:39:22 -0400 Subject: [PATCH 04/10] chore: Restore bevy-reflect feature example --- examples/bevy-reflect/Cargo.toml | 9 +++++++++ examples/bevy-reflect/src/main.rs | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 examples/bevy-reflect/Cargo.toml create mode 100644 examples/bevy-reflect/src/main.rs diff --git a/examples/bevy-reflect/Cargo.toml b/examples/bevy-reflect/Cargo.toml new file mode 100644 index 00000000..467d69fa --- /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.0-rc.1" +bevy_reflect = "0.17.0-rc.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); + } +} From 4810ca116100c16c3c04752600d8eaf014a0f3d0 Mon Sep 17 00:00:00 2001 From: TeamDman Date: Sat, 20 Sep 2025 22:47:29 -0400 Subject: [PATCH 05/10] Fix bevy example workspace member --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 68a07841..bcee343a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ "bench", - "examples/bevy", + "examples/bevy-reflect", "examples/bytes", "examples/diesel", "examples/macros", From 68bc8c4066621e8b8d7b27cf13a39f4252860dd1 Mon Sep 17 00:00:00 2001 From: TeamDman Date: Sun, 28 Sep 2025 10:47:51 -0400 Subject: [PATCH 06/10] Update to bevy-reflect 0.17.0-rc.2 --- compact_str/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compact_str/Cargo.toml b/compact_str/Cargo.toml index d520a4c2..0d10f4d5 100644 --- a/compact_str/Cargo.toml +++ b/compact_str/Cargo.toml @@ -45,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.0-rc.1", optional = true} +bevy_reflect = {version = "0.17.0-rc.2", optional = true} castaway = { version = "0.2.3", default-features = false, features = ["alloc"] } cfg-if = "1" itoa = "1" From bcf39d4b885e2e8afcde9cd36c250cc1b7ea7e3f Mon Sep 17 00:00:00 2001 From: TeamDman Date: Mon, 29 Sep 2025 20:02:59 -0400 Subject: [PATCH 07/10] Add more tests ensuring derive macro functions as expected --- compact_str/src/features/bevy_reflect.rs | 27 ++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/compact_str/src/features/bevy_reflect.rs b/compact_str/src/features/bevy_reflect.rs index b2bba527..ff07ecc0 100644 --- a/compact_str/src/features/bevy_reflect.rs +++ b/compact_str/src/features/bevy_reflect.rs @@ -15,9 +15,16 @@ impl_reflect_opaque!((in crate::CompactString)CompactString( #[cfg(test)] mod tests { - use bevy_reflect::{FromReflect, PartialReflect}; - 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() { @@ -41,4 +48,20 @@ mod tests { 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); + } } From 60cd3f08a1b10b81531d602c4a30d5f597b55ae5 Mon Sep 17 00:00:00 2001 From: TeamDman Date: Thu, 2 Oct 2025 00:10:37 -0400 Subject: [PATCH 08/10] Update to Bevy 0.17.1 --- compact_str/Cargo.toml | 2 +- examples/bevy-reflect/Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compact_str/Cargo.toml b/compact_str/Cargo.toml index 0d10f4d5..74de09b7 100644 --- a/compact_str/Cargo.toml +++ b/compact_str/Cargo.toml @@ -45,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.0-rc.2", optional = true} +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/examples/bevy-reflect/Cargo.toml b/examples/bevy-reflect/Cargo.toml index 467d69fa..665af290 100644 --- a/examples/bevy-reflect/Cargo.toml +++ b/examples/bevy-reflect/Cargo.toml @@ -5,5 +5,5 @@ edition = "2021" [dependencies] compact_str = { version = "0.9.0", features = ["bevy-reflect"], path="../../compact_str" } -bevy_ecs = "0.17.0-rc.1" -bevy_reflect = "0.17.0-rc.1" +bevy_ecs = "0.17.1" +bevy_reflect = "0.17.1" From ca84351fad379092f5958db3469ec0f261c8db11 Mon Sep 17 00:00:00 2001 From: TeamDman Date: Thu, 2 Oct 2025 16:05:12 -0400 Subject: [PATCH 09/10] Update compact_str/src/features/bevy_reflect.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- compact_str/src/features/bevy_reflect.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compact_str/src/features/bevy_reflect.rs b/compact_str/src/features/bevy_reflect.rs index ff07ecc0..8454fe58 100644 --- a/compact_str/src/features/bevy_reflect.rs +++ b/compact_str/src/features/bevy_reflect.rs @@ -22,7 +22,6 @@ mod tests { struct MyTestComponentStruct { pub value: CompactString, } - #[derive(Debug, Reflect, Eq, PartialEq)] struct MyTestComponentTuple(pub CompactString); From 76c987da5b9409cc32d1a26c2763237525aae235 Mon Sep 17 00:00:00 2001 From: TeamDman Date: Thu, 2 Oct 2025 16:05:22 -0400 Subject: [PATCH 10/10] Update compact_str/Cargo.toml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- compact_str/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compact_str/Cargo.toml b/compact_str/Cargo.toml index 74de09b7..2fed1a8c 100644 --- a/compact_str/Cargo.toml +++ b/compact_str/Cargo.toml @@ -45,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} +bevy_reflect = { version = "0.17.1", optional = true } castaway = { version = "0.2.3", default-features = false, features = ["alloc"] } cfg-if = "1" itoa = "1"