Skip to content

Commit bbedbc0

Browse files
committed
std: inline str::with_capacity and vec::with_capacity
1 parent cced3c9 commit bbedbc0

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/libstd/str.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ impl<'self, S: Str> StrVector for &'self [S] {
180180

181181
let len = self.iter().transform(|s| s.as_slice().len()).sum();
182182

183-
let mut s = ~"";
184-
185-
s.reserve(len);
183+
let mut s = with_capacity(len);
186184

187185
unsafe {
188186
do s.as_mut_buf |buf, _| {
@@ -678,6 +676,7 @@ pub fn from_utf16(v: &[u16]) -> ~str {
678676
* Allocates a new string with the specified capacity. The string returned is
679677
* the empty string, but has capacity for much more.
680678
*/
679+
#[inline]
681680
pub fn with_capacity(capacity: uint) -> ~str {
682681
let mut buf = ~"";
683682
buf.reserve(capacity);
@@ -1830,10 +1829,9 @@ impl<'self> StrSlice<'self> for &'self str {
18301829
/// Given a string, make a new string with repeated copies of it.
18311830
fn repeat(&self, nn: uint) -> ~str {
18321831
do self.as_imm_buf |buf, len| {
1833-
let mut ret = ~"";
18341832
// ignore the NULL terminator
18351833
let len = len - 1;
1836-
ret.reserve(nn * len);
1834+
let mut ret = with_capacity(nn * len);
18371835
18381836
unsafe {
18391837
do ret.as_mut_buf |rbuf, _len| {

src/libstd/vec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
8787
}
8888

8989
/// Creates a new vector with a capacity of `capacity`
90+
#[inline]
9091
pub fn with_capacity<T>(capacity: uint) -> ~[T] {
9192
unsafe {
9293
if contains_managed::<T>() {

0 commit comments

Comments
 (0)