about summary refs log tree commit diff
diff options
context:
space:
mode:
authorXuanwo <github@xuanwo.io>2021-12-28 11:28:05 +0800
committerXuanwo <github@xuanwo.io>2021-12-28 11:28:05 +0800
commitc40ac57efb88b308b869be2ec47da59aba3c842c (patch)
tree23707be8198766bbe546c93cb8e593a6928cd5e6
parent4ee34f355101db4d7a97455589c9dbc335d7a963 (diff)
downloadrust-c40ac57efb88b308b869be2ec47da59aba3c842c.tar.gz
rust-c40ac57efb88b308b869be2ec47da59aba3c842c.zip
Add try_reserve for OsString
Signed-off-by: Xuanwo <github@xuanwo.io>
-rw-r--r--library/std/src/ffi/os_str.rs81
-rw-r--r--library/std/src/sys/unix/os_str.rs11
2 files changed, 92 insertions, 0 deletions
diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs
index 0f9912fa64d..2933018a495 100644
--- a/library/std/src/ffi/os_str.rs
+++ b/library/std/src/ffi/os_str.rs
@@ -3,6 +3,7 @@ mod tests;
 
 use crate::borrow::{Borrow, Cow};
 use crate::cmp;
+use crate::collections::TryReserveError;
 use crate::fmt;
 use crate::hash::{Hash, Hasher};
 use crate::iter::{Extend, FromIterator};
@@ -265,6 +266,43 @@ impl OsString {
         self.inner.reserve(additional)
     }
 
+    /// Tries to reserve capacity for at least `additional` more elements to be inserted
+    /// in the given `OsString`. The collection may reserve more space to avoid
+    /// frequent reallocations. After calling `try_reserve`, capacity will be
+    /// greater than or equal to `self.len() + additional`. Does nothing if
+    /// capacity is already sufficient.
+    ///
+    /// # Errors
+    ///
+    /// If the capacity overflows, or the allocator reports a failure, then an error
+    /// is returned.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(try_reserve_2)]
+    /// use std::ffi::OsString;
+    /// use std::collections::TryReserveError;
+    ///
+    /// fn find_max_slow(data: &str) -> Result<OsString, TryReserveError> {
+    ///     let mut s = OsString::new();
+    ///
+    ///     // Pre-reserve the memory, exiting if we can't
+    ///     s.try_reserve(data.len())?;
+    ///
+    ///     // Now we know this can't OOM in the middle of our complex work
+    ///     s.push(data);
+    ///
+    ///     Ok(s)
+    /// }
+    /// # find_max_slow("123").expect("why is the test harness OOMing on 12 bytes?");
+    /// ```
+    #[unstable(feature = "try_reserve_2", issue = "91789")]
+    #[inline]
+    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
+        self.inner.try_reserve(additional)
+    }
+
     /// Reserves the minimum capacity for exactly `additional` more capacity to
     /// be inserted in the given `OsString`. Does nothing if the capacity is
     /// already sufficient.
@@ -290,6 +328,49 @@ impl OsString {
         self.inner.reserve_exact(additional)
     }
 
+    /// Tries to reserve the minimum capacity for exactly `additional`
+    /// elements to be inserted in the given `OsString`. After calling
+    /// `try_reserve_exact`, capacity will be greater than or equal to
+    /// `self.len() + additional` if it returns `Ok(())`.
+    /// Does nothing if the capacity is already sufficient.
+    ///
+    /// Note that the allocator may give the collection more space than it
+    /// requests. Therefore, capacity can not be relied upon to be precisely
+    /// minimal. Prefer [`try_reserve`] if future insertions are expected.
+    ///
+    /// [`try_reserve`]: OsString::try_reserve
+    ///
+    /// # Errors
+    ///
+    /// If the capacity overflows, or the allocator reports a failure, then an error
+    /// is returned.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(try_reserve_2)]
+    /// use std::ffi::OsString;
+    /// use std::collections::TryReserveError;
+    ///
+    /// fn find_max_slow(data: &str) -> Result<OsString, TryReserveError> {
+    ///     let mut s = OsString::from(data);
+    ///
+    ///     // Pre-reserve the memory, exiting if we can't
+    ///     s.try_reserve_exact(data.len())?;
+    ///
+    ///     // Now we know this can't OOM in the middle of our complex work
+    ///     s.push(data);
+    ///
+    ///     Ok(s)
+    /// }
+    /// # find_max_slow("123").expect("why is the test harness OOMing on 12 bytes?");
+    /// ```
+    #[unstable(feature = "try_reserve_2", issue = "91789")]
+    #[inline]
+    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
+        self.inner.try_reserve_exact(additional)
+    }
+
     /// Shrinks the capacity of the `OsString` to match its length.
     ///
     /// # Examples
diff --git a/library/std/src/sys/unix/os_str.rs b/library/std/src/sys/unix/os_str.rs
index ae96d6b4df4..ccbc182240c 100644
--- a/library/std/src/sys/unix/os_str.rs
+++ b/library/std/src/sys/unix/os_str.rs
@@ -2,6 +2,7 @@
 //! systems: just a `Vec<u8>`/`[u8]`.
 
 use crate::borrow::Cow;
+use crate::collections::TryReserveError;
 use crate::fmt;
 use crate::fmt::Write;
 use crate::mem;
@@ -113,11 +114,21 @@ impl Buf {
     }
 
     #[inline]
+    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
+        self.inner.try_reserve(additional)
+    }
+
+    #[inline]
     pub fn reserve_exact(&mut self, additional: usize) {
         self.inner.reserve_exact(additional)
     }
 
     #[inline]
+    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
+        self.inner.try_reserve_exact(additional)
+    }
+
+    #[inline]
     pub fn shrink_to_fit(&mut self) {
         self.inner.shrink_to_fit()
     }