Skip to content

Commit

Permalink
feat: BumpBox::<[MaybeUninit<T>]>::init_fill_iter (fixes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluurryy committed May 19, 2024
1 parent e3294b2 commit 282d405
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/bump_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,45 @@ impl<'a, T: Sized> BumpBox<'a, [MaybeUninit<T>]> {
initializer.into_init()
}

/// Initializes `self` by filling it with elements returned from an iterator.
///
/// # Panics
///
/// This function will panic if the iterator runs out of items before the slice is filled.
///
/// # Examples
///
/// ```
/// # use bump_scope::Bump;
/// # let mut bump: Bump = Bump::new();
/// let buf = bump.alloc_uninit_slice(5);
/// let buf = buf.init_fill_iter(['a', 'b'].iter().copied().cycle());
/// assert_eq!(buf, ['a', 'b', 'a', 'b', 'a']);
/// ```
#[must_use]
#[inline]
pub fn init_fill_iter(self, mut iter: impl Iterator<Item = T>) -> BumpBox<'a, [T]> {
#[cold]
#[inline(never)]
#[track_caller]
fn iter_ran_out() -> ! {
panic!("iterator ran out of items to fill the slice with");
}

let mut initializer = self.initializer();

while !initializer.is_full() {
match iter.next() {
Some(item) => {
initializer.push(item);
}
None => iter_ran_out(),
}
}

initializer.into_init()
}

/// Initializes `self` by copying the elements from `slice` into `self`.
///
/// The length of `slice` must be the same as `self`.
Expand Down

0 comments on commit 282d405

Please sign in to comment.