Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions exn/src/iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2025 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Extension trait for [`Iterator`]s of [`Result`]s
pub trait IteratorExt<T, E>: Iterator<Item = Result<T, E>> {
/// Transforms this [`Iterator`] of [`Result`]s into a [`Result`] of _collections_.
///
/// This method produces a [`Result`] where _both_ the [`Ok`] and [`Err`] variants are
/// [`FromIterator`].
///
/// Unlike [`Result`]'s [`FromIterator`] implementation, this method is _not_ short-circuiting;
/// it will always consume all items in `self`.
///
/// This method pairs well with [`Exn::from_iter`][crate::Exn::from_iter]:
///
/// ```
/// use std::io;
///
/// use exn::Exn;
/// use exn::IteratorExt;
/// use exn::Result;
/// use exn::ResultExt;
///
/// let files: Result<Vec<_>, io::Error> = ["a/b", "c/d", "e/f"]
/// .into_iter()
/// .map(|path| {
/// std::fs::File::open(path)
/// .or_raise(|| io::Error::other(format!("failed to open {path}")))
/// })
/// .collect_all::<_, Vec<_>>()
/// .map_err(|children| Exn::from_iter(children, io::Error::other("failed to open all files")));
/// #
/// # drop(files);
/// ```
///
/// # Errors
///
/// Similar to [`Result`]'s [`FromIterator`] implementation, if any item is [`Err`], this
/// method will return [`Err`].
fn collect_all<A, B>(mut self) -> Result<A, B>
where
Self: Sized,
A: FromIterator<T>,
B: FromIterator<E>,
{
self.by_ref()
.collect::<Result<A, E>>()
.map_err(|first_err| {
std::iter::once(first_err)
.chain(self.filter_map(Result::err))
.collect()
})
}
}

impl<I, T, E> IteratorExt<T, E> for I where I: Iterator<Item = Result<T, E>> {}
2 changes: 2 additions & 0 deletions exn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ mod debug;
mod display;
mod ext;
mod impls;
mod iterator;
mod macros;
mod option;
mod result;
Expand All @@ -87,6 +88,7 @@ pub use self::ext::ErrorExt;
pub use self::ext::Ok;
pub use self::impls::Exn;
pub use self::impls::Frame;
pub use self::iterator::IteratorExt;
pub use self::option::OptionExt;
pub use self::result::Result;
pub use self::result::ResultExt;
24 changes: 24 additions & 0 deletions exn/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use exn::ErrorExt;
use exn::Exn;
use exn::IteratorExt;
use exn::OptionExt;
use exn::ResultExt;

Expand Down Expand Up @@ -116,3 +117,26 @@ fn test_ensure_fail() {
let result = foo();
insta::assert_debug_snapshot!(result.unwrap_err());
}

#[test]
fn test_iterator_ext_ok() {
[Ok::<_, SimpleError>(()), Ok(()), Ok(())]
.into_iter()
.collect_all::<(), Vec<_>>()
.unwrap();
}

#[test]
fn test_iterator_ext_err() {
let result = [
Ok(()),
Err(SimpleError("E1")),
Ok(()),
Err(SimpleError("E2")),
]
.into_iter()
.collect_all::<(), Vec<_>>()
.map_err(|errs| Exn::from_iter(errs, SimpleError("An error")));

insta::assert_debug_snapshot!(result.unwrap_err());
}
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__bail.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:92:9
An error, at exn/tests/simple.rs:93:9
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__ensure_fail.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:112:9
An error, at exn/tests/simple.rs:113:9
10 changes: 5 additions & 5 deletions exn/tests/snapshots/simple__error_straightforward.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
source: exn/tests/simple.rs
expression: e5
---
E5, at exn/tests/simple.rs:37:17
E5, at exn/tests/simple.rs:38:17
|
|-> E4, at exn/tests/simple.rs:36:17
|-> E4, at exn/tests/simple.rs:37:17
|
|-> E3, at exn/tests/simple.rs:35:17
|-> E3, at exn/tests/simple.rs:36:17
|
|-> E2, at exn/tests/simple.rs:34:17
|-> E2, at exn/tests/simple.rs:35:17
|
|-> E1, at exn/tests/simple.rs:33:32
|-> E1, at exn/tests/simple.rs:34:32
24 changes: 12 additions & 12 deletions exn/tests/snapshots/simple__error_tree.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
source: exn/tests/simple.rs
expression: e6
---
E6, at exn/tests/simple.rs:60:14
E6, at exn/tests/simple.rs:61:14
|
|-> E5, at exn/tests/simple.rs:52:14
|-> E5, at exn/tests/simple.rs:53:14
| |
| |-> E3, at exn/tests/simple.rs:44:17
| |-> E3, at exn/tests/simple.rs:45:17
| | |
| | |-> E1, at exn/tests/simple.rs:43:32
| | |-> E1, at exn/tests/simple.rs:44:32
| |
| |-> E10, at exn/tests/simple.rs:47:18
| |-> E10, at exn/tests/simple.rs:48:18
| | |
| | |-> E9, at exn/tests/simple.rs:46:32
| | |-> E9, at exn/tests/simple.rs:47:32
| |
| |-> E12, at exn/tests/simple.rs:50:19
| |-> E12, at exn/tests/simple.rs:51:19
| |
| |-> E11, at exn/tests/simple.rs:49:34
| |-> E11, at exn/tests/simple.rs:50:34
|
|-> E4, at exn/tests/simple.rs:55:17
|-> E4, at exn/tests/simple.rs:56:17
| |
| |-> E2, at exn/tests/simple.rs:54:32
| |-> E2, at exn/tests/simple.rs:55:32
|
|-> E8, at exn/tests/simple.rs:58:17
|-> E8, at exn/tests/simple.rs:59:17
|
|-> E7, at exn/tests/simple.rs:57:32
|-> E7, at exn/tests/simple.rs:58:32
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__from_error.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:81:9
An error, at exn/tests/simple.rs:82:9
9 changes: 9 additions & 0 deletions exn/tests/snapshots/simple__iterator_ext_err.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:139:21
|
|-> E1, at exn/tests/simple.rs:139:21
|
|-> E2, at exn/tests/simple.rs:139:21
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__option_ext.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:74:25
An error, at exn/tests/simple.rs:75:25
4 changes: 2 additions & 2 deletions exn/tests/snapshots/simple__result_ext.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
Another error, at exn/tests/simple.rs:67:25
Another error, at exn/tests/simple.rs:68:25
|
|-> An error, at exn/tests/simple.rs:67:25
|-> An error, at exn/tests/simple.rs:68:25
Loading