Skip to content

Commit 42aae9b

Browse files
authored
Merge pull request #3 from HadrienG2/HadrienG2-patch-1
Drop array::from_fn workaround
2 parents 29cb9bf + 54eac24 commit 42aae9b

File tree

1 file changed

+2
-71
lines changed

1 file changed

+2
-71
lines changed

src/lib.rs

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ use core::{
146146
cell::RefCell,
147147
hint::unreachable_unchecked,
148148
iter::{ExactSizeIterator, Iterator},
149-
mem::MaybeUninit,
150149
ops::{Add, Mul},
151150
};
152151
use num_traits::{One, Zero};
@@ -321,7 +320,7 @@ pub trait IteratorILP: Iterator + Sized + TrustedLowerBound {
321320
for _ in 0..stream_len {
322321
// Fetch one Option<Item> per stream
323322
let item_opts: [Option<Self::Item>; STREAMS] =
324-
array_from_fn(|_| unsafe { next_unchecked(&mut iter) });
323+
core::array::from_fn(|_| unsafe { next_unchecked(&mut iter) });
325324

326325
// Check if the item of interest was found
327326
if let Some(item) = item_opts.into_iter().flatten().next() {
@@ -424,7 +423,7 @@ pub trait IteratorILP: Iterator + Sized + TrustedLowerBound {
424423
assert_ne!(STREAMS, 0, "Need at least one stream to make progress");
425424

426425
// Set up accumulators
427-
let mut accumulators = array_from_fn::<STREAMS, _>(|_| Some(neutral()));
426+
let mut accumulators: [Option<Acc>; STREAMS] = core::array::from_fn(|_| Some(neutral()));
428427
let mut accumulate = |accumulator: &mut Option<Acc>, item| {
429428
if let Some(prev_acc) = accumulator.take() {
430429
*accumulator = Some(accumulate(prev_acc, item));
@@ -545,74 +544,6 @@ unsafe fn next_unchecked<Item>(iter: &mut impl Iterator<Item = Item>) -> Item {
545544
}
546545
}
547546

548-
/// Build an array using a mapping from index to value
549-
///
550-
/// This may look suspiciously like `std::array::from_fn()`, because it is
551-
/// actually a clone of that function. Unfortunately, at the time of
552-
/// writing, the real thing does not optimize well because the compiler fails to
553-
/// inline some steps, so we need to clone it for performance...
554-
#[inline]
555-
fn array_from_fn<const SIZE: usize, T>(mut idx_to_elem: impl FnMut(usize) -> T) -> [T; SIZE] {
556-
let mut array = PartialArray::new();
557-
for idx in 0..SIZE {
558-
array.push(idx_to_elem(idx));
559-
}
560-
array.collect()
561-
}
562-
563-
/// Partially initialized array
564-
struct PartialArray<T, const N: usize> {
565-
inner: MaybeUninit<[T; N]>,
566-
num_initialized: usize,
567-
}
568-
//
569-
impl<T, const N: usize> PartialArray<T, N> {
570-
/// Prepare to iteratively initialize an array
571-
#[inline]
572-
fn new() -> Self {
573-
Self {
574-
inner: MaybeUninit::uninit(),
575-
num_initialized: 0,
576-
}
577-
}
578-
579-
/// Initialize the next array element
580-
#[inline]
581-
fn push(&mut self, value: T) {
582-
assert!(self.num_initialized < N);
583-
unsafe {
584-
let ptr = self
585-
.inner
586-
.as_mut_ptr()
587-
.cast::<T>()
588-
.add(self.num_initialized);
589-
ptr.write(value);
590-
self.num_initialized += 1;
591-
}
592-
}
593-
594-
/// Assume the array is fully initialized and collect its value
595-
#[inline]
596-
fn collect(self) -> [T; N] {
597-
assert_eq!(self.num_initialized, N);
598-
unsafe {
599-
let result = self.inner.assume_init_read();
600-
core::mem::forget(self);
601-
result
602-
}
603-
}
604-
}
605-
//
606-
impl<T, const N: usize> Drop for PartialArray<T, N> {
607-
/// Drop already initialized elements on panic
608-
fn drop(&mut self) {
609-
let ptr = self.inner.as_mut_ptr().cast::<T>();
610-
for idx in 0..self.num_initialized {
611-
unsafe { ptr.add(idx).drop_in_place() };
612-
}
613-
}
614-
}
615-
616547
/// An iterator that reports an accurate lower bound using [`size_hint()`]
617548
///
618549
/// # Safety

0 commit comments

Comments
 (0)