Skip to content

Commit faef954

Browse files
authored
Drop array::from_fn workaround
Now that MRSV is 1.70, issue #1 cannot be observed on any supported Rust version. Fixes #1.
1 parent 29cb9bf commit faef954

File tree

1 file changed

+2
-70
lines changed

1 file changed

+2
-70
lines changed

src/lib.rs

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ pub trait IteratorILP: Iterator + Sized + TrustedLowerBound {
321321
for _ in 0..stream_len {
322322
// Fetch one Option<Item> per stream
323323
let item_opts: [Option<Self::Item>; STREAMS] =
324-
array_from_fn(|_| unsafe { next_unchecked(&mut iter) });
324+
core::array::from_fn(|_| unsafe { next_unchecked(&mut iter) });
325325

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

426426
// Set up accumulators
427-
let mut accumulators = array_from_fn::<STREAMS, _>(|_| Some(neutral()));
427+
let mut accumulators = core::array::from_fn::<STREAMS, _>(|_| Some(neutral()));
428428
let mut accumulate = |accumulator: &mut Option<Acc>, item| {
429429
if let Some(prev_acc) = accumulator.take() {
430430
*accumulator = Some(accumulate(prev_acc, item));
@@ -545,74 +545,6 @@ unsafe fn next_unchecked<Item>(iter: &mut impl Iterator<Item = Item>) -> Item {
545545
}
546546
}
547547

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-
616548
/// An iterator that reports an accurate lower bound using [`size_hint()`]
617549
///
618550
/// # Safety

0 commit comments

Comments
 (0)