File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change 93
93
is_char_boundary,
94
94
char_at,
95
95
reserve,
96
+ reserve_at_least,
96
97
97
98
unsafe ;
98
99
@@ -1505,6 +1506,30 @@ fn reserve(&s: str, n: uint) {
1505
1506
rustrt:: str_reserve_shared ( s, n) ;
1506
1507
}
1507
1508
1509
+ #[ doc = "
1510
+ Reserves capacity for at least `n` bytes in the given string, not including
1511
+ the null terminator.
1512
+
1513
+ Assuming single-byte characters, the resulting string will be large
1514
+ enough to hold a string of length `n`. To account for the null terminator,
1515
+ the underlying buffer will have the size `n` + 1.
1516
+
1517
+ This function will over-allocate in order to amortize the allocation costs
1518
+ in scenarios where the caller may need to repeatedly reserve additional
1519
+ space.
1520
+
1521
+ If the capacity for `s` is already equal to or greater than the requested
1522
+ capacity, then no action is taken.
1523
+
1524
+ # Arguments
1525
+
1526
+ * s - A string
1527
+ * n - The number of bytes to reserve space for
1528
+ " ]
1529
+ fn reserve_at_least ( & s: str , n : uint ) unsafe {
1530
+ reserve ( s, uint:: next_power_of_two ( n + 1 u) - 1 u)
1531
+ }
1532
+
1508
1533
#[ doc = "Unsafe operations" ]
1509
1534
mod unsafe {
1510
1535
export
You can’t perform that action at this time.
0 commit comments