Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: naming #25

Merged
merged 2 commits into from
Nov 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ let regen_system = System::builder()
.boxed();

let despawn_system = System::builder()
.with_query(Query::new(entity_ids()).filter(health().le(0.0)))
.with_query(Query::new(entity_ids()).with_filter(health().le(0.0)))
.with_cmd_mut()
.build(|mut q: QueryBorrow<EntityIds, _>, cmd: &mut CommandBuffer| {
for id in &mut q {
6 changes: 3 additions & 3 deletions benches/common/serialize_binary.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use std::iter::repeat;
use bincode::Options;
use flax::{
component,
serialize::{SerdeBuilder, SerializeFormat},
serialize::{SerializationContextBuilder, SerializeFormat},
BatchSpawn, World,
};
use serde::{Deserialize, Serialize};
@@ -58,7 +58,7 @@ impl Benchmark {
pub fn run_col(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
@@ -80,7 +80,7 @@ impl Benchmark {
pub fn run_row(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
4 changes: 2 additions & 2 deletions benches/common/serialize_text.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use std::iter::repeat;

use flax::{
component,
serialize::{SerdeBuilder, SerializeFormat},
serialize::{SerializationContextBuilder, SerializeFormat},
BatchSpawn, World,
};

@@ -58,7 +58,7 @@ impl Benchmark {
pub fn run_col(&mut self) {
let Self(world) = self;

let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(transform())
.with(position())
.with(rotation())
6 changes: 3 additions & 3 deletions examples/guide/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flax::{
component, entity_ids, CommandBuffer, Component, Debuggable, Entity, EntityBorrow, FetchExt,
Mutable, Query, QueryBorrow, Schedule, System, World,
component, entity_ids, CommandBuffer, Component, ComponentMut, Debuggable, Entity,
EntityBorrow, FetchExt, Query, QueryBorrow, Schedule, System, World,
};
use glam::{vec2, Vec2};
use rand::{rngs::StdRng, Rng, SeedableRng};
@@ -99,7 +99,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(query)
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
4 changes: 2 additions & 2 deletions examples/guide/serialize.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ fn main() -> anyhow::Result<()> {

tracing_subscriber::fmt().init();

use flax::serialize::{SerdeBuilder, SerializeFormat};
use flax::serialize::{SerializationContextBuilder, SerializeFormat};
tracing::info!("It works");

let mut world = World::new();
@@ -45,7 +45,7 @@ fn main() -> anyhow::Result<()> {
// ANCHOR_END: setup

// ANCHOR: serialize
let (serializer, deserializer) = SerdeBuilder::new()
let (serializer, deserializer) = SerializationContextBuilder::new()
.with(name())
.with(position())
.with(velocity())
8 changes: 4 additions & 4 deletions examples/guide/systems.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use flax::{
component, entity_ids, BoxedSystem, CommandBuffer, Component, Debuggable, Entity, FetchExt,
Mutable, Query, QueryBorrow, Schedule, System, World,
component, entity_ids, BoxedSystem, CommandBuffer, Component, ComponentMut, Debuggable, Entity,
FetchExt, Query, QueryBorrow, Schedule, System, World,
};
use glam::{vec2, Vec2};
use rand::{rngs::StdRng, Rng, SeedableRng};
@@ -31,7 +31,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(Query::new((entity_ids(), position(), distance().as_mut())))
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
@@ -48,7 +48,7 @@ fn main() -> anyhow::Result<()> {
.with_name("update_distance")
.with_query(Query::new((entity_ids(), position(), distance().as_mut())))
.build(
|mut query: QueryBorrow<(_, Component<Vec2>, Mutable<f32>), _>| {
|mut query: QueryBorrow<(_, Component<Vec2>, ComponentMut<f32>), _>| {
for (id, pos, dist) in &mut query {
println!("Updating distance for {id} with position: {pos:?}");
*dist = pos.length();
2 changes: 1 addition & 1 deletion examples/query/transform.rs
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ fn main() {
Query::new((world_position().as_mut(), position())).with_strategy(Dfs::new(child_of)),
)
.build(
|mut query: DfsBorrow<(Mutable<Vec3>, Component<Vec3>), All, ()>| {
|mut query: DfsBorrow<(ComponentMut<Vec3>, Component<Vec3>), All, ()>| {
query.traverse(&Vec3::ZERO, |(world_pos, &pos), _, &parent_pos| {
*world_pos = pos + parent_pos;
*world_pos
2 changes: 1 addition & 1 deletion src/archetypes.rs
Original file line number Diff line number Diff line change
@@ -273,7 +273,7 @@ pub(crate) struct ArchetypeIndex {
}

impl Debug for ArchetypeIndex {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ArchetypeIndex")
.field("components", &self.components)
.finish()
6 changes: 3 additions & 3 deletions src/component.rs
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ use crate::{
metadata::Metadata,
relation::RelationExt,
vtable::{ComponentVTable, UntypedVTable},
Entity, Mutable,
Entity, ComponentMut,
};

/// Trait alias for a 'static + Send + Sync type which can be used as a
@@ -260,8 +260,8 @@ impl<T: ComponentValue> Component<T> {
}

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

/// Transform this into a (maybe) mutable fetch
29 changes: 24 additions & 5 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::vec::Vec;
use bitflags::bitflags;
use itertools::Itertools;

use crate::{
@@ -37,6 +38,18 @@ impl Event {
}
}

bitflags! {
/// The type of ECS event
pub struct EventKindFilter: u8 {
/// Component was added
const ADDED = 1;
/// Component was removed
const REMOVED = 2;
/// Component was modified
const MODIFIED = 4;
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// The type of ECS event
pub enum EventKind {
@@ -139,8 +152,9 @@ pub trait EventSubscriber: ComponentValue {
subscriber: self,
}
}

/// Filter a subscriber to only receive events of a specific kind
fn filter_event_kind(self, event_kind: EventKind) -> FilterEventKind<Self>
fn filter_event_kind(self, event_kind: EventKindFilter) -> FilterEventKind<Self>
where
Self: Sized,
{
@@ -426,7 +440,7 @@ where

/// Filter a subscriber to only receive events of a specific kind
pub struct FilterEventKind<S> {
event_kind: EventKind,
event_kind: EventKindFilter,
subscriber: S,
}

@@ -435,23 +449,28 @@ where
S: EventSubscriber,
{
fn on_added(&self, storage: &Storage, event: &EventData) {
if self.event_kind == EventKind::Added {
if self.event_kind.contains(EventKindFilter::ADDED) {
self.subscriber.on_added(storage, event)
}
}

fn on_modified(&self, event: &EventData) {
if self.event_kind == EventKind::Modified {
if self.event_kind.contains(EventKindFilter::MODIFIED) {
self.subscriber.on_modified(event)
}
}

fn on_removed(&self, storage: &Storage, event: &EventData) {
if self.event_kind == EventKind::Removed {
if self.event_kind.contains(EventKindFilter::REMOVED) {
self.subscriber.on_removed(storage, event)
}
}

#[inline]
fn matches_component(&self, desc: ComponentDesc) -> bool {
self.subscriber.matches_component(desc)
}

fn is_connected(&self) -> bool {
self.subscriber.is_connected()
}
6 changes: 3 additions & 3 deletions src/fetch/component_mut.rs
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@ use super::{FetchAccessData, FetchPrepareData, PreparedFetch};
#[derive(Debug, Clone)]
/// Mutable component fetch
/// See [crate::Component::as_mut]
pub struct Mutable<T>(pub(crate) Component<T>);
pub struct ComponentMut<T>(pub(crate) Component<T>);

impl<'w, T> Fetch<'w> for Mutable<T>
impl<'w, T> Fetch<'w> for ComponentMut<T>
where
T: ComponentValue,
{
@@ -64,7 +64,7 @@ where
}
}

impl<'q, T: ComponentValue> FetchItem<'q> for Mutable<T> {
impl<'q, T: ComponentValue> FetchItem<'q> for ComponentMut<T> {
type Item = &'q mut T;
}

16 changes: 8 additions & 8 deletions src/fetch/transform.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use crate::{
archetype::ChangeKind,
component::ComponentValue,
filter::{ChangeFilter, Filtered, NoEntities, Union},
Component, EntityIds, FetchExt, Mutable,
Component, EntityIds, FetchExt, ComponentMut,
};

/// Allows transforming a fetch into another.
@@ -32,14 +32,14 @@ impl<T: ComponentValue> TransformFetch<Added> for Component<T> {
}
}

impl<T: ComponentValue> TransformFetch<Modified> for Mutable<T> {
impl<T: ComponentValue> TransformFetch<Modified> for ComponentMut<T> {
type Output = Filtered<Self, NoEntities>;
fn transform_fetch(self, _: Modified) -> Self::Output {
self.filtered(NoEntities)
}
}

impl<T: ComponentValue> TransformFetch<Added> for Mutable<T> {
impl<T: ComponentValue> TransformFetch<Added> for ComponentMut<T> {
type Output = Filtered<Self, NoEntities>;
fn transform_fetch(self, _: Added) -> Self::Output {
self.filtered(NoEntities)
@@ -179,7 +179,7 @@ mod tests {
#[test]
#[cfg(feature = "derive")]
fn query_modified_struct() {
use crate::{fetch::Cloned, Component, Fetch, Mutable, Opt};
use crate::{fetch::Cloned, Component, Fetch, ComponentMut, Opt};

component! {
a: i32,
@@ -193,8 +193,8 @@ mod tests {
struct MyFetch {
a: Component<i32>,
b: Cloned<Component<String>>,
c: Mutable<f32>,
other: Opt<Mutable<()>>,
c: ComponentMut<f32>,
other: Opt<ComponentMut<()>>,
}

let mut world = World::new();
@@ -273,7 +273,7 @@ mod tests {
#[test]
#[cfg(feature = "derive")]
fn query_inserted_struct() {
use crate::{fetch::Cloned, Component, EntityIds, Fetch, Mutable};
use crate::{fetch::Cloned, Component, EntityIds, Fetch, ComponentMut};

#[derive(Debug)]
struct Custom;
@@ -294,7 +294,7 @@ mod tests {
a: Component<i32>,
b: Cloned<Component<String>>,
#[fetch(ignore)]
c: Mutable<Custom>,
c: ComponentMut<Custom>,
}

let mut world = World::new();
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@
//! .boxed();
//!
//! let despawn_system = System::builder()
//! .with_query(Query::new(entity_ids()).filter(health().le(0.0)))
//! .with_query(Query::new(entity_ids()).with_filter(health().le(0.0)))
//! .with_cmd_mut()
//! .build(|mut q: QueryBorrow<EntityIds, _>, cmd: &mut CommandBuffer| {
//! for id in &mut q {
@@ -264,7 +264,7 @@ pub use entity_ref::{EntityRef, EntityRefMut};
pub use entry::{Entry, OccupiedEntry, VacantEntry};
pub use error::Error;
pub use fetch::{
relations_like, EntityIds, Fetch, FetchExt, FetchItem, Mutable, Opt, OptOr, Relations,
relations_like, ComponentMut, EntityIds, Fetch, FetchExt, FetchItem, Opt, OptOr, Relations,
};

pub use metadata::{Debuggable, Exclusive};
Loading