Skip to content

Commit 159bb9c

Browse files
committed
Update chain API and implementation
Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>
1 parent 14dabe3 commit 159bb9c

File tree

8 files changed

+467
-532
lines changed

8 files changed

+467
-532
lines changed

src/chain.rs

Lines changed: 345 additions & 390 deletions
Large diffs are not rendered by default.

src/chain/fork_clone_builder.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
use bevy::prelude::{Entity, Commands};
1919

20-
use crate::{OutputChain, UnusedTarget, AddOperation, ForkClone, ForkTargetStorage};
20+
use crate::{Chain, UnusedTarget, AddOperation, ForkClone, ForkTargetStorage};
2121

2222
pub trait ForkCloneBuilder<Response> {
2323
type Outputs;
2424

2525
fn build_fork_clone(
2626
self,
27+
scope: Entity,
2728
source: Entity,
2829
commands: &mut Commands
2930
) -> Self::Outputs;
@@ -32,13 +33,14 @@ pub trait ForkCloneBuilder<Response> {
3233
impl<R, F0, U0, F1, U1> ForkCloneBuilder<R> for (F0, F1)
3334
where
3435
R: 'static + Send + Sync + Clone,
35-
F0: FnOnce(OutputChain<R>) -> U0,
36-
F1: FnOnce(OutputChain<R>) -> U1,
36+
F0: FnOnce(Chain<R>) -> U0,
37+
F1: FnOnce(Chain<R>) -> U1,
3738
{
3839
type Outputs = (U0, U1);
3940

4041
fn build_fork_clone(
4142
self,
43+
scope: Entity,
4244
source: Entity,
4345
commands: &mut Commands
4446
) -> Self::Outputs {
@@ -52,23 +54,24 @@ where
5254
)
5355
));
5456

55-
let u_0 = (self.0)(OutputChain::new(source, target_0, commands));
56-
let u_1 = (self.1)(OutputChain::new(source, target_1, commands));
57+
let u_0 = (self.0)(Chain::new(scope, target_0, commands));
58+
let u_1 = (self.1)(Chain::new(scope, target_1, commands));
5759
(u_0, u_1)
5860
}
5961
}
6062

6163
impl<R, F0, U0, F1, U1, F2, U2> ForkCloneBuilder<R> for (F0, F1, F2)
6264
where
6365
R: 'static + Send + Sync + Clone,
64-
F0: FnOnce(OutputChain<R>) -> U0,
65-
F1: FnOnce(OutputChain<R>) -> U1,
66-
F2: FnOnce(OutputChain<R>) -> U2,
66+
F0: FnOnce(Chain<R>) -> U0,
67+
F1: FnOnce(Chain<R>) -> U1,
68+
F2: FnOnce(Chain<R>) -> U2,
6769
{
6870
type Outputs = (U0, U1, U2);
6971

7072
fn build_fork_clone(
7173
self,
74+
scope: Entity,
7275
source: Entity,
7376
commands: &mut Commands
7477
) -> Self::Outputs {
@@ -83,9 +86,9 @@ where
8386
)
8487
));
8588

86-
let u_0 = (self.0)(OutputChain::new(source, target_0, commands));
87-
let u_1 = (self.1)(OutputChain::new(source, target_1, commands));
88-
let u_2 = (self.2)(OutputChain::new(source, target_2, commands));
89+
let u_0 = (self.0)(Chain::new(scope, target_0, commands));
90+
let u_1 = (self.1)(Chain::new(scope, target_1, commands));
91+
let u_2 = (self.2)(Chain::new(scope, target_2, commands));
8992
(u_0, u_1, u_2)
9093
}
9194
}

src/chain/unzip.rs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ use bevy::prelude::{Entity, Commands};
2020
use smallvec::SmallVec;
2121

2222
use crate::{
23-
Dangling, UnusedTarget, ForkTargetStorage, OperationRequest, Input, ManageInput,
24-
ForkUnzip, AddOperation, OutputChain, FunnelInputStorage, OperationResult,
25-
SingleTargetStorage, OrBroken, OperationReachability,
26-
OperationError, InspectInput,
23+
UnusedTarget, ForkTargetStorage, OperationRequest, Input, ManageInput,
24+
ForkUnzip, AddOperation, FunnelInputStorage, OperationResult,
25+
SingleTargetStorage, OrBroken, OperationReachability, Output,
26+
OperationError, InspectInput, Chain,
2727
};
2828

