Skip to content

Commit 7347ef9

Browse files
authored
Allow usage outside of the Bevy engine (ECS-only scenarios) (#48)
This should allow for people who wish to use `bevy_ecs` outside of the Bevy ecosystem to take advantage of this crate without pulling in dependencies that aren't necessary for their projects. The `bevy` feature is active by default so current users should be unaffected when upgrading, using this in an ECS-only scenario simply requires disabling default features. ```toml [dependencies.bevy-trait-query] default-features = false ``` Great crate by the way, has been extremely useful for a hobby project of mine so far!👍
1 parent 7042d7e commit 7347ef9

File tree

10 files changed

+84
-63
lines changed

10 files changed

+84
-63
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ jobs:
3030
toolchain: stable
3131
- name: Install alsa and udev
3232
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
33-
- name: Run tests
33+
- name: Run tests with minimal features
34+
run: cargo test --lib --no-default-features
35+
- name: "Run tests with default features"
3436
run: cargo test
3537

3638
clippy:

Cargo.toml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,35 @@ categories = ["game-development"]
1111

1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

14+
[features]
15+
default = ["bevy_app", "bevy_core"]
16+
1417
[dependencies]
1518
bevy-trait-query-impl = { path = "proc-macro", version = "0.4.0-dev" }
19+
tracing = "0.1"
1620

17-
[dependencies.bevy]
21+
[dependencies.bevy_ecs]
1822
git = "https://github.com/bevyengine/bevy"
1923
rev = "060711669903306c59eaea427498948992f0e768"
20-
default-features = false
24+
25+
[dependencies.bevy_app]
26+
git = "https://github.com/bevyengine/bevy"
27+
rev = "060711669903306c59eaea427498948992f0e768"
28+
optional = true
29+
30+
[dependencies.bevy_core]
31+
git = "https://github.com/bevyengine/bevy"
32+
rev = "060711669903306c59eaea427498948992f0e768"
33+
optional = true
2134

2235
[dev-dependencies]
2336
criterion = "0.5"
2437

38+
[dev-dependencies.bevy]
39+
git = "https://github.com/bevyengine/bevy"
40+
rev = "060711669903306c59eaea427498948992f0e768"
41+
default-features = false
42+
2543
[[bench]]
2644
name = "concrete"
2745
harness = false

benches/all.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::all)]
22

3-
use bevy::prelude::*;
3+
use bevy_core::Name;
4+
use bevy_ecs::prelude::*;
45
use bevy_trait_query::*;
56
use criterion::*;
67
use std::fmt::Display;
@@ -58,7 +59,7 @@ impl<'w> Benchmark<'w> {
5859
}
5960

6061
let query = world.query();
61-
Self(world, query, default())
62+
Self(world, query, Default::default())
6263
}
6364
fn multiple() -> Self {
6465
let mut world = World::new();
@@ -75,7 +76,7 @@ impl<'w> Benchmark<'w> {
7576
}
7677

7778
let query = world.query();
78-
Self(world, query, default())
79+
Self(world, query, Default::default())
7980
}
8081
// Queries with only one, and queries with multiple.
8182
pub fn distributed() -> Self {
@@ -99,7 +100,7 @@ impl<'w> Benchmark<'w> {
99100
}
100101

101102
let query = world.query();
102-
Self(world, query, default())
103+
Self(world, query, Default::default())
103104
}
104105

105106
pub fn run(&mut self) {

benches/concrete.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::all)]
22

3-
use bevy::prelude::*;
3+
use bevy_core::Name;
4+
use bevy_ecs::prelude::*;
45
use bevy_trait_query::*;
56
use criterion::*;
67
use std::fmt::Display;
@@ -52,7 +53,7 @@ impl<'w> Benchmark<'w> {
5253
}
5354

5455
let query = world.query();
55-
Self(world, query, default())
56+
Self(world, query, Default::default())
5657
}
5758
fn multiple() -> Self {
5859
let mut world = World::new();
@@ -66,7 +67,7 @@ impl<'w> Benchmark<'w> {
6667
}
6768

6869
let query = world.query();
69-
Self(world, query, default())
70+
Self(world, query, Default::default())
7071
}
7172

7273
pub fn run(&mut self) {

benches/fragmented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::all)]
22

3-
use bevy::prelude::*;
3+
use bevy_ecs::prelude::*;
44
use bevy_trait_query::*;
55
use criterion::*;
66
use std::fmt::Display;

