wagon_utils/
fallible_itertools.rs

1
2use std::cmp::Ordering;
3
4use itertools::Itertools;
5
6extern crate alloc;
7
8mod extrema_set;
9
10/// A slightly modified version of the [`Itertools`] trait which returns [`Result`] instead.
11///
12/// Automatically implemented for anything that implements [`Itertools`]. This trait is intended
13/// for methods which require fallible operations (comparisons, additions etc.). Not all
14/// [`Itertools`] methods have been ported over. More may be added as needed.
15pub trait FallibleItertools: Itertools {
16    /// Return all minimum elements of an iterator, as determined by
17    /// the specified function. Or an error if the comparison fails.
18    ///
19    /// # Examples
20    ///
21    /// ```
22    /// # use std::cmp::Ordering;
23    /// use wagon_utils::FallibleItertools;
24    ///
25    /// let a: [(i32, i32); 0] = [];
26    /// assert_eq!(a.iter().fallible_min_set_by(|_, _| Ok::<std::cmp::Ordering, ()>(Ordering::Equal)), Ok(Vec::<&(i32, i32)>::new()));
27    ///
28    /// let a = [(1, 2)];
29    /// assert_eq!(a.iter().fallible_min_set_by(|&&(k1,_), &&(k2, _)| Ok::<std::cmp::Ordering, ()>(k1.cmp(&k2))), Ok(vec![&(1, 2)]));
30    ///
31    /// let a = [(1, 2), (2, 2), (3, 9), (4, 8), (5, 9)];
32    /// assert_eq!(a.iter().fallible_min_set_by(|&&(_,k1), &&(_,k2)| Err("error")), Err("error"));
33    ///
34    /// let a = [(1, 2), (1, 3), (1, 4), (1, 5)];
35    /// assert_eq!(a.iter().fallible_min_set_by(|&&(k1,_), &&(k2, _)| Ok::<std::cmp::Ordering, ()>(k1.cmp(&k2))), Ok(vec![&(1, 2), &(1, 3), &(1, 4), &(1, 5)]));
36    /// ```
37    ///
38    /// The elements can be floats but no particular result is guaranteed
39    /// if an element is NaN.
40    ///
41    /// # Errors
42    /// Returns an error if the comparison function returns an error at any point.
43    fn fallible_min_set_by<F, E>(self, mut compare: F) -> Result<Vec<Self::Item>, E>
44    where
45        Self: Sized,
46        F: FnMut(&Self::Item, &Self::Item) -> Result<Ordering, E>,
47    {
48        extrema_set::min_set_impl(self, |_| (), |x, y, (), ()| compare(x, y))
49    }
50
51    /// Return all maximum elements of an iterator, as determined by
52    /// the specified function.
53    ///
54    /// # Examples
55    ///
56    /// ```
57    /// # use std::cmp::Ordering;
58    /// use wagon_utils::FallibleItertools;
59    ///
60    /// let a: [(i32, i32); 0] = [];
61    /// assert_eq!(a.iter().fallible_max_set_by(|_, _| Ok::<std::cmp::Ordering, ()>(Ordering::Equal)), Ok(Vec::<&(i32, i32)>::new()));
62    ///
63    /// let a = [(1, 2)];
64    /// assert_eq!(a.iter().fallible_max_set_by(|&&(k1,_), &&(k2, _)| Ok::<std::cmp::Ordering, ()>(k1.cmp(&k2))), Ok(vec![&(1, 2)]));
65    ///
66    /// let a = [(1, 2), (2, 2), (3, 9), (4, 8), (5, 9)];
67    /// assert_eq!(a.iter().fallible_max_set_by(|&&(_,k1), &&(_,k2)| Err("error")), Err("error"));
68    ///
69    /// let a = [(1, 2), (1, 3), (1, 4), (1, 5)];
70    /// assert_eq!(a.iter().fallible_max_set_by(|&&(k1,_), &&(k2, _)| Ok::<std::cmp::Ordering, ()>(k1.cmp(&k2))), Ok(vec![&(1, 2), &(1, 3), &(1, 4), &(1, 5)]));
71    /// ```
72    ///
73    /// The elements can be floats but no particular result is guaranteed
74    /// if an element is NaN.
75    ///
76    /// # Errors
77    /// Returns an error if the comparison function returns an error at any point.
78    fn fallible_max_set_by<F, E>(self, mut compare: F) -> Result<Vec<Self::Item>, E>
79    where
80        Self: Sized,
81        F: FnMut(&Self::Item, &Self::Item) -> Result<Ordering, E>,
82    {
83        extrema_set::max_set_impl(self, |_| (), |x, y, (), ()| compare(x, y))
84    }
85}
86
87impl<T> FallibleItertools for T where T: Itertools {}