Add try_reserve for OsString

Signed-off-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
Xuanwo 2021-12-28 11:28:05 +08:00
parent 4ee34f3551
commit c40ac57efb
No known key found for this signature in database
GPG Key ID: C423B4FA6B48E945
2 changed files with 92 additions and 0 deletions

View File

@ -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

View File

@ -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;
@ -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()