Function core::array::try_from_fn_erased

source ·
fn try_from_fn_erased<T, R>(
    buffer: &mut [MaybeUninit<T>],
    generator: impl FnMut(usize) -> R
) -> ControlFlow<R::Residual>where
    R: Try<Output = T>,
Expand description

Version of try_from_fn using a passed-in slice in order to avoid needing to monomorphize for every array length.

This takes a generator rather than an iterator so that at the type level it never needs to worry about running out of items. When combined with an infallible Try type, that means the loop canonicalizes easily, allowing it to optimize well.

It would be possible to unify this and iter_next_chunk_erased into one function that does the union of both things, but last time it was that way it resulted in poor codegen from the “are there enough source items?” checks not optimizing away. So if you give it a shot, make sure to watch what happens in the codegen tests.