changeset 49979:f5b168979626

rust: move `filter_map_results` to public util This is a useful general-purpose function. It will be used in the next changesets.
author Raphaël Gomès <rgomes@octobus.net>
date Wed, 11 Jan 2023 17:27:19 +0100
parents e43f91244de2
children 95ffa065204e
files rust/hg-core/src/dirstate_tree/dirstate_map.rs rust/hg-core/src/utils.rs
diffstat 2 files changed, 21 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Wed Jan 11 15:44:21 2023 +0100
+++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs	Wed Jan 11 17:27:19 2023 +0100
@@ -15,6 +15,7 @@
 use crate::dirstate::StateMapIter;
 use crate::dirstate::TruncatedTimestamp;
 use crate::matchers::Matcher;
+use crate::utils::filter_map_results;
 use crate::utils::hg_path::{HgPath, HgPathBuf};
 use crate::DirstateEntry;
 use crate::DirstateError;
@@ -912,26 +913,6 @@
     }
 }
 
-/// Like `Iterator::filter_map`, but over a fallible iterator of `Result`s.
-///
-/// The callback is only called for incoming `Ok` values. Errors are passed
-/// through as-is. In order to let it use the `?` operator the callback is
-/// expected to return a `Result` of `Option`, instead of an `Option` of
-/// `Result`.
-fn filter_map_results<'a, I, F, A, B, E>(
-    iter: I,
-    f: F,
-) -> impl Iterator<Item = Result<B, E>> + 'a
-where
-    I: Iterator<Item = Result<A, E>> + 'a,
-    F: Fn(A) -> Result<Option<B>, E> + 'a,
-{
-    iter.filter_map(move |result| match result {
-        Ok(node) => f(node).transpose(),
-        Err(e) => Some(Err(e)),
-    })
-}
-
 type DebugDirstateTuple<'a> = (&'a HgPath, (u8, i32, i32, i32));
 
 impl OwningDirstateMap {
--- a/rust/hg-core/src/utils.rs	Wed Jan 11 15:44:21 2023 +0100
+++ b/rust/hg-core/src/utils.rs	Wed Jan 11 17:27:19 2023 +0100
@@ -477,3 +477,23 @@
         Ok(())
     }
 }
+
+/// Like `Iterator::filter_map`, but over a fallible iterator of `Result`s.
+///
+/// The callback is only called for incoming `Ok` values. Errors are passed
+/// through as-is. In order to let it use the `?` operator the callback is
+/// expected to return a `Result` of `Option`, instead of an `Option` of
+/// `Result`.
+pub fn filter_map_results<'a, I, F, A, B, E>(
+    iter: I,
+    f: F,
+) -> impl Iterator<Item = Result<B, E>> + 'a
+where
+    I: Iterator<Item = Result<A, E>> + 'a,
+    F: Fn(A) -> Result<Option<B>, E> + 'a,
+{
+    iter.filter_map(move |result| match result {
+        Ok(node) => f(node).transpose(),
+        Err(e) => Some(Err(e)),
+    })
+}