@@ -22,6 +22,7 @@ use core::str::next_code_point;
22
22
23
23
use crate :: borrow:: Cow ;
24
24
use crate :: char;
25
+ use crate :: collections:: TryReserveError ;
25
26
use crate :: fmt;
26
27
use crate :: hash:: { Hash , Hasher } ;
27
28
use crate :: iter:: FromIterator ;
@@ -231,11 +232,47 @@ impl Wtf8Buf {
231
232
self . bytes . reserve ( additional)
232
233
}
233
234
235
+ /// Tries to reserve capacity for at least `additional` more elements to be inserted
236
+ /// in the given `Wtf8Buf`. The collection may reserve more space to avoid
237
+ /// frequent reallocations. After calling `try_reserve`, capacity will be
238
+ /// greater than or equal to `self.len() + additional`. Does nothing if
239
+ /// capacity is already sufficient.
240
+ ///
241
+ /// # Errors
242
+ ///
243
+ /// If the capacity overflows, or the allocator reports a failure, then an error
244
+ /// is returned.
245
+ #[ inline]
246
+ pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
247
+ self . bytes . try_reserve ( additional)
248
+ }
249
+
234
250
#[ inline]
235
251
pub fn reserve_exact ( & mut self , additional : usize ) {
236
252
self . bytes . reserve_exact ( additional)
237
253
}
238
254
255
+ /// Tries to reserve the minimum capacity for exactly `additional`
256
+ /// elements to be inserted in the given `Wtf8Buf`. After calling
257
+ /// `try_reserve_exact`, capacity will be greater than or equal to
258
+ /// `self.len() + additional` if it returns `Ok(())`.
259
+ /// Does nothing if the capacity is already sufficient.
260
+ ///
261
+ /// Note that the allocator may give the collection more space than it
262
+ /// requests. Therefore, capacity can not be relied upon to be precisely
263
+ /// minimal. Prefer [`try_reserve`] if future insertions are expected.
264
+ ///
265
+ /// [`try_reserve`]: Wtf8Buf::try_reserve
266
+ ///
267
+ /// # Errors
268
+ ///
269
+ /// If the capacity overflows, or the allocator reports a failure, then an error
270
+ /// is returned.
271
+ #[ inline]
272
+ pub fn try_reserve_exact ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
273
+ self . bytes . try_reserve_exact ( additional)
274
+ }
275
+
239
276
#[ inline]
240
277
pub fn shrink_to_fit ( & mut self ) {
241
278
self . bytes . shrink_to_fit ( )
0 commit comments