benches/one.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![allow(clippy::all)]
22

3-
use bevy::prelude::*;
3+
use bevy_core::Name;
4+
use bevy_ecs::prelude::*;
45
use bevy_trait_query::*;
56
use criterion::*;
67
use std::fmt::Display;
@@ -58,7 +59,7 @@ impl<'w> Benchmark<'w> {
5859
}
5960

6061
let query = world.query();
61-
Self(world, query, default())
62+
Self(world, query, Default::default())
6263
}
6364
// There will be some entities that have multiple trait impls, and will be filtered out.
6465
pub fn filtered() -> Self {
@@ -82,7 +83,7 @@ impl<'w> Benchmark<'w> {
8283
}
8384

8485
let query = world.query();
85-
Self(world, query, default())
86+
Self(world, query, Default::default())
8687
}
8788

8889
pub fn run(&mut self) {

src/all.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use bevy::ecs::{
1+
use bevy_ecs::{
22
change_detection::{DetectChanges, Mut, Ref},
33
component::{ComponentId, Tick},
44
entity::Entity,
5+
ptr::UnsafeCellDeref,
56
query::{QueryItem, ReadOnlyWorldQuery, WorldQuery},
67
storage::{SparseSets, Table, TableRow},
78
world::{unsafe_world_cell::UnsafeWorldCell, World},
89
};
9-
use bevy::ptr::UnsafeCellDeref;
1010

1111
use crate::{
1212
debug_unreachable, trait_registry_error, zip_exact, TraitImplMeta, TraitImplRegistry,
@@ -484,16 +484,16 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
484484
unsafe fn set_archetype<'w>(
485485
fetch: &mut Self::Fetch<'w>,
486486
_state: &Self::State,
487-
_archetype: &'w bevy::ecs::archetype::Archetype,
488-
table: &'w bevy::ecs::storage::Table,
487+
_archetype: &'w bevy_ecs::archetype::Archetype,
488+
table: &'w bevy_ecs::storage::Table,
489489
) {
490490
fetch.table = Some(table);
491491
}
492492

493493
unsafe fn set_table<'w>(
494494
fetch: &mut Self::Fetch<'w>,
495495
_state: &Self::State,
496-
table: &'w bevy::ecs::storage::Table,
496+
table: &'w bevy_ecs::storage::Table,
497497
) {
498498
fetch.table = Some(table);
499499
}
@@ -519,7 +519,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
519519
#[inline]
520520
fn update_component_access(
521521
state: &Self::State,
522-
access: &mut bevy::ecs::query::FilteredAccess<ComponentId>,
522+
access: &mut bevy_ecs::query::FilteredAccess<ComponentId>,
523523
) {
524524
for &component in &*state.components {
525525
assert!(
@@ -534,8 +534,8 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a Trait> {
534534
#[inline]
535535
fn update_archetype_component_access(
536536
state: &Self::State,
537-
archetype: &bevy::ecs::archetype::Archetype,
538-
access: &mut bevy::ecs::query::Access<bevy::ecs::archetype::ArchetypeComponentId>,
537+
archetype: &bevy_ecs::archetype::Archetype,
538+
access: &mut bevy_ecs::query::Access<bevy_ecs::archetype::ArchetypeComponentId>,
539539
) {
540540
for &component in &*state.components {
541541
if let Some(archetype_component_id) = archetype.get_archetype_component_id(component) {
@@ -596,8 +596,8 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
596596
unsafe fn set_archetype<'w>(
597597
fetch: &mut Self::Fetch<'w>,
598598
_state: &Self::State,
599-
_archetype: &'w bevy::ecs::archetype::Archetype,
600-
table: &'w bevy::ecs::storage::Table,
599+
_archetype: &'w bevy_ecs::archetype::Archetype,
600+
table: &'w bevy_ecs::storage::Table,
601601
) {
602602
fetch.table = Some(table);
603603
}
@@ -606,7 +606,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
606606
unsafe fn set_table<'w>(
607607
fetch: &mut Self::Fetch<'w>,
608608
_state: &Self::State,
609-
table: &'w bevy::ecs::storage::Table,
609+
table: &'w bevy_ecs::storage::Table,
610610
) {
611611
fetch.table = Some(table);
612612
}
@@ -632,7 +632,7 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
632632
#[inline]
633633
fn update_component_access(
634634
state: &Self::State,
635-
access: &mut bevy::ecs::query::FilteredAccess<ComponentId>,
635+
access: &mut bevy_ecs::query::FilteredAccess<ComponentId>,
636636
) {
637637
for &component in &*state.components {
638638
assert!(
@@ -647,8 +647,8 @@ unsafe impl<'a, Trait: ?Sized + TraitQuery> WorldQuery for All<&'a mut Trait> {
647647
#[inline]
648648
fn update_archetype_component_access(
649649
state: &Self::State,
650-
archetype: &bevy::ecs::archetype::Archetype,
651-
access: &mut bevy::ecs::query::Access<bevy::ecs::archetype::ArchetypeComponentId>,
650+
archetype: &bevy_ecs::archetype::Archetype,
651+
access: &mut bevy_ecs::query::Access<bevy_ecs::archetype::ArchetypeComponentId>,
652652
) {
653653
for &component in &*state.components {
654654
if let Some(archetype_component_id) = archetype.get_archetype_component_id(component) {

src/lib.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//! ) {
3232
//! // ...
3333
//! }
34-
//! # bevy::ecs::system::assert_is_system(show_tooltips_system);
34+
//! # bevy_ecs::system::assert_is_system(show_tooltips_system);
3535
//! ```
3636
//!
3737
//! Since Rust unfortunately lacks any kind of reflection, it is necessary to register each
@@ -186,7 +186,7 @@
186186
//! println!("Tooltip: {}", tooltip.tooltip());
187187
//! }
188188
//! }
189-
//! # bevy::ecs::system::assert_is_system(show_tooltips);
189+
//! # bevy_ecs::system::assert_is_system(show_tooltips);
190190
//! ```
191191
//!
192192
//! Trait queries support basic change detection filtration. So to get all the components that
@@ -252,12 +252,9 @@
252252
//! | 1-2 matches | - | 16.959 µs | 82.179 µs |
253253
//!
254254
255-
use bevy::{
256-
ecs::{
257-
component::{ComponentId, StorageType},
258-
world::World,
259-
},
260-
prelude::*,
255+
use bevy_ecs::{
256+
component::{ComponentId, ComponentStorage, StorageType},
257+
prelude::{Component, Resource, World},
261258
ptr::{Ptr, PtrMut},
262259
};
263260

@@ -303,7 +300,7 @@ impl RegisterExt for World {
303300
{
304301
let component_id = self.init_component::<C>();
305302
let registry = self
306-
.get_resource_or_insert_with::<TraitImplRegistry<Trait>>(default)
303+
.get_resource_or_insert_with::<TraitImplRegistry<Trait>>(Default::default)
307304
.into_inner();
308305
let meta = TraitImplMeta {
309306
size_bytes: std::mem::size_of::<C>(),
@@ -314,7 +311,8 @@ impl RegisterExt for World {
314311
}
315312
}
316313

317-
impl RegisterExt for App {
314+
#[cfg(feature = "bevy_app")]
315+
impl RegisterExt for bevy_app::App {
318316
fn register_component_as<Trait: ?Sized + TraitQuery, C: Component>(&mut self) -> &mut Self
319317
where
320318
(C,): TraitQueryMarker<Trait, Covered = C>,
@@ -370,7 +368,6 @@ impl<Trait: ?Sized + TraitQuery> TraitImplRegistry<Trait> {
370368
self.components.push(component);
371369
self.meta.push(meta);
372370

373-
use bevy::ecs::component::ComponentStorage;
374371
match <C as Component>::Storage::STORAGE_TYPE {
375372
StorageType::Table => {
376373
self.table_components.push(component);
@@ -403,7 +400,7 @@ impl<T: ?Sized> Clone for TraitImplMeta<T> {
403400

404401
#[doc(hidden)]
405402
pub mod imports {
406-
pub use bevy::ecs::{
403+
pub use bevy_ecs::{
407404
archetype::{Archetype, ArchetypeComponentId},
408405
component::Tick,
409406
component::{Component, ComponentId},
@@ -426,7 +423,7 @@ impl<Trait: ?Sized + TraitQuery> TraitQueryState<Trait> {
426423
fn init(world: &mut World) -> Self {
427424
#[cold]
428425
fn missing_registry<T: ?Sized + 'static>() -> TraitImplRegistry<T> {
429-
warn!(
426+
tracing::warn!(
430427
"no components found matching `{}`, did you forget to register them?",
431428
std::any::type_name::<T>()
432429
);

0 commit comments

Comments
 (0)