Skip to content

Commit

Permalink
feat: allow constructing query modifiers in const contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Oct 15, 2023
1 parent a787f5f commit e5c69c4
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 33 deletions.
6 changes: 5 additions & 1 deletion asteroids/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ component! {
#[macroquad::main("Asteroids")]
async fn main() -> Result<()> {
registry()
.with(HierarchicalLayer::default())
.with(
HierarchicalLayer::default()
.with_span_retrace(true)
.with_deferred_spans(true),
)
.with(tracing_subscriber::filter::LevelFilter::INFO)
.init();

Expand Down
1 change: 1 addition & 0 deletions examples/guide/relations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use flax::{
components::{child_of, name},
relation::RelationExt,
*,
};
use itertools::Itertools;
Expand Down
3 changes: 2 additions & 1 deletion src/archetype/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ impl DerefMut for ChangeList {

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
/// Represents a change for a slice of entities for a specific component
pub(crate) enum ChangeKind {
#[doc(hidden)]
pub enum ChangeKind {
/// Component was modified
Modified = 0,
/// Component was added
Expand Down
9 changes: 6 additions & 3 deletions src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use crate::{
fetch::MaybeMut,
filter::{ChangeFilter, RemovedFilter, With, WithRelation, Without, WithoutRelation},
metadata::Metadata,
relation::RelationExt,
vtable::{ComponentVTable, UntypedVTable},
Entity, Mutable, RelationExt,
Entity, Mutable,
};

/// Trait alias for a 'static + Send + Sync type which can be used as a
Expand Down Expand Up @@ -248,7 +249,7 @@ impl<T: ComponentValue> Component<T> {
}

/// Transform this into a mutable fetch
pub fn as_mut(self) -> Mutable<T> {
pub const fn as_mut(self) -> Mutable<T> {
Mutable(self)
}

Expand All @@ -258,7 +259,9 @@ impl<T: ComponentValue> Component<T> {
}

/// Construct a fine grained change detection filter.
pub(crate) fn into_change_filter(self, kind: ChangeKind) -> ChangeFilter<T> {
///
/// Prefer [`TransformFetch`](crate::fetch::TransformFetch) if not in a const context
pub fn into_change_filter(self, kind: ChangeKind) -> ChangeFilter<T> {
ChangeFilter::new(self, kind)
}

Expand Down
3 changes: 2 additions & 1 deletion src/entity/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::{
buffer::ComponentBuffer,
component::{ComponentDesc, ComponentValue},
error::Result,
CommandBuffer, Component, Entity, RelationExt, World,
relation::RelationExt,
CommandBuffer, Component, Entity, World,
};
use alloc::{boxed::Box, vec::Vec};

Expand Down
4 changes: 2 additions & 2 deletions src/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use crate::{
error::MissingComponent,
format::EntityFormatter,
query::QueryOne,
relation::{RelationExt, RelationIter, RelationIterMut},
writer::{EntityWriter, FnWriter, Missing, Replace, SingleComponentWriter, WriteDedup},
Component, Entity, Fetch, RelationExt, World,
Component, Entity, Fetch, World,
};
use crate::{RelationIter, RelationIterMut};

/// Borrow all the components of an entity at once.
///
Expand Down
2 changes: 1 addition & 1 deletion src/fetch/as_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::vec::Vec;
use core::{fmt, ops::Deref};

/// Dereferences the fetch item
pub struct AsDeref<F>(pub(crate) F);
pub struct AsDeref<F>(pub F);

impl<'q, F, V> FetchItem<'q> for AsDeref<F>
where
Expand Down
2 changes: 1 addition & 1 deletion src/fetch/cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{FetchAccessData, FetchPrepareData, PreparedFetch, RandomFetch, Trans
///
/// This is useful as the query item is 'static
/// See [crate::Component::as_mut]
pub struct Cloned<F>(pub(crate) F);
pub struct Cloned<F>(pub F);

impl<'q, F> FetchItem<'q> for Cloned<F>
where
Expand Down
2 changes: 1 addition & 1 deletion src/fetch/copied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use super::{FetchAccessData, FetchPrepareData, PreparedFetch, RandomFetch, Trans
///
/// This is useful as the query item is 'static
/// See [crate::Component::as_mut]
pub struct Copied<F>(pub(crate) F);
pub struct Copied<F>(pub F);

impl<'q, F> FetchItem<'q> for Copied<F>
where
Expand Down
3 changes: 2 additions & 1 deletion src/fetch/ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::{
component::ComponentValue,
filter::{Cmp, Equal, Greater, GreaterEq, Less, LessEq},
Fetch, FetchItem, RelationExt,
relation::RelationExt,
Fetch, FetchItem,
};

use super::{
Expand Down
7 changes: 7 additions & 0 deletions src/fetch/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ pub struct Map<Q, F> {
pub(crate) func: F,
}

impl<Q, F> Map<Q, F> {
/// Creates a new mapped query
pub const fn new(query: Q, func: F) -> Self {
Self { query, func }
}
}

impl<'q, Q, F, T> FetchItem<'q> for Map<Q, F>
where
Q: FetchItem<'q>,
Expand Down
3 changes: 2 additions & 1 deletion src/fetch/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ pub struct OptOr<F, V> {
}

impl<F, V> OptOr<F, V> {
pub(crate) fn new(inner: F, or: V) -> Self {
/// Creates a new `OptOr` fetch modifier
pub const fn new(inner: F, or: V) -> Self {
Self {
fetch: inner,
value: or,
Expand Down
3 changes: 2 additions & 1 deletion src/fetch/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use smallvec::SmallVec;
use crate::{
archetype::{CellGuard, Slot},
component::{dummy, ComponentValue},
relation::RelationExt,
system::{Access, AccessKind},
Component, Entity, Fetch, FetchItem, RelationExt,
Component, Entity, Fetch, FetchItem,
};

use super::{FetchAccessData, FetchPrepareData, PreparedFetch};
Expand Down
2 changes: 1 addition & 1 deletion src/fetch/satisfied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{archetype::Slice, Fetch, FetchItem};
use super::{FetchAccessData, FmtQuery, PreparedFetch};

/// Yields true iff `F` would match the query
pub struct Satisfied<F>(pub(crate) F);
pub struct Satisfied<F>(pub F);

impl<'q, F: FetchItem<'q>> FetchItem<'q> for Satisfied<F> {
type Item = bool;
Expand Down
2 changes: 1 addition & 1 deletion src/fetch/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub struct Source<Q, S> {

impl<Q, S> Source<Q, S> {
/// Creates a new source fetch
pub fn new(fetch: Q, source: S) -> Self {
pub const fn new(fetch: Q, source: S) -> Self {
Self { fetch, source }
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/filter/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ trait CmpMethod<L> {

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Less<R>(pub(crate) R);
pub struct Less<R>(pub R);
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Greater<R>(pub(crate) R);
pub struct Greater<R>(pub R);
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct Equal<R>(pub(crate) R);
pub struct Equal<R>(pub R);
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct LessEq<R>(pub(crate) R);
pub struct LessEq<R>(pub R);
#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct GreaterEq<R>(pub(crate) R);
pub struct GreaterEq<R>(pub R);

impl<L, R> CmpMethod<L> for Less<R>
where
Expand Down Expand Up @@ -112,7 +112,7 @@ pub struct Cmp<F, C> {

impl<F, C> Cmp<F, C> {
/// Creates a new comparison filter
pub fn new(fetch: F, method: C) -> Self {
pub const fn new(fetch: F, method: C) -> Self {
Self { fetch, method }
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
//! details.
//!
//! ```rust
//! # use flax::*;
//! # use flax::{*, components::*};
//! component! {
//! child_of(parent): () => [ Debuggable ],
//! }
Expand Down Expand Up @@ -235,7 +235,8 @@ pub mod format;
pub mod metadata;
/// Query the world
pub mod query;
mod relation;
/// Low level relation construction
pub mod relation;
/// System execution
pub mod schedule;

Expand Down Expand Up @@ -268,7 +269,7 @@ pub use query::{
Children, Dfs, DfsBorrow, DfsIter, EntityBorrow, EntityQuery, Planar, Query, QueryBorrow,
QueryIter, Topo,
};
pub use relation::{Relation, RelationExt, RelationIter, RelationIterMut};
pub use relation::RelationExt;
pub use schedule::{Schedule, ScheduleBuilder, SystemInfo};
pub use system::{BoxedSystem, SharedResource, System, SystemBuilder};
pub use world::World;
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ macro_rules! component {
static VTABLE: &$crate::vtable::ComponentVTable<$ty> =
&$crate::vtable::ComponentVTable::new(stringify!($name), &META);
use $crate::entity::EntityKind;
use $crate::RelationExt;
use $crate::relation::RelationExt;
$crate::Component::static_init(&COMPONENT_ID, EntityKind::COMPONENT, VTABLE).of($obj)
}

Expand Down
3 changes: 2 additions & 1 deletion src/metadata/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ mod test {
use crate::{
entity_ids,
events::{Event, EventKind, EventSubscriber},
relations_like, Entity, EntityIds, Query, QueryBorrow, RelationExt, Relations, World,
relation::RelationExt,
relations_like, Entity, EntityIds, Query, QueryBorrow, Relations, World,
};
use alloc::{sync::Arc, vec, vec::Vec};
use itertools::Itertools;
Expand Down
3 changes: 2 additions & 1 deletion src/query/dfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use crate::{
component::{ComponentKey, ComponentValue},
fetch::{FetchAccessData, PreparedFetch},
filter::{All, Filtered},
relation::RelationExt,
system::{Access, AccessKind},
FetchItem,
};
use alloc::{collections::BTreeMap, vec::Vec};
use smallvec::SmallVec;

use crate::{Entity, Fetch, RelationExt, World};
use crate::{Entity, Fetch, World};

use super::{borrow::QueryBorrowState, Chunk, PreparedArchetype, QueryStrategy};

Expand Down
3 changes: 2 additions & 1 deletion src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ use crate::{
component::ComponentValue,
fetch::FmtQuery,
filter::{All, BatchSize, Filtered, With, WithRelation, Without, WithoutRelation},
relation::RelationExt,
system::Access,
util::TuplePush,
Component, Entity, Fetch, FetchItem, RelationExt, World,
Component, Entity, Fetch, FetchItem, World,
};
use alloc::vec::Vec;

Expand Down
3 changes: 2 additions & 1 deletion src/query/topo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use crate::{
component::ComponentValue,
fetch::{FetchAccessData, PreparedFetch},
filter::Filtered,
relation::RelationExt,
system::{Access, AccessKind},
Entity, Fetch, FetchItem, RelationExt, World,
Entity, Fetch, FetchItem, World,
};

use super::{
Expand Down
3 changes: 2 additions & 1 deletion src/query/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::{
component::ComponentValue,
fetch::FetchAccessData,
filter::{All, And, Filtered},
Entity, Fetch, FetchItem, RelationExt, World,
relation::RelationExt,
Entity, Fetch, FetchItem, World,
};
use alloc::{
collections::{BTreeMap, BTreeSet},
Expand Down
4 changes: 2 additions & 2 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use crate::{
events::EventSubscriber,
filter::{ArchetypeFilter, StaticFilter},
format::{EntitiesFormatter, HierarchyFormatter, WorldFormatter},
relation::Relation,
relation::{Relation, RelationExt},
writer::{
self, EntityWriter, FnWriter, Replace, ReplaceDyn, SingleComponentWriter, WriteDedup,
},
BatchSpawn, Component, ComponentVTable, Error, Fetch, Query, RefMut, RelationExt,
BatchSpawn, Component, ComponentVTable, Error, Fetch, Query, RefMut,
};

#[derive(Debug, Default)]
Expand Down
1 change: 1 addition & 0 deletions tests/relations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use flax::{
components::{child_of, name},
filter::All,
relation::RelationExt,
*,
};
use itertools::Itertools;
Expand Down

0 comments on commit e5c69c4

Please sign in to comment.