Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"bench",
"examples/bevy-reflect",
"examples/bytes",
"examples/diesel",
"examples/macros",
Expand Down
3 changes: 2 additions & 1 deletion compact_str/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"
Expand Down
66 changes: 66 additions & 0 deletions compact_str/src/features/bevy_reflect.rs
Original file line number Diff line number Diff line change
@@ -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 = <CompactString as FromReflect>::from_reflect(&string);
assert_eq!(Some(string), output);
}

#[test]
fn compactstring_heap_should_from_reflect() {
let string = CompactString::new("abc".repeat(100));
let output = <CompactString as FromReflect>::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 = <MyTestComponentStruct as FromReflect>::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 = <MyTestComponentTuple as FromReflect>::from_reflect(&my_struct);
assert_eq!(Some(my_struct), output);
}
}
2 changes: 2 additions & 0 deletions compact_str/src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ mod smallvec;
mod sqlx;
#[cfg(feature = "zeroize")]
mod zeroize;
#[cfg(feature = "bevy-reflect")]
mod bevy_reflect;
9 changes: 9 additions & 0 deletions examples/bevy-reflect/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions examples/bevy-reflect/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading