Skip to content

Commit

Permalink
Bump::alloc_str
Browse files Browse the repository at this point in the history
This is similar to allocating slices, but for string slices. Because
string slices are a strange beast and none of the other methods can be
used *directly*. It could be done at the caller side, but then the user
would need to use `unsafe` which might be unacceptable in business-logic
applications or crates (or be content with the needless check for utf8
which is already guaranteed to be valid).
  • Loading branch information
vorner committed Nov 21, 2019
1 parent e61f3a4 commit b633583
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* Add `alloc_str`, which is similar to `alloc_slice_*` methods, but works on
string slices.

# 2.6.0

Released 2019-08-19.
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ use core::marker::PhantomData;
use core::mem;
use core::ptr::{self, NonNull};
use core::slice;
use core::str;
use core_alloc::alloc::{alloc, dealloc, Layout};

/// An arena to bump allocate into.
Expand Down Expand Up @@ -640,6 +641,29 @@ impl Bump {
}
}

/// `Copy` a string slice into this `Bump` and return an exclusive reference to it.
///
/// ## Panics
///
/// Panics if reserving space for the string would cause an overflow.
///
/// ## Example
///
/// ```
/// let bump = bumpalo::Bump::new();
/// let hello = bump.alloc_str("hello world");
/// assert_eq!("hello world", hello);
/// ```
#[inline(always)]
#[allow(clippy::mut_from_ref)]
pub fn alloc_str(&self, src: &str) -> &mut str {
let buffer = self.alloc_slice_copy(src.as_bytes());
unsafe {
// This is OK, because it already came in as str, so it is guaranteed to be utf8
str::from_utf8_unchecked_mut(buffer)
}
}

/// Allocates a new slice of size `len` into this `Bump` and returns an
/// exclusive reference to the copy.
///
Expand Down
8 changes: 8 additions & 0 deletions tests/quickchecks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,12 @@ quickcheck! {
allocated.push(range);
}
}

fn alloc_strs(allocs: Vec<String>) -> () {

This comment has been minimized.

Copy link
@95th

95th Nov 22, 2019

why -> ()?

This comment has been minimized.

Copy link
@vorner

vorner Nov 22, 2019

Author Contributor

I don't know. All the others had it so I didn't break the habit. I guess it's some quirk of the quickcheck! macro this lives in.

This comment has been minimized.

Copy link
@fitzgen

fitzgen Nov 22, 2019

Owner

The macro doesn't have a case for implicit unit return, so you have to spell it out.

let b = Bump::new();
let allocated: Vec<&str> = allocs.iter().map(|s| b.alloc_str(s) as &_).collect();
for (val, alloc) in allocs.into_iter().zip(allocated) {
assert_eq!(val, alloc);
}
}
}

0 comments on commit b633583

Please sign in to comment.