orbit icon indicating copy to clipboard operation
orbit copied to clipboard

Use AnyErrorOf in Future::ThenIfSuccess

Open beckerhe opened this issue 2 years ago • 0 comments

Future::ThenIfSuccess allows to chain a continuation to a future that is only executed when the future completes with no error.

In terms of types that means we have a Future<Result<T, E>>, call .ThenIfSuccess with a closure T -> U or T -> Result<U, E> and the result will be a Future<Result<U, E>> in both cases (that's the status quo).

Imagine you want to chain a closure T -> Result<U, E2>. This is currently not supported due to the mismatching error types.

This changes with this PR. Future<Result<T, E>>::ThenIfSuccess<T -> Result<U, E2>>() will result in a Future<Result<U, AnyErrorOf<E, E2>>>. That allows easier composition of closures with different error types.

Note that this changes the semantics of ThenIfScuccess and I had to make some changes in existing code to avoid behavioural changes.

beckerhe avatar Feb 06 '23 10:02 beckerhe