2929
/// A trait for response types that can be unzipped
3030
pub trait Unzippable {
3131
type Unzipped;
32-
fn unzip_chain(source: Entity, commands: &mut Commands) -> Self::Unzipped;
32+
fn unzip_chain(scope: Entity, source: Entity, commands: &mut Commands) -> Self::Unzipped;
3333

3434
fn make_targets(commands: &mut Commands) -> SmallVec<[Entity; 8]>;
3535

@@ -62,11 +62,11 @@ pub enum JoinStatus {
6262
pub type JoinStatusResult = Result<JoinStatus, OperationError>;
6363

6464
impl<A: 'static + Send + Sync> Unzippable for (A,) {
65-
type Unzipped = Dangling<A>;
66-
fn unzip_chain(source: Entity, commands: &mut Commands) -> Self::Unzipped {
65+
type Unzipped = Output<A>;
66+
fn unzip_chain(scope: Entity, source: Entity, commands: &mut Commands) -> Self::Unzipped {
6767
let targets = Self::make_targets(commands);
6868

69-
let result = Dangling::new(source, targets[0]);
69+
let result = Output::new(scope, targets[0]);
7070

7171
commands.add(AddOperation::new(
7272
source,
@@ -130,7 +130,7 @@ impl<A: 'static + Send + Sync> Unzippable for (A,) {
130130
OperationRequest { source, world, roster }: OperationRequest,
131131
) -> OperationResult {
132132
let inputs = world.get::<FunnelInputStorage>(source).or_broken()?;
133-
let target = world.get::<SingleTargetStorage>(source).or_broken()?.0;
133+
let target = world.get::<SingleTargetStorage>(source).or_broken()?.get();
134134

135135
let input_0 = *inputs.0.get(0).or_broken()?;
136136

@@ -146,13 +146,13 @@ impl<A: 'static + Send + Sync> Unzippable for (A,) {
146146
}
147147

148148
impl<A: 'static + Send + Sync, B: 'static + Send + Sync> Unzippable for (A, B) {
149-
type Unzipped = (Dangling<A>, Dangling<B>);
150-
fn unzip_chain(source: Entity, commands: &mut Commands) -> Self::Unzipped {
149+
type Unzipped = (Output<A>, Output<B>);
150+
fn unzip_chain(scope: Entity, source: Entity, commands: &mut Commands) -> Self::Unzipped {
151151
let targets = Self::make_targets(commands);
152152

153153
let result = (
154-
Dangling::new(source, targets[0]),
155-
Dangling::new(source, targets[1]),
154+
Output::new(scope, targets[0]),
155+
Output::new(scope, targets[1]),
156156
);
157157

158158
commands.add(AddOperation::new(
@@ -216,6 +216,7 @@ impl<A: 'static + Send + Sync, B: 'static + Send + Sync> Unzippable for (A, B) {
216216
}
217217
}
218218

219+
let world = reachability.world();
219220
if !world.get_entity(input_1).or_broken()?.buffer_ready::<B>(session)? {
220221
status = JoinStatus::Pending;
221222
if !reachability.check_upstream(input_1)? {
@@ -235,7 +236,7 @@ impl<A: 'static + Send + Sync, B: 'static + Send + Sync> Unzippable for (A, B) {
235236
OperationRequest { source, world, roster }: OperationRequest,
236237
) -> OperationResult {
237238
let inputs = world.get::<FunnelInputStorage>(source).or_broken()?;
238-
let target = world.get::<SingleTargetStorage>(source).or_broken()?.0;
239+
let target = world.get::<SingleTargetStorage>(source).or_broken()?.get();
239240

240241
let input_0 = *inputs.0.get(0).or_broken()?;
241242
let input_1 = *inputs.0.get(1).or_broken()?;
@@ -262,14 +263,14 @@ where
262263
B: 'static + Send + Sync,
263264
C: 'static + Send + Sync,
264265
{
265-
type Unzipped = (Dangling<A>, Dangling<B>, Dangling<C>);
266-
fn unzip_chain(source: Entity, commands: &mut Commands) -> Self::Unzipped {
266+
type Unzipped = (Output<A>, Output<B>, Output<C>);
267+
fn unzip_chain(scope: Entity, source: Entity, commands: &mut Commands) -> Self::Unzipped {
267268
let targets = Self::make_targets(commands);
268269

269270
let result = (
270-
Dangling::new(source, targets[0]),
271-
Dangling::new(source, targets[1]),
272-
Dangling::new(source, targets[2]),
271+
Output::new(scope, targets[0]),
272+
Output::new(scope, targets[1]),
273+
Output::new(scope, targets[2]),
273274
);
274275

275276
commands.add(AddOperation::new(
@@ -320,36 +321,35 @@ where
320321
}
321322

322323
fn join_status(
323-
mut reachability: OperationReachability,
324+
mut r: OperationReachability,
324325
) -> JoinStatusResult {
325-
let source = reachability.source();
326-
let session = reachability.session();
327-
let world = reachability.world();
328-
let inputs = world.get::<FunnelInputStorage>(source).or_broken()?;
326+
let source = r.source();
327+
let session = r.session();
328+
let inputs = r.world().get::<FunnelInputStorage>(source).or_broken()?;
329329
let mut unreachable: Vec<Entity> = Vec::new();
330330
let mut status = JoinStatus::Ready;
331331

332332
let input_0 = *inputs.0.get(0).or_broken()?;
333333
let input_1 = *inputs.0.get(1).or_broken()?;
334334
let input_2 = *inputs.0.get(2).or_broken()?;
335335

336-
if !world.get_entity(input_0).or_broken()?.buffer_ready::<A>(session)? {
336+
if !r.world().get_entity(input_0).or_broken()?.buffer_ready::<A>(session)? {
337337
status = JoinStatus::Pending;
338-
if !reachability.check_upstream(input_0)? {
338+
if !r.check_upstream(input_0)? {
339339
unreachable.push(input_0);
340340
}
341341
}
342342

343-
if !world.get_entity(input_1).or_broken()?.buffer_ready::<B>(session)? {
343+
if !r.world().get_entity(input_1).or_broken()?.buffer_ready::<B>(session)? {
344344
status = JoinStatus::Pending;
345-
if !reachability.check_upstream(input_1)? {
345+
if !r.check_upstream(input_1)? {
346346
unreachable.push(input_1);
347347
}
348348
}
349349

350-
if !world.get_entity(input_2).or_broken()?.buffer_ready::<C>(session)? {
350+
if !r.world().get_entity(input_2).or_broken()?.buffer_ready::<C>(session)? {
351351
status = JoinStatus::Pending;
352-
if !reachability.check_upstream(input_2)? {
352+
if !r.check_upstream(input_2)? {
353353
unreachable.push(input_2);
354354
}
355355
}
@@ -366,7 +366,7 @@ where
366366
OperationRequest { source, world, roster }: OperationRequest,
367367
) -> OperationResult {
368368
let inputs = world.get::<FunnelInputStorage>(source).or_broken()?;
369-
let target = world.get::<SingleTargetStorage>(source).or_broken()?.0;
369+
let target = world.get::<SingleTargetStorage>(source).or_broken()?.get();
370370

371371
let input_0 = *inputs.0.get(0).or_broken()?;
372372
let input_1 = *inputs.0.get(1).or_broken()?;
@@ -397,21 +397,21 @@ where
397397
/// tuple.
398398
pub trait UnzipBuilder<Z> {
399399
type Output;
400-
fn unzip_build(self, source: Entity, commands: &mut Commands) -> Self::Output;
400+
fn unzip_build(self, scope: Entity, source: Entity, commands: &mut Commands) -> Self::Output;
401401
}
402402

403403
impl<A, Fa, Ua, B, Fb, Ub> UnzipBuilder<(A, B)> for (Fa, Fb)
404404
where
405405
A: 'static + Send + Sync,
406406
B: 'static + Send + Sync,
407-
Fa: FnOnce(OutputChain<A>) -> Ua,
408-
Fb: FnOnce(OutputChain<B>) -> Ub,
407+
Fa: FnOnce(Chain<A>) -> Ua,
408+
Fb: FnOnce(Chain<B>) -> Ub,
409409
{
410410
type Output = (Ua, Ub);
411-
fn unzip_build(self, source: Entity, commands: &mut Commands) -> Self::Output {
412-
let dangling = <(A, B)>::unzip_chain(source, commands);
413-
let u_a = (self.0)(dangling.0.resume(commands));
414-
let u_b = (self.1)(dangling.1.resume(commands));
411+
fn unzip_build(self, scope: Entity, source: Entity, commands: &mut Commands) -> Self::Output {
412+
let outputs = <(A, B)>::unzip_chain(scope, source, commands);
413+
let u_a = (self.0)(outputs.0.chain(commands));
414+
let u_b = (self.1)(outputs.1.chain(commands));
415415
(u_a, u_b)
416416
}
417417
}
@@ -421,16 +421,16 @@ where
421421
A: 'static + Send + Sync,
422422
B: 'static + Send + Sync,
423423
C: 'static + Send + Sync,
424-
Fa: FnOnce(OutputChain<A>) -> Ua,
425-
Fb: FnOnce(OutputChain<B>) -> Ub,
426-
Fc: FnOnce(OutputChain<C>) -> Uc,
424+
Fa: FnOnce(Chain<A>) -> Ua,
425+
Fb: FnOnce(Chain<B>) -> Ub,
426+
Fc: FnOnce(Chain<C>) -> Uc,
427427
{
428428
type Output = (Ua, Ub, Uc);
429-
fn unzip_build(self, source: Entity, commands: &mut Commands) -> Self::Output {
430-
let dangling = <(A, B, C)>::unzip_chain(source, commands);
431-
let u_a = (self.0)(dangling.0.resume(commands));
432-
let u_b = (self.1)(dangling.1.resume(commands));
433-
let u_c = (self.2)(dangling.2.resume(commands));
429+
fn unzip_build(self, scope: Entity, source: Entity, commands: &mut Commands) -> Self::Output {
430+
let outputs = <(A, B, C)>::unzip_chain(scope, source, commands);
431+
let u_a = (self.0)(outputs.0.chain(commands));
432+
let u_b = (self.1)(outputs.1.chain(commands));
433+
let u_c = (self.2)(outputs.2.chain(commands));
434434
(u_a, u_b, u_c)
435435
}
436436
}

0 commit comments

Comments
 (0)