Skip to content

Commit

Permalink
Make ArrayFlatMap compatible with Rust 1.52.1 (current MSRV).
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Sep 23, 2021
1 parent b8b35ff commit 92e15f2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = u8> + 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")]
Expand Down
22 changes: 14 additions & 8 deletions src/polyfill/array_flat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, Item, F, const LEN: usize> {
inner: FlatMap<I, [Item; LEN], F>,
// TODO(rust 1.53.0): `FlatMap<I, [Item; LEN], F>`
inner: FlatMap<I, core::array::IntoIter<Item, LEN>, F>,
remaining: usize,
}

impl<I, Item, F, const LEN: usize> ArrayFlatMap<I, Item, F, LEN>
where
I: ExactSizeIterator,
F: FnMut(I::Item) -> [Item; LEN],
F: FnMut(I::Item) -> core::array::IntoIter<Item, LEN>,
{
/// Constructs an `ArrayFlatMap` wrapping the given iterator, using the
/// given function
Expand All @@ -30,8 +31,8 @@ where
impl<I, Item, F, const LEN: usize> Clone for ArrayFlatMap<I, Item, F, LEN>
where
I: Iterator,
F: FnMut(I::Item) -> [Item; LEN],
FlatMap<I, [Item; LEN], F>: Clone,
F: FnMut(I::Item) -> core::array::IntoIter<Item, LEN>,
FlatMap<I, core::array::IntoIter<Item, LEN>, F>: Clone,
{
fn clone(&self) -> Self {
Self {
Expand All @@ -44,7 +45,7 @@ where
impl<I, Item, F, const LEN: usize> Iterator for ArrayFlatMap<I, Item, F, LEN>
where
I: Iterator,
F: FnMut(I::Item) -> [Item; LEN],
F: FnMut(I::Item) -> core::array::IntoIter<Item, LEN>,
{
type Item = Item;

Expand All @@ -65,7 +66,7 @@ where
impl<I, Item, F, const LEN: usize> ExactSizeIterator for ArrayFlatMap<I, Item, F, LEN>
where
I: Iterator,
F: FnMut(I::Item) -> [Item; LEN],
F: FnMut(I::Item) -> core::array::IntoIter<Item, LEN>,
{
}

Expand Down Expand Up @@ -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::<Vec<_>>(), expected);
for i in 0..expected.len() {
let len = mapped.len();
Expand Down Expand Up @@ -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::<usize>());
Expand Down

0 comments on commit 92e15f2

Please sign in to comment.