Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AnyErrorOf in Future::ThenIfSuccess #4803

Closed

Conversation

beckerhe
Copy link
Collaborator

@beckerhe beckerhe commented Feb 6, 2023

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.

`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.

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.
orbitprofiler-bot

This comment was marked as resolved.

@beckerhe beckerhe marked this pull request as ready for review February 6, 2023 15:06
@beckerhe beckerhe requested a review from akopich February 6, 2023 15:07
@beckerhe beckerhe marked this pull request as draft February 6, 2023 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants