Trait bumpalo::collections::FromIteratorIn
source · pub trait FromIteratorIn<A> {
type Alloc;
// Required method
fn from_iter_in<I>(iter: I, alloc: Self::Alloc) -> Self
where I: IntoIterator<Item = A>;
}
Expand description
A trait for types that support being constructed from an iterator, parameterized by an allocator.
Required Associated Types§
Required Methods§
sourcefn from_iter_in<I>(iter: I, alloc: Self::Alloc) -> Selfwhere
I: IntoIterator<Item = A>,
fn from_iter_in<I>(iter: I, alloc: Self::Alloc) -> Selfwhere I: IntoIterator<Item = A>,
Similar to FromIterator::from_iter
, but with a given allocator.
let five_fives = std::iter::repeat(5).take(5);
let bump = Bump::new();
let v = Vec::from_iter_in(five_fives, &bump);
assert_eq!(v, [5, 5, 5, 5, 5]);
Implementations on Foreign Types§
source§impl<T, E, V: FromIteratorIn<T>> FromIteratorIn<Result<T, E>> for Result<V, E>
impl<T, E, V: FromIteratorIn<T>> FromIteratorIn<Result<T, E>> for Result<V, E>
source§fn from_iter_in<I>(iter: I, alloc: Self::Alloc) -> Selfwhere
I: IntoIterator<Item = Result<T, E>>,
fn from_iter_in<I>(iter: I, alloc: Self::Alloc) -> Selfwhere I: IntoIterator<Item = Result<T, E>>,
Takes each element in the Iterator
: if it is an Err
, no further
elements are taken, and the Err
is returned. Should no Err
occur, a
container with the values of each Result
is returned.
Here is an example which increments every integer in a vector, checking for overflow:
let bump = Bump::new();
let v = vec![1, 2, u32::MAX];
let res: Result<Vec<u32>, &'static str> = v.iter().take(2).map(|x: &u32|
x.checked_add(1).ok_or("Overflow!")
).collect_in(&bump);
assert_eq!(res, Ok(bumpalo::vec![in ≎ 2, 3]));
let res: Result<Vec<u32>, &'static str> = v.iter().map(|x: &u32|
x.checked_add(1).ok_or("Overflow!")
).collect_in(&bump);
assert_eq!(res, Err("Overflow!"));