Skip to content

Commit 80c46d1

Browse files
committed
Fix branching and add more disposal tests
Signed-off-by: Michael X. Grey <grey@openrobotics.org>
1 parent 61e9145 commit 80c46d1

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/chain.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ where
453453
) -> Chain<'w, 's, 'a, 'b, T> {
454454
Chain::<Result<T, E>>::new(
455455
self.target, self.builder,
456-
).branch_result_zip(
456+
).fork_result(
457457
|chain| chain.output(),
458458
build_err,
459459
).0.chain(self.builder)
@@ -466,8 +466,7 @@ where
466466
///
467467
/// The outputs of both builder functions will be zipped as the return value
468468
/// of this function.
469-
#[must_use]
470-
pub fn branch_result_zip<U, V>(
469+
pub fn fork_result<U, V>(
471470
self,
472471
build_ok: impl FnOnce(Chain<T>) -> U,
473472
build_err: impl FnOnce(Chain<E>) -> V,
@@ -844,7 +843,6 @@ mod tests {
844843
.connect(scope.terminate);
845844
});
846845

847-
848846
let mut promise = context.command(|commands| {
849847
commands
850848
.request((2.0, 3.0), workflow)
@@ -927,5 +925,45 @@ mod tests {
927925
context.run_with_conditions(&mut promise, Duration::from_secs(2));
928926
assert!(promise.peek().is_cancelled());
929927
assert!(context.no_unhandled_errors());
928+
929+
let workflow = context.spawn_io_workflow(
930+
|scope: Scope<Result<f64, Result<f64, TestError>>, f64>, builder| {
931+
scope.input.chain(builder)
932+
.fork_result(
933+
|chain| chain.connect(scope.terminate),
934+
|chain|
935+
chain.dispose_on_err().connect(scope.terminate)
936+
);
937+
});
938+
939+
let mut promise = context.command(|commands| {
940+
commands
941+
.request(Ok(1.0), workflow)
942+
.take_response()
943+
});
944+
945+
context.run_with_conditions(&mut promise, Duration::from_secs(2));
946+
assert!(promise.peek().available().is_some_and(|v| *v == 1.0));
947+
assert!(context.no_unhandled_errors());
948+
949+
let mut promise = context.command(|commands| {
950+
commands
951+
.request(Err(Ok(5.0)), workflow)
952+
.take_response()
953+
});
954+
955+
context.run_with_conditions(&mut promise, Duration::from_secs(2));
956+
assert!(promise.peek().available().is_some_and(|v| *v == 5.0));
957+
assert!(context.no_unhandled_errors());
958+
959+
let mut promise = context.command(|commands| {
960+
commands
961+
.request(Err(Err(TestError)), workflow)
962+
.take_response()
963+
});
964+
965+
context.run_with_conditions(&mut promise, Duration::from_secs(2));
966+
assert!(promise.peek().is_cancelled());
967+
assert!(context.no_unhandled_errors());
930968
}
931969
}

src/operation/branching.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ pub(crate) fn make_option_branching<T>(
5252
}
5353
}
5454

55-
#[derive(Component)]
56-
struct BranchingActivatorStorage<F: 'static + Send + Sync>(F);
55+
#[derive(Component, Clone, Copy)]
56+
struct BranchingActivatorStorage<F: 'static + Send + Sync + Copy>(F);
5757

5858
impl<InputT, Outputs, F> Operation for Branching<InputT, Outputs, F>
5959
where
6060
InputT: 'static + Send + Sync,
6161
Outputs: Branchable,
62-
F: FnOnce(InputT) -> Outputs::Activation + 'static + Send + Sync,
62+
F: Fn(InputT) -> Outputs::Activation + Copy + 'static + Send + Sync,
6363
{
6464
fn setup(self, OperationSetup { source, world }: OperationSetup) -> OperationResult {
6565
for target in &self.targets.0 {
@@ -79,7 +79,7 @@ where
7979
) -> OperationResult {
8080
let mut source_mut = world.get_entity_mut(source).or_broken()?;
8181
let Input { session, data: input } = source_mut.take_input::<InputT>()?;
82-
let BranchingActivatorStorage::<F>(activator) = source_mut.take().or_broken()?;
82+
let BranchingActivatorStorage::<F>(activator) = source_mut.get().copied().or_broken()?;
8383

8484
let activation = activator(input);
8585
Outputs::activate(session, activation, source, world, roster)

src/request.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub trait RequestExt<'w, 's> {
5656
P::Response: 'static + Send + Sync,
5757
P::Streams: StreamPack;
5858

59-
/// Call this on [`Commands`] to begin building an impulse chain from a value
60-
/// without calling any provider.
59+
/// Call this on [`Commands`] to begin building an impulse chain from a
60+
/// value without calling any provider.
6161
fn provide<'a, T: 'static + Send + Sync>(
6262
&'a mut self,
6363
value: T,

0 commit comments

Comments
 (0)