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 Vec::bump method #189

Merged
merged 1 commit into from Jan 9, 2023
Merged

Add Vec::bump method #189

merged 1 commit into from Jan 9, 2023

Conversation

mattfbacon
Copy link
Contributor

This PR adds a way to get the Bump allocator backing a Vec, for example to make more allocations if the Vec contains other bump-allocated data structures.

One common example of this is a bump-allocated recursive data structure. Consider:

use bumpalo::Bump;
use bumpalo::collections::Vec;

struct Tree<'bump> {
	value: i32,
	children: Vec<'bump, Self>,
}

impl Tree<'bump> {
	fn new(value: i32, bump: &'bump Bump) -> Self {
		Self {
			value,
			children: Vec::new_in(bump),
		}
	}

	fn add_child(&mut self, value: i32) {
		self.children.push(Self::new(value, self.children.bump()));
	}
}

Without this PR, Recursive would be forced to carry an extra shared reference to the Bump.

Copy link
Owner

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Thanks!

Would you be interested in adding Box::bump and String::bump as well? Note the the box version should be a static method to match the std Box::allocator method.

@fitzgen fitzgen merged commit 701514f into fitzgen:main Jan 9, 2023
@mattfbacon mattfbacon mentioned this pull request Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants