diff --git a/src/extensions/collections/btree_map.rs b/src/extensions/collections/btree_map.rs index 692f306..511ffb5 100644 --- a/src/extensions/collections/btree_map.rs +++ b/src/extensions/collections/btree_map.rs @@ -65,11 +65,6 @@ impl Map for BTreeMap { flat_map_pairs(self.iter(), |&x| function(x)) } - #[inline] - fn fold(&self, init: B, function: impl FnMut(B, (&Key, &Value)) -> B) -> B { - self.iter().fold(init, function) - } - #[inline] fn map(&self, mut function: impl FnMut((&Key, &Value)) -> (L, W)) -> Self::This where diff --git a/src/extensions/collections/hash_map.rs b/src/extensions/collections/hash_map.rs index 127b35c..5317ef8 100644 --- a/src/extensions/collections/hash_map.rs +++ b/src/extensions/collections/hash_map.rs @@ -64,11 +64,6 @@ impl Map for HashMap { flat_map_pairs(self.iter(), |&x| function(x)) } - #[inline] - fn fold(&self, init: B, function: impl FnMut(B, (&Key, &Value)) -> B) -> B { - self.iter().fold(init, function) - } - #[inline] fn map(&self, mut function: impl FnMut((&Key, &Value)) -> (L, W)) -> Self::This where diff --git a/src/extensions/map.rs b/src/extensions/map.rs index 251934f..b2ede17 100644 --- a/src/extensions/map.rs +++ b/src/extensions/map.rs @@ -824,7 +824,7 @@ pub trait Map { /// ]); /// /// // the sum of all the elements of the array - /// let sum = a.fold(0, |acc, (&k, &v)| acc + k + v.len()); + /// let sum = a.fold(0, |acc, (k, v)| acc + k + v.len()); /// /// assert_eq!(sum, 9); /// ``` @@ -839,7 +839,12 @@ pub trait Map { /// | 3 | 5 | 3 | c | 9 | /// /// And so, our final result, `9`. - fn fold(&self, init: B, function: impl FnMut(B, (&Key, &Value)) -> B) -> B; + fn fold(self, init: B, function: impl FnMut(B, (Key, Value)) -> B) -> B + where + Self: IntoIterator + Sized, + { + self.into_iter().fold(init, function) + } /// Creates a map by retaining the values representing the intersection /// of the original map with another map i.e., the values that are @@ -1581,7 +1586,7 @@ pub trait Map { /// /// // Which is equivalent to doing it with `fold`: /// # let a = source.clone(); - /// let folded = a.fold((0, 0), |(a, b), (&k, &v)| (a + k, b + v)); + /// let folded = a.fold((0, 0), |(a, b), (k, v)| (a + k, b + v)); /// /// assert_eq!(reduced, folded); /// ```