@@ -3,6 +3,7 @@ mod tests;
33
44use crate :: borrow:: { Borrow , Cow } ;
55use crate :: cmp;
6+ use crate :: collections:: TryReserveError ;
67use crate :: fmt;
78use crate :: hash:: { Hash , Hasher } ;
89use crate :: iter:: { Extend , FromIterator } ;
@@ -265,6 +266,43 @@ impl OsString {
265266 self . inner . reserve ( additional)
266267 }
267268
269+ /// Tries to reserve capacity for at least `additional` more length units
270+ /// in the given `OsString`. The string may reserve more space to avoid
271+ /// frequent reallocations. After calling `try_reserve`, capacity will be
272+ /// greater than or equal to `self.len() + additional`. Does nothing if
273+ /// capacity is already sufficient.
274+ ///
275+ /// # Errors
276+ ///
277+ /// If the capacity overflows, or the allocator reports a failure, then an error
278+ /// is returned.
279+ ///
280+ /// # Examples
281+ ///
282+ /// ```
283+ /// #![feature(try_reserve_2)]
284+ /// use std::ffi::{OsStr, OsString};
285+ /// use std::collections::TryReserveError;
286+ ///
287+ /// fn process_data(data: &str) -> Result<OsString, TryReserveError> {
288+ /// let mut s = OsString::new();
289+ ///
290+ /// // Pre-reserve the memory, exiting if we can't
291+ /// s.try_reserve(OsStr::new(data).len())?;
292+ ///
293+ /// // Now we know this can't OOM in the middle of our complex work
294+ /// s.push(data);
295+ ///
296+ /// Ok(s)
297+ /// }
298+ /// # process_data("123").expect("why is the test harness OOMing on 3 bytes?");
299+ /// ```
300+ #[ unstable( feature = "try_reserve_2" , issue = "91789" ) ]
301+ #[ inline]
302+ pub fn try_reserve ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
303+ self . inner . try_reserve ( additional)
304+ }
305+
268306 /// Reserves the minimum capacity for exactly `additional` more capacity to
269307 /// be inserted in the given `OsString`. Does nothing if the capacity is
270308 /// already sufficient.
@@ -290,6 +328,49 @@ impl OsString {
290328 self . inner . reserve_exact ( additional)
291329 }
292330
331+ /// Tries to reserve the minimum capacity for exactly `additional`
332+ /// more length units in the given `OsString`. After calling
333+ /// `try_reserve_exact`, capacity will be greater than or equal to
334+ /// `self.len() + additional` if it returns `Ok(())`.
335+ /// Does nothing if the capacity is already sufficient.
336+ ///
337+ /// Note that the allocator may give the `OsString` more space than it
338+ /// requests. Therefore, capacity can not be relied upon to be precisely
339+ /// minimal. Prefer [`try_reserve`] if future insertions are expected.
340+ ///
341+ /// [`try_reserve`]: OsString::try_reserve
342+ ///
343+ /// # Errors
344+ ///
345+ /// If the capacity overflows, or the allocator reports a failure, then an error
346+ /// is returned.
347+ ///
348+ /// # Examples
349+ ///
350+ /// ```
351+ /// #![feature(try_reserve_2)]
352+ /// use std::ffi::{OsStr, OsString};
353+ /// use std::collections::TryReserveError;
354+ ///
355+ /// fn process_data(data: &str) -> Result<OsString, TryReserveError> {
356+ /// let mut s = OsString::new();
357+ ///
358+ /// // Pre-reserve the memory, exiting if we can't
359+ /// s.try_reserve_exact(OsStr::new(data).len())?;
360+ ///
361+ /// // Now we know this can't OOM in the middle of our complex work
362+ /// s.push(data);
363+ ///
364+ /// Ok(s)
365+ /// }
366+ /// # process_data("123").expect("why is the test harness OOMing on 3 bytes?");
367+ /// ```
368+ #[ unstable( feature = "try_reserve_2" , issue = "91789" ) ]
369+ #[ inline]
370+ pub fn try_reserve_exact ( & mut self , additional : usize ) -> Result < ( ) , TryReserveError > {
371+ self . inner . try_reserve_exact ( additional)
372+ }
373+
293374 /// Shrinks the capacity of the `OsString` to match its length.
294375 ///
295376 /// # Examples
0 commit comments