INNER CODE UNIT · Rust
collect_into_alloc_vec
binius-zk/binius64 · crates/compute/src/lib.rs:69
fn collect_into_alloc_vec<A: Allocator>(self, alloc: &A) -> A::Vec<Self::Item>;
}
impl<I: IndexedParallelIterator> CollectIntoAllocVec for I {
fn collect_into_alloc_vec<A: Allocator>(self, alloc: &A) -> A::Vec<Self::Item> {
let len = self.len();
let mut buffer = alloc.alloc::<Self::Item>(len);
// The allocator may hand back more capacity than requested, so bound the spare slice to the
// item count: a rayon zip yields as many items as its shorter side holds.
self.zip(&mut buffer.spare_capacity_mut()[..len])
.for_each(|(item, slot)| {
slot.write(item);
});
// SAFETY: the two zipped sides are both `len` long, so the loop wrote each of the `len`
// slots exactly once.
unsafe { buffer.set_len(len) };
buffer
}