From c40ac57efb88b308b869be2ec47da59aba3c842c Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Tue, 28 Dec 2021 11:28:05 +0800 Subject: [PATCH] Add try_reserve for OsString Signed-off-by: Xuanwo --- library/std/src/ffi/os_str.rs | 81 ++++++++++++++++++++++++++++++ library/std/src/sys/unix/os_str.rs | 11 ++++ 2 files changed, 92 insertions(+) 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 { + /// 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 { + /// 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]`. use crate::borrow::Cow; +use crate::collections::TryReserveError; use crate::fmt; use crate::fmt::Write; use crate::mem; @@ -112,11 +113,21 @@ impl Buf { self.inner.reserve(additional) } + #[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()