Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collect_in for iterators. #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 53 additions & 5 deletions src/collections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
//! [`vec!`]: ../../macro.vec.html

use super::raw_vec::RawVec;
use crate::Bump;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{self, Hash};
Expand All @@ -96,7 +97,6 @@ use core::ops::{Index, IndexMut, RangeBounds};
use core::ptr;
use core::ptr::NonNull;
use std::slice;
use crate::Bump;

unsafe fn arith_offset<T>(p: *const T, offset: isize) -> *const T {
p.offset(offset)
Expand Down Expand Up @@ -1848,13 +1848,20 @@ macro_rules! __impl_slice_eq1 {
__impl_slice_eq1! { $Lhs, $Rhs, Sized }
};
($Lhs: ty, $Rhs: ty, $Bound: ident) => {
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs
where
A: PartialEq<B>,
{
#[inline]
fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
fn eq(&self, other: &$Rhs) -> bool {
self[..] == other[..]
}
#[inline]
fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
fn ne(&self, other: &$Rhs) -> bool {
self[..] != other[..]
}
}
}
};
}

__impl_slice_eq1! { Vec<'a, A>, Vec<'b, B> }
Expand Down Expand Up @@ -2339,3 +2346,44 @@ where
}
}
}

/// Similar to `std::iter::FromIterator`, but for a bump vec.
pub trait FromIteratorBump<'bump, A> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this FromIteratorIn and its method from_iter_in

/// Similar to `std::iter::FromIterator::from_iter`, but for a bump vec.
fn from_iter<T>(iter: T, bump: &'bump Bump) -> Self
where
T: IntoIterator<Item = A>;
}

impl<'bump, T> FromIteratorBump<'bump, T> for Vec<'bump, T> {
fn from_iter<I>(iter: I, bump: &'bump Bump) -> Self
where
I: IntoIterator<Item = T>,
{
let mut v = Vec::new_in(bump);
for t in iter.into_iter() {
v.push(t);
}
v
}
}

/// Extension to `Iterator` trait to allow for collecting in bump alloc.
pub trait IteratorBump: Iterator {
/// Like `Iterator::collect` but allocates into a bump arena.
fn collect_in<'bump, B>(self, bump: &'bump Bump) -> B
where
B: FromIteratorBump<'bump, <Self as Iterator>::Item>;
}

impl<T> IteratorBump for T
where
T: Iterator,
{
fn collect_in<'bump, B>(self, bump: &'bump Bump) -> B
where
B: FromIteratorBump<'bump, <Self as Iterator>::Item>,
{
<B as FromIteratorBump<_>>::from_iter(self, bump)
}
}
7 changes: 7 additions & 0 deletions tests/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ fn push_a_bunch_of_items() {
v.push(x);
}
}

#[test]
fn collect_in_a_bump_arena() {
use bumpalo::collections::vec::{IteratorBump, Vec};
let b = Bump::new();
(0..10_000).collect_in::<Vec<_>>(&b);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also test that it contains the items we expect.

}