Skip to content

Commit

Permalink
refactor(query_builder,term,entity_view,world,term): more consistent …
Browse files Browse the repository at this point in the history
…type parameters and fn naming for first,second
  • Loading branch information
Indra-db committed Dec 22, 2024
1 parent 2200640 commit 5d45f70
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 40 deletions.
4 changes: 2 additions & 2 deletions flecs_ecs/src/addons/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ impl EntityView<'_> {
///
/// * C++ API: `entity_builder::set_json_second`
#[doc(alias = "entity_builder::set_json_second")]
pub fn set_json_second<Target: ComponentId>(
pub fn set_json_second<Second: ComponentId>(
self,
rel: impl Into<Entity> + Copy,
json: &str,
desc: Option<&FromJsonDesc>,
) -> Self {
self.set_json_id((rel, Target::get_id(self.world)), json, desc)
self.set_json_id((rel, Second::get_id(self.world)), json, desc)
}

/// Serialize entity to JSON.
Expand Down
8 changes: 4 additions & 4 deletions flecs_ecs/src/core/entity_view/entity_view_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,8 +2057,8 @@ impl<'a> EntityView<'a> {
///
/// * C++ API: `entity_view::enabled`
#[doc(alias = "entity_view::enabled")]
pub fn is_enabled_first<T: ComponentId>(self, second: impl Into<Entity>) -> bool {
self.is_enabled_id((T::id(self.world), second.into()))
pub fn is_enabled_first<First: ComponentId>(self, second: impl Into<Entity>) -> bool {
self.is_enabled_id((First::id(self.world), second.into()))
}

/// Test if pair is enabled.
Expand All @@ -2076,8 +2076,8 @@ impl<'a> EntityView<'a> {
///
/// * C++ API: `entity_view::enabled`
#[doc(alias = "entity_view::enabled")]
pub fn is_enabled_second<U: ComponentId>(self, first: impl Into<Entity>) -> bool {
self.is_enabled_id((first.into(), U::id(self.world)))
pub fn is_enabled_second<Second: ComponentId>(self, first: impl Into<Entity>) -> bool {
self.is_enabled_id((first.into(), Second::id(self.world)))
}

/// Clones the current entity to a new or specified entity.
Expand Down
21 changes: 20 additions & 1 deletion flecs_ecs/src/core/entity_view/entity_view_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,30 @@ impl<'a> EntityView<'a> {
///
/// * C++ API: `entity_builder::enable`
#[doc(alias = "entity_builder::enable")]
pub fn enable_second<First: ComponentId>(self, second: impl Into<Entity>) -> Self {
pub fn enable_first<First: ComponentId>(self, second: impl Into<Entity>) -> Self {
let world = self.world;
self.enable_id((First::id(world), second.into()))
}

/// Enables a pair with a specific ID for the first element.
///
/// # Type Parameters
///
/// - `Second`: The second element of the pair.
///
/// # Arguments
///
/// - `first`: The ID of the first element of the pair.
///
/// # See also
///
/// * C++ API: `entity_builder::enable`
#[doc(alias = "entity_builder::enable")]
pub fn enable_second<Second: ComponentId>(self, first: impl Into<Entity>) -> Self {
let world = self.world;
self.enable_id((first.into(), Second::id(world)))
}

/// Disables self (entity).
///
/// Disabled entities are not matched with systems and cannot be searched with queries,
Expand Down
32 changes: 16 additions & 16 deletions flecs_ecs/src/core/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,26 +706,26 @@ pub trait QueryBuilderImpl<'a>: TermBuilderImpl<'a> {
}

/// Set the relationship as current term and in mode out
fn write_first<T: ComponentId>(&mut self, second: impl Into<Entity> + Copy) -> &mut Self {
self.with_first::<T>(second);
fn write_first<First: ComponentId>(&mut self, second: impl Into<Entity> + Copy) -> &mut Self {
self.with_first::<First>(second);
TermBuilderImpl::write_curr(self)
}

/// Set the relationship as current term and in mode out
fn write_first_name<T: ComponentId>(&mut self, second: &'a str) -> &mut Self {
self.with_first_name::<T>(second);
fn write_first_name<First: ComponentId>(&mut self, second: &'a str) -> &mut Self {
self.with_first_name::<First>(second);
TermBuilderImpl::write_curr(self)
}

/// Set the relationship as current term and in mode out
fn write_second<T: ComponentId>(&mut self, first: impl Into<Entity> + Copy) -> &mut Self {
self.with_second::<T>(first);
fn write_second<Second: ComponentId>(&mut self, first: impl Into<Entity> + Copy) -> &mut Self {
self.with_second::<Second>(first);
TermBuilderImpl::write_curr(self)
}

/// Set the relationship as current term and in mode out
fn write_second_name<T: ComponentId>(&mut self, first: &'a str) -> &mut Self {
self.with_second_name::<T>(first);
fn write_second_name<Second: ComponentId>(&mut self, first: &'a str) -> &mut Self {
self.with_second_name::<Second>(first);
TermBuilderImpl::write_curr(self)
}

Expand Down Expand Up @@ -763,26 +763,26 @@ pub trait QueryBuilderImpl<'a>: TermBuilderImpl<'a> {
}

/// Set the relationship as current term and in mode in
fn read_first<T: ComponentId>(&mut self, second: impl Into<Entity> + Copy) -> &mut Self {
self.with_first::<T>(second);
fn read_first<First: ComponentId>(&mut self, second: impl Into<Entity> + Copy) -> &mut Self {
self.with_first::<First>(second);
TermBuilderImpl::read_curr(self)
}

/// Set the relationship as current term and in mode in
fn read_first_name<T: ComponentId>(&mut self, second: &'a str) -> &mut Self {
self.with_first_name::<T>(second);
fn read_first_name<First: ComponentId>(&mut self, second: &'a str) -> &mut Self {
self.with_first_name::<First>(second);
TermBuilderImpl::read_curr(self)
}

/// Set the relationship as current term and in mode in
fn read_second<T: ComponentId>(&mut self, first: impl Into<Entity> + Copy) -> &mut Self {
self.with_second::<T>(first);
fn read_second<Second: ComponentId>(&mut self, first: impl Into<Entity> + Copy) -> &mut Self {
self.with_second::<Second>(first);
TermBuilderImpl::read_curr(self)
}

/// Set the relationship as current term and in mode in
fn read_second_name<T: ComponentId>(&mut self, first: &'a str) -> &mut Self {
self.with_second_name::<T>(first);
fn read_second_name<Second: ComponentId>(&mut self, first: &'a str) -> &mut Self {
self.with_second_name::<Second>(first);
TermBuilderImpl::read_curr(self)
}

Expand Down
54 changes: 49 additions & 5 deletions flecs_ecs/src/core/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,32 @@ pub trait TableOperations<'a>: IntoTable {
///
/// * C++ API: `table::type_index`
#[doc(alias = "table::type_index")]
fn find_type_index_second_id<First: ComponentId>(
&self,
second: impl Into<Entity>,
) -> Option<i32> {
fn find_type_index_first<First: ComponentId>(&self, second: impl Into<Entity>) -> Option<i32> {
self.find_type_index_pair_ids(First::id(self.world()), second)
}

/// Find type index for pair of component types
///
/// # Type parameters
///
/// * `Second` - The type of the second component
///
/// # Arguments
///
/// * `first` - The id of the first component
///
/// # Returns
///
/// The index of the pair in the table type, or `None` if the pair is not in the table
///
/// # See also
///
/// * C++ API: `table::type_index`
#[doc(alias = "table::type_index")]
fn find_type_index_second<Second: ComponentId>(&self, first: impl Into<Entity>) -> Option<i32> {
self.find_type_index_pair_ids(first, Second::id(self.world()))
}

/// Find index for (component) id in table type
///
/// This operation returns the index of first occurrence of the id in the table type. The id may be a wildcard.
Expand Down Expand Up @@ -397,13 +416,38 @@ pub trait TableOperations<'a>: IntoTable {
///
/// * C++ API: `table::column_index`
#[doc(alias = "table::column_index")]
fn find_column_index_second_id<First: ComponentId>(
fn find_column_index_first<First: ComponentId>(
&self,
second: impl Into<Entity>,
) -> Option<i32> {
self.find_column_index_pair_ids(First::id(self.world()), second)
}

/// Find index for pair of component types in table type
///
/// # Type parameters
///
/// * `Second` - The type of the second component
///
/// # Arguments
///
/// * `first` - The id of the first component
///
/// # Returns
///
/// The index of the pair in the table, or `None` if the pair is not in the table
///
/// # See also
///
/// * C++ API: `table::column_index`
#[doc(alias = "table::column_index")]
fn find_column_index_second<Second: ComponentId>(
&self,
first: impl Into<Entity>,
) -> Option<i32> {
self.find_column_index_pair_ids(first, Second::id(self.world()))
}

/// Test if table has component type
///
/// This is a constant time operation.
Expand Down
8 changes: 4 additions & 4 deletions flecs_ecs/src/core/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ pub trait TermBuilderImpl<'a>: Sized + WorldProvider<'a> + internals::QueryConfi
///
/// * C++ API: `term_builder_i::first`
#[doc(alias = "term_builder_i::first")]
fn set_first<T: ComponentId>(&mut self) -> &mut Self {
fn set_first<First: ComponentId>(&mut self) -> &mut Self {
check_term_access_validity(self);
self.set_first_id(T::id(self.world()))
self.set_first_id(First::id(self.world()))
}

/// Select first identifier, initialize it with name. If name starts with a $
Expand Down Expand Up @@ -604,9 +604,9 @@ pub trait TermBuilderImpl<'a>: Sized + WorldProvider<'a> + internals::QueryConfi
///
/// * C++ API: `term_builder_i::second`
#[doc(alias = "term_builder_i::second")]
fn set_second<T: ComponentId>(&mut self) -> &mut Self {
fn set_second<Second: ComponentId>(&mut self) -> &mut Self {
check_term_access_validity(self);
self.set_second_id(T::id(self.world()))
self.set_second_id(Second::id(self.world()))
}

/// Select second identifier, initialize it with name. If name starts with a $
Expand Down
16 changes: 8 additions & 8 deletions flecs_ecs/src/core/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,7 @@ impl World {
///
/// * C++ API: `world::delete_with`
#[doc(alias = "world::delete_with")]
pub fn delete_with_id(&self, id: impl IntoId) {
pub fn delete_entities_with_id(&self, id: impl IntoId) {
unsafe {
sys::ecs_delete_with(self.raw_world.as_ptr(), *id.into());
}
Expand All @@ -2895,7 +2895,7 @@ impl World {
/// * C++ API: `world::delete_with`
#[doc(alias = "world::delete_with")]
pub fn delete_entities_with<T: ComponentOrPairId>(&self) {
self.delete_with_id(T::get_id(self));
self.delete_entities_with_id(T::get_id(self));
}

/// Delete all entities with the given pair
Expand All @@ -2912,8 +2912,8 @@ impl World {
///
/// * C++ API: `world::delete_with`
#[doc(alias = "world::delete_with")]
pub fn delete_with_second<Second: ComponentId>(&self, first: impl Into<Entity>) {
self.delete_with_id(ecs_pair(*first.into(), Second::id(self)));
pub fn delete_entities_with_second<Second: ComponentId>(&self, first: impl Into<Entity>) {
self.delete_entities_with_id(ecs_pair(*first.into(), Second::id(self)));
}

/// Delete all entities with the given pair
Expand All @@ -2930,8 +2930,8 @@ impl World {
///
/// * C++ API: `world::delete_with`
#[doc(alias = "world::delete_with")]
pub fn delete_entities_with_second_id<First: ComponentId>(&self, second: impl Into<Entity>) {
self.delete_with_id(ecs_pair(First::id(self), *second.into()));
pub fn delete_entities_with_first<First: ComponentId>(&self, second: impl Into<Entity>) {
self.delete_entities_with_id(ecs_pair(First::id(self), *second.into()));
}

/// Delete all entities with the given enum constant
Expand All @@ -2952,7 +2952,7 @@ impl World {
&self,
enum_value: T,
) {
self.delete_with_id(enum_value.id_variant(self));
self.delete_entities_with_id(enum_value.id_variant(self));
}

/// Delete all entities with the given enum tag pair / relationship
Expand All @@ -2974,7 +2974,7 @@ impl World {
First: ComponentId,
Second: ComponentId + ComponentType<Enum> + EnumComponentInfo,
{
self.delete_with_id(ecs_pair(First::id(self), **enum_value.id_variant(self)));
self.delete_entities_with_id(ecs_pair(First::id(self), **enum_value.id_variant(self)));
}

/// Remove all instances of the given id from entities
Expand Down

0 comments on commit 5d45f70

Please sign in to comment.