about summary refs log tree commit diff
path: root/src/libcore/tests
diff options
context:
space:
mode:
authorTim Vermeulen <tvermeulen@me.com>2019-07-06 23:22:20 +0200
committerTim Vermeulen <tvermeulen@me.com>2019-07-29 02:40:50 +0200
commit2e41ba8742c341eaf1ec1b5954e089e4d2c02dd0 (patch)
tree18eff9da3d21d47a8c64f992c33e88d76f24fdfe /src/libcore/tests
parent4560cb830fce63fcffdc4558f4281aaac6a3a1ba (diff)
downloadrust-2e41ba8742c341eaf1ec1b5954e089e4d2c02dd0.tar.gz
rust-2e41ba8742c341eaf1ec1b5954e089e4d2c02dd0.zip
Use internal iteration in the Sum and Product impls of Result and Option
Diffstat (limited to 'src/libcore/tests')
-rw-r--r--src/libcore/tests/iter.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libcore/tests/iter.rs b/src/libcore/tests/iter.rs
index b7b0849e212..31530cc2c94 100644
--- a/src/libcore/tests/iter.rs
+++ b/src/libcore/tests/iter.rs
@@ -1082,6 +1082,23 @@ fn test_iterator_sum_result() {
     assert_eq!(v.iter().cloned().sum::<Result<i32, _>>(), Ok(10));
     let v: &[Result<i32, ()>] = &[Ok(1), Err(()), Ok(3), Ok(4)];
     assert_eq!(v.iter().cloned().sum::<Result<i32, _>>(), Err(()));
+
+    #[derive(PartialEq, Debug)]
+    struct S(Result<i32, ()>);
+
+    impl Sum<Result<i32, ()>> for S {
+        fn sum<I: Iterator<Item = Result<i32, ()>>>(mut iter: I) -> Self {
+            // takes the sum by repeatedly calling `next` on `iter`,
+            // thus testing that repeated calls to `ResultShunt::try_fold`
+            // produce the expected results
+            Self(iter.by_ref().sum())
+        }
+    }
+
+    let v: &[Result<i32, ()>] = &[Ok(1), Ok(2), Ok(3), Ok(4)];
+    assert_eq!(v.iter().cloned().sum::<S>(), S(Ok(10)));
+    let v: &[Result<i32, ()>] = &[Ok(1), Err(()), Ok(3), Ok(4)];
+    assert_eq!(v.iter().cloned().sum::<S>(), S(Err(())));
 }
 
 #[test]