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

Add Iterator::try_collect() #308

Merged
merged 1 commit into from
Aug 7, 2023
Merged

Add Iterator::try_collect() #308

merged 1 commit into from
Aug 7, 2023

Conversation

danakj
Copy link
Collaborator

@danakj danakj commented Aug 7, 2023

Closes #295

try_collect can collect from an iterator of T: Try into a container C<U> that supports FromIterator<T: Try> to create the C<U>.

The actual Try types do not need to support FromIterator then, and this replaces the need for Option or Result to implement FromIterator, if the user calls .try_collect() instead of .collect().

We needed to add another operation on Try types for this, which is to convert from T: Try in an error state to U: Try in an error state, where they have the same (or a convertible) error type. This new concept is sus::ops::TryErrorConvertibleTo and it's accessed through sus::ops::try_preserve_error().

The TryErrorConvertibleTo concept is implemented for Option, Result and std::optional.

Then we also needed a way to take a TryType and a type Bar and produce the type TryType so that we can say try_collect<Vec> on an iterator of Option and get back Option<Vec>. This is done through sus::ops::TryRemapOutputType<TryType, NewOutputType>.

@danakj danakj force-pushed the trycollect branch 8 times, most recently from 884308e to 5c2befc Compare August 7, 2023 03:04
Closes chromium#295

try_collect can collect from an iterator of `T: Try` into a container
C that supports FromIterator<T: Try> to create C<U>.

The actual Try types do not need to support FromIterator then, and this
replaces the need for Option or Result to implement FromIterator.

We needed to add another operation on Try types for this, which is to
convert from T: Try in an error state to U: Try in an error state, where
they have the same (or a convertible) error type. This new concept is
sus::ops::TryErrorConvertibleTo and it's accessed through
sus::ops::try_preserve_error().

The TryErrorConvertibleTo concept is implemented for Option, Result and
std::optional.
@danakj danakj merged commit 6e9d7cd into chromium:main Aug 7, 2023
@danakj danakj deleted the trycollect branch August 7, 2023 03:22
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.

Add Iterator::try_collect
1 participant