From 92e15f23dc7c12829990f5fef09062a69d54dbf7 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 23 Sep 2021 13:07:01 -0700 Subject: [PATCH] Make `ArrayFlatMap` compatible with Rust 1.52.1 (current MSRV). --- src/limb.rs | 5 ++++- src/polyfill/array_flat_map.rs | 22 ++++++++++++++-------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/limb.rs b/src/limb.rs index 35560f0b30..4d189e39de 100644 --- a/src/limb.rs +++ b/src/limb.rs @@ -249,7 +249,10 @@ pub fn big_endian_from_limbs(limbs: &[Limb], out: &mut [u8]) { /// The number of bytes returned will be a multiple of `LIMB_BYTES`. fn be_bytes(limbs: &[Limb]) -> impl ExactSizeIterator + Clone + '_ { // The unwrap is safe because a slice can never be larger than `usize` bytes. - ArrayFlatMap::new(limbs.iter().rev().copied(), Limb::to_be_bytes).unwrap() + ArrayFlatMap::new(limbs.iter().rev().copied(), |limb| { + core::array::IntoIter::new(Limb::to_be_bytes(limb)) + }) + .unwrap() } #[cfg(feature = "alloc")] diff --git a/src/polyfill/array_flat_map.rs b/src/polyfill/array_flat_map.rs index b7f7be9dab..4818ddc0c2 100644 --- a/src/polyfill/array_flat_map.rs +++ b/src/polyfill/array_flat_map.rs @@ -9,14 +9,15 @@ use core::iter::FlatMap; /// length of the flat-mapped result is `inner_len * LEN`. (The constructor /// verifies that this multiplication doesn't overflow `usize`.) pub struct ArrayFlatMap { - inner: FlatMap, + // TODO(rust 1.53.0): `FlatMap` + inner: FlatMap, F>, remaining: usize, } impl ArrayFlatMap where I: ExactSizeIterator, - F: FnMut(I::Item) -> [Item; LEN], + F: FnMut(I::Item) -> core::array::IntoIter, { /// Constructs an `ArrayFlatMap` wrapping the given iterator, using the /// given function @@ -30,8 +31,8 @@ where impl Clone for ArrayFlatMap where I: Iterator, - F: FnMut(I::Item) -> [Item; LEN], - FlatMap: Clone, + F: FnMut(I::Item) -> core::array::IntoIter, + FlatMap, F>: Clone, { fn clone(&self) -> Self { Self { @@ -44,7 +45,7 @@ where impl Iterator for ArrayFlatMap where I: Iterator, - F: FnMut(I::Item) -> [Item; LEN], + F: FnMut(I::Item) -> core::array::IntoIter, { type Item = Item; @@ -65,7 +66,7 @@ where impl ExactSizeIterator for ArrayFlatMap where I: Iterator, - F: FnMut(I::Item) -> [Item; LEN], + F: FnMut(I::Item) -> core::array::IntoIter, { } @@ -93,7 +94,10 @@ mod tests { ), ]; TEST_CASES.iter().copied().for_each(|(input, f, expected)| { - let mut mapped = ArrayFlatMap::new(input.iter().copied(), f).unwrap(); + let mut mapped = ArrayFlatMap::new(input.iter().copied(), |input| { + core::array::IntoIter::new(f(input)) + }) + .unwrap(); assert_eq!(&mapped.clone().collect::>(), expected); for i in 0..expected.len() { let len = mapped.len(); @@ -138,7 +142,9 @@ mod tests { let inner = DownwardCounter { remaining: input_len, }; - let mapped = ArrayFlatMap::new(inner, usize::to_be_bytes); + let mapped = ArrayFlatMap::new(inner, |input| { + core::array::IntoIter::new(usize::to_be_bytes(input)) + }); assert_eq!(mapped.is_some(), is_some); if let Some(mapped) = mapped { assert_eq!(mapped.len(), input_len * core::mem::size_of::());