From 79a1b1572bbf5c24498a00e377be775bd1b83801 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Wed, 6 Sep 2023 09:10:14 +0200 Subject: [PATCH] Do not sum options A mistake on my part. It only returns None if the iterator yields None. But it would overflow when an addition does. I guess a `checked_sum` method would be nice. PS: Do not product options either, as multiplications could overflow the same way. That's how I found out. --- src/combinations.rs | 3 --- src/combinations_with_replacement.rs | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/combinations.rs b/src/combinations.rs index 4b0742a0d..e230119c1 100644 --- a/src/combinations.rs +++ b/src/combinations.rs @@ -208,9 +208,6 @@ fn remaining_for(n: usize, first: bool, indices: &[usize]) -> Option { indices .iter() .enumerate() - // TODO: Once the MSRV hits 1.37.0, we can sum options instead: - // .map(|(i, n0)| checked_binomial(n - 1 - *n0, k - i)) - // .sum() .fold(Some(0), |sum, (i, n0)| { sum.and_then(|s| s.checked_add(checked_binomial(n - 1 - *n0, k - i)?)) }) diff --git a/src/combinations_with_replacement.rs b/src/combinations_with_replacement.rs index 0e3a20e0c..5d6a034b5 100644 --- a/src/combinations_with_replacement.rs +++ b/src/combinations_with_replacement.rs @@ -157,9 +157,6 @@ fn remaining_for(n: usize, first: bool, indices: &[usize]) -> Option { indices .iter() .enumerate() - // TODO: Once the MSRV hits 1.37.0, we can sum options instead: - // .map(|(i, n0)| count(n - 1 - *n0, k - i)) - // .sum() .fold(Some(0), |sum, (i, n0)| { sum.and_then(|s| s.checked_add(count(n - 1 - *n0, k - i)?)) })