Skip to content

Commit

Permalink
Refactor and Simplify Iterable Macro (#38)
Browse files Browse the repository at this point in the history
* Refactor Iterable macro implementation

* Add clippy ignores

* Simplify slice access
  • Loading branch information
james-j-obrien authored Apr 9, 2024
1 parent 18d6657 commit 4031855
Show file tree
Hide file tree
Showing 29 changed files with 330 additions and 554 deletions.
6 changes: 3 additions & 3 deletions flecs_ecs/examples/entity_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ fn main() {
alice.remove::<Walking>();

// Iterate all entities with position
world.each_entity::<(&Position,)>(|entity, pos| {
world.each_entity::<&Position>(|entity, pos| {
println!("{} has {:?}", entity.name(), pos);
});

// Output
// Bob's position: Position { x: 10.0, y: 20.0 }
// [Position, Walking, (Identifier,Name)]
// Alice has (Position { x: 10.0, y: 20.0 },)
// Bob has (Position { x: 20.0, y: 30.0 },)
// Alice has Position { x: 10.0, y: 20.0 }
// Bob has Position { x: 20.0, y: 30.0 }
}
4 changes: 2 additions & 2 deletions flecs_ecs/examples/observer_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ fn main() {

// Create an observer for three events
world
.observer_builder::<(&Position,)>()
.observer_builder::<&Position>()
.add_event::<flecs::OnAdd>() //or .add_event_id(OnAdd::ID)
.add_event::<flecs::OnRemove>()
.add_event::<flecs::OnSet>()
.on_each_iter(|it, index, (pos,)| {
.on_each_iter(|it, index, pos| {
if it.event() == flecs::OnAdd::ID {
// No assumptions about the component value should be made here. If
// a ctor for the component was registered it will be called before
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/observer_custom_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn main() {

// Create an observer for three events
world
.observer_builder::<(&Position,)>()
.observer_builder::<&Position>()
.add_event::<MyEvent>()
.on_each_iter(|it, index, (_pos,)| {
.on_each_iter(|it, index, _pos| {
println!(
" - {}: {}: {}",
it.event().name(),
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/observer_yield_existing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn main() {

// Create an observer for three events
world
.observer_builder::<(&Position,)>()
.observer_builder::<&Position>()
.add_event::<flecs::OnSet>()
.yield_existing(true)
.on_each_iter(|it, index, (pos,)| {
.on_each_iter(|it, index, pos| {
println!(
" - {}: {}: {}: {{ {}, {} }}",
it.event().name(),
Expand Down
2 changes: 1 addition & 1 deletion flecs_ecs/examples/prefab_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn main() {
println!("after set: {:?}", d_inst);

// Prefab components can be iterated like regular components:
world.each_entity::<(&Defence,)>(|entity, (d,)| {
world.each_entity::<&Defence>(|entity, d| {
println!("{}: {}", entity.path().unwrap(), d.value);
});

Expand Down
2 changes: 1 addition & 1 deletion flecs_ecs/examples/query_change_tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn main() {
// Each query has its own private dirty state which is reset only when the
// query is iterated.

let query_read = world.query::<(&Position,)>();
let query_read = world.query::<&Position>();

// Create a query that writes the component based on a Dirty state.
let query_write = world
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_find_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn main() {
.set(Position { x: 20.0, y: 30.0 });

// Create a simple query for component Position
let query = world.query::<(&Position,)>();
let query = world.query::<&Position>();

let entity: Option<Entity> = query.find(|(pos,)| (pos.x - 20.0).abs() < f32::EPSILON);
let entity: Option<Entity> = query.find(|pos| (pos.x - 20.0).abs() < f32::EPSILON);

if let Some(entity) = entity {
println!("Entity found: {:?}", entity.path().unwrap());
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
world.component::<Third>();

let query = world
.query_builder::<(&Position,)>()
.query_builder::<&Position>()
.group_by::<Group>()
.build();

Expand Down Expand Up @@ -44,7 +44,7 @@ fn main() {

println!();

query.iter(|it, (pos,)| {
query.iter(|it, pos| {
let group = world.new_entity_from_id(it.group_id());
println!(
"Group: {:?} - Table: [{:?}]",
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_group_by_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {

// Grouped query
let query = world
.query_builder::<(&Position,)>()
.query_builder::<&Position>()
.group_by_fn::<Group>(Some(callback_group_by_relationship))
.build();

Expand Down Expand Up @@ -82,7 +82,7 @@ fn main() {
// - table [Position, Tag, (Group, Third)]
//

query.iter(|it, (pos,)| {
query.iter(|it, pos| {
let group = world.new_entity_from_id(it.group_id());
println!(
"Group: {:?} - Table: [{:?}]",
Expand Down
2 changes: 1 addition & 1 deletion flecs_ecs/examples/query_group_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn main() {
.add::<Npc>();

let query = world
.query_builder::<(&Npc,)>()
.query_builder::<&Npc>()
.group_by::<WorldCell>()
.build();

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ fn main() {

//TODO: pair wrapper class to clean up, beautify this API
world
.filter_builder::<(&Position,)>()
.filter_builder::<&Position>()
.term_at(1)
.select_second::<World>()
.build()
.each_entity(|entity, (position,)| {
.each_entity(|entity, position| {
println!(
"Entity {} is at ({}, {})",
entity.name(),
Expand Down
10 changes: 5 additions & 5 deletions flecs_ecs/examples/query_sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extern "C" fn compare_position(
(p1.x > p2.x) as i32 - (p1.x < p2.x) as i32
}

fn print_query(query: &Query<'_, (&Position,)>) {
query.each(|(pos,)| println!("{:?}", pos));
fn print_query(query: &Query<'_, &Position>) {
query.each(|pos| println!("{:?}", pos));
}

fn main() {
Expand All @@ -29,15 +29,15 @@ fn main() {

// Create a sorted system
let sys = world
.system_builder::<(&Position,)>()
.system_builder::<&Position>()
.order_by(compare_position)
.on_each(|(pos,)| {
.on_each(|pos| {
println!("{:?}", pos);
});

// Create a sorted query
let query = world
.query_builder::<(&Position,)>()
.query_builder::<&Position>()
.order_by(compare_position)
.build();

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_wildcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {

// Create a query that matches edible components
let query = world
.query_builder::<(&Eats,)>()
.query_builder::<&Eats>()
.term_at(1)
// Change first argument to (Eats, *)
// alternative you can do `.select_second_id(flecs::Wildcard::ID)``
Expand All @@ -32,7 +32,7 @@ fn main() {

// Iterate the query with a flecs::iter. This makes it possible to inspect
// the pair that we are currently matched with.
query.each_iter(|it, index, (eats,)| {
query.each_iter(|it, index, eats| {
let entity = it.entity(index);
let food = it.pair(1).unwrap().second();

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
// This is useful for things like tags, which because they don't have a
// value are less useful to pass to the each/iter functions as argument.
let query = world
.query_builder::<(&Position,)>()
.query_builder::<&Position>()
.with_type::<&Npc>()
.build();

Expand All @@ -34,7 +34,7 @@ fn main() {
.set(Position { x: 10.0, y: 20.0 });

// Note how the Npc tag is not part of the each signature
query.each_entity(|entity, (pos,)| {
query.each_entity(|entity, pos| {
println!("Entity {}: {:?}", entity.name(), pos);
});

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/query_without.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// The without method is short for:
// .term<Npc>().not_()
let query = world
.query_builder::<(&Position,)>()
.query_builder::<&Position>()
.without_type::<&Npc>()
.build();

Expand All @@ -36,7 +36,7 @@ fn main() {
.add::<Npc>();

// Note how the Npc tag is not part of the each signature
query.each_entity(|entity, (pos,)| {
query.each_entity(|entity, pos| {
println!("Entity {}: {:?}", entity.name(), pos);
});

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/relationships_component_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ fn main() {
// When querying for a relationship component, add the pair type as template
// argument to the builder:
let query = world
.query_builder::<(&Requires,)>()
.query_builder::<&Requires>()
.term_at(1)
.select_second::<Gigawatts>()
.build();

query.each(|(requires,)| {
query.each(|requires| {
println!("requires: {} gigawatts", requires.amount);
});

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/rules_component_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ fn main() {
world.new_entity_named(c"builder_2").add::<Builder>();

// Create a rule to find all ranged units
let r = world.rule::<(&RangedUnit,)>();
let r = world.rule::<&RangedUnit>();

// Iterate the rule
r.each_entity(|e, (_,)| {
r.each_entity(|e, _| {
println!("Unit {} found", e.name());
});

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/rules_setting_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn main() {
// - find all entities with (Platoon, *), store * in _Platoon
// - check if _Platoon has (Player, *), store * in _Player
let rule = world
.rule_builder::<(&RangedUnit,)>()
.rule_builder::<&RangedUnit>()
.with_type::<&Platoon>()
.select_second_name(c"$Platoon")
.with_pair_name::<&Player>(c"$Player")
Expand All @@ -113,7 +113,7 @@ fn main() {
// Iterate rule, limit the results to units of MyPlayer
rule.iterable()
.set_var(player_var, world.lookup_name(c"MyPlayer", true))
.each_iter(|it, index, (_,)| {
.each_iter(|it, index, _| {
let unit = it.entity(index);
println!(
"Unit {} of class {} in platoon {} for player {}",
Expand Down
12 changes: 6 additions & 6 deletions flecs_ecs/examples/system_mutate_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fn main() {

// System that deletes an entity after a timeout expires
world
.system_builder::<(&mut Timeout,)>()
.on_each_iter(|it, index, (timeout,)| {
.system_builder::<&mut Timeout>()
.on_each_iter(|it, index, timeout| {
timeout.value -= it.delta_time();
if timeout.value <= 0.0 {
// Delete the entity
Expand All @@ -33,8 +33,8 @@ fn main() {

// System that prints remaining expiry time
world
.system_builder::<(&Timeout,)>()
.on_each_entity(|e, (timeout,)| {
.system_builder::<&Timeout>()
.on_each_entity(|e, timeout| {
println!(
"PrintExpire: {} has {:.2} seconds left",
e.name(),
Expand All @@ -44,9 +44,9 @@ fn main() {

// Observer that triggers when entity is actually deleted
world
.observer_builder::<(&Timeout,)>()
.observer_builder::<&Timeout>()
.add_event::<flecs::OnRemove>()
.on_each_entity(|e, (_timeout,)| {
.on_each_entity(|e, _timeout| {
println!("Expired: {} actually deleted", e.name());
});

Expand Down
10 changes: 5 additions & 5 deletions flecs_ecs/examples/system_mutate_entity_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ fn main() {

// System that deletes an entity after a timeout expires
world
.system_builder::<(&mut Timeout,)>()
.on_each_iter(|it, _index, (timeout,)| {
.system_builder::<&mut Timeout>()
.on_each_iter(|it, _index, timeout| {
timeout.value -= it.delta_time();
if timeout.value <= 0.0 {
// Delete the entity
Expand Down Expand Up @@ -50,7 +50,7 @@ fn main() {
});

// System that prints remaining expiry time
world.system_builder::<(&Timeout,)>().on_each(|(timeout,)| {
world.system_builder::<&Timeout>().on_each(|timeout| {
println!(
"PrintExpire: {} has {:.2} seconds left",
timeout.to_delete.name(),
Expand All @@ -60,9 +60,9 @@ fn main() {

// Observer that triggers when entity is actually deleted
world
.observer_builder::<(&Tag,)>()
.observer_builder::<&Tag>()
.add_event::<flecs::OnRemove>()
.on_each_entity(|e, (_tag,)| {
.on_each_entity(|e, _tag| {
println!("Expired: {} actually deleted", e.name());
});

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/system_no_readonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ fn main() {

// Create query to find all waiters without a plate
let q_waiter = world
.query_builder::<(&Waiter,)>()
.query_builder::<&Waiter>()
.without_pair::<&Plate, flecs::Wildcard>()
.build();

// System that assigns plates to waiter. By making this system no_readonly
// plate assignments are assigned directly (not deferred) to waiters, which
// ensures that we won't assign plates to the same waiter more than once.
world
.system_builder_named::<(&Plate,)>(c"AssignPlate")
.system_builder_named::<&Plate>(c"AssignPlate")
.without_pair::<&Waiter, flecs::Wildcard>()
.no_readonly(true)
.on_iter_only(move |it| {
Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/system_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn main() {

// Create a system for printing the entity position
world
.system_builder::<(&Position,)>()
.system_builder::<&Position>()
.kind::<flecs::pipeline::PostUpdate>()
.on_each_entity(|e, (p,)| {
.on_each_entity(|e, p| {
println!("{}: {{ {}, {} }}", e.name(), p.x, p.y);
});

Expand Down
4 changes: 2 additions & 2 deletions flecs_ecs/examples/system_sync_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn main() {

// Print resulting Position
world
.system_builder_named::<(&Position,)>(c"PrintPosition")
.on_each_entity(|e, (p,)| {
.system_builder_named::<&Position>(c"PrintPosition")
.on_each_entity(|e, p| {
println!("{}: {{ {}, {} }}", e.name(), p.x, p.y);
});

Expand Down
Loading

0 comments on commit 4031855

Please sign in to comment.