rustc_type_ir::interner

Trait CollectAndApply

Source
pub trait CollectAndApply<T, R>: Sized {
    type Output;

    // Required method
    fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
       where I: Iterator<Item = Self>,
             F: FnOnce(&[T]) -> R;
}
Expand description

Imagine you have a function F: FnOnce(&[T]) -> R, plus an iterator iter that produces T items. You could combine them with f(&iter.collect::<Vec<_>>()), but this requires allocating memory for the Vec.

This trait allows for faster implementations, intended for cases where the number of items produced by the iterator is small. There is a blanket impl for T items, but there is also a fallible impl for Result<T, E> items.

Required Associated Types§

Required Methods§

Source

fn collect_and_apply<I, F>(iter: I, f: F) -> Self::Output
where I: Iterator<Item = Self>, F: FnOnce(&[T]) -> R,

Produce a result of type Self::Output from iter. The result will typically be produced by applying f on the elements produced by iter, though this may not happen in some impls, e.g. if an error occurred during iteration.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, R, E> CollectAndApply<T, R> for Result<T, E>

A fallible impl that will fail, without calling f, if there are any errors during collection.

Source§

fn collect_and_apply<I, F>(iter: I, f: F) -> Result<R, E>
where I: Iterator<Item = Result<T, E>>, F: FnOnce(&[T]) -> R,

Equivalent to Ok(f(&iter.collect::<Result<Vec<_>>>()?)).

Source§

type Output = Result<R, E>

Implementors§

Source§

impl<T, R> CollectAndApply<T, R> for T

The blanket impl that always collects all elements and applies f.