summaryrefslogtreecommitdiff
path: root/ecs/src/pair.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2025-09-24 22:16:50 +0200
committerHampusM <hampus@hampusmat.com>2025-09-25 22:42:35 +0200
commitcbed21f5e6cfb449d49087cedc867c8e50721ba9 (patch)
tree3850ac5e7907a40e4c4e5627f547bd44c37896ce /ecs/src/pair.rs
parent8d76fe6be211dfc8fc57d4e2f7e312e757ca899c (diff)
refactor(ecs): replace Pair ctor functions with builder
Diffstat (limited to 'ecs/src/pair.rs')
-rw-r--r--ecs/src/pair.rs114
1 files changed, 96 insertions, 18 deletions
diff --git a/ecs/src/pair.rs b/ecs/src/pair.rs
index 85be9f6..985a2b3 100644
--- a/ecs/src/pair.rs
+++ b/ecs/src/pair.rs
@@ -19,8 +19,91 @@ use crate::query::{
TermsBuilderInterface,
};
use crate::uid::{PairParams as UidPairParams, Uid, With as WithUid};
+use crate::util::impl_multiple;
use crate::{Component, EntityComponentRef, World};
+/// Pair builder.
+#[derive(Debug)]
+pub struct Builder<Relation, Target>
+{
+ relation: Relation,
+ target: Target,
+}
+
+impl<Relation, Target> Builder<Relation, Target>
+{
+ pub fn relation<NewRelation: Component>(self) -> Builder<Uid, Target>
+ {
+ Builder {
+ relation: NewRelation::id(),
+ target: self.target,
+ }
+ }
+
+ pub fn relation_id(self, id: Uid) -> Builder<Uid, Target>
+ {
+ Builder { relation: id, target: self.target }
+ }
+
+ pub fn target<NewTarget: Component>(self) -> Builder<Relation, Uid>
+ {
+ Builder {
+ relation: self.relation,
+ target: NewTarget::id(),
+ }
+ }
+
+ pub fn target_id(self, id: Uid) -> Builder<Relation, Uid>
+ {
+ Builder { relation: self.relation, target: id }
+ }
+}
+
+impl_multiple!(
+ Builder,
+ (impl<Target> _<Uid, Target>, impl<Target> _<(), Target>)
+ (ty_param_1, ty_param_2) => {
+ pub fn target_as_data<NewTarget: Component>(
+ self,
+ data: NewTarget,
+ ) -> Builder<$ty_param_1, NewTarget>
+ {
+ Builder {
+ relation: self.relation,
+ target: data,
+ }
+ }
+ }
+);
+
+impl_multiple!(
+ Builder,
+ (
+ impl _<Uid, Uid>,
+ impl<Relation: Component> _<Relation, Uid>,
+ impl<Target: Component> _<Uid, Target>,
+ impl<Relation: Component, Target: Component> _<Relation, Target>
+ )
+ (ty_param_1, ty_param_2) => {
+ #[must_use]
+ pub fn build(self) -> Pair<$ty_param_1, $ty_param_2>
+ {
+ Pair {
+ relation: self.relation,
+ target: self.target
+ }
+ }
+ }
+);
+
+impl Default for Builder<(), ()>
+{
+ fn default() -> Self
+ {
+ Self { relation: (), target: () }
+ }
+}
+
#[derive(Debug)]
pub struct Pair<Relation, Target>
{
@@ -28,14 +111,17 @@ pub struct Pair<Relation, Target>
target: Target,
}
-impl Pair<Uid, Uid>
+impl Pair<(), ()>
{
#[must_use]
- pub fn new<Relation: Component>(target: Uid) -> Self
+ pub fn builder() -> Builder<(), ()>
{
- Self { relation: Relation::id(), target }
+ Builder { relation: (), target: () }
}
+}
+impl Pair<Uid, Uid>
+{
#[must_use]
pub fn id(&self) -> Uid
{
@@ -46,20 +132,6 @@ impl Pair<Uid, Uid>
}
}
-impl<Target> Pair<Uid, Target>
-where
- Target: Component,
-{
- /// Returns a new pair that contains the target component as data.
- pub fn new_with_comp_target<Relation: Component>(target_component: Target) -> Self
- {
- Self {
- relation: Relation::uid(),
- target: target_component,
- }
- }
-}
-
impl IntoComponentParts for Pair<Uid, Uid>
{
fn into_parts(self) -> ComponentParts
@@ -358,7 +430,13 @@ impl<'a, Relation: Component> MultipleWithWildcard<'a, Relation, Wildcard>
world: self.world,
component_ref: self
.entity_handle
- .get_matching_components(Pair::new::<Relation>(target_id).id())
+ .get_matching_components(
+ Pair::builder()
+ .relation::<Relation>()
+ .target_id(target_id)
+ .build()
+ .id(),
+ )
.next()?,
_pd: PhantomData,
})