Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try_reserve and try_reserve_exact for OsString #92338

Merged
merged 7 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -265,6 +266,43 @@ impl OsString {
self.inner.reserve(additional)
}

/// Tries to reserve capacity for at least `additional` more elements to be inserted
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
/// in the given `OsString`. The collection may reserve more space to avoid
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
/// 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);
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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.
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions library/std/src/sys/unix/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions library/std/src/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// The underlying OsString/OsStr implementation on Windows is a
/// wrapper around the "WTF-8" encoding; see the `wtf8` module for more.
use crate::borrow::Cow;
use crate::collections::TryReserveError;
use crate::fmt;
use crate::mem;
use crate::rc::Rc;
Expand Down Expand Up @@ -104,10 +105,18 @@ impl Buf {
self.inner.reserve(additional)
}

pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve(additional)
}

pub fn reserve_exact(&mut self, additional: usize) {
self.inner.reserve_exact(additional)
}

pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.inner.try_reserve_exact(additional)
}

pub fn shrink_to_fit(&mut self) {
self.inner.shrink_to_fit()
}
Expand Down
37 changes: 37 additions & 0 deletions library/std/src/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use core::str::next_code_point;

use crate::borrow::Cow;
use crate::char;
use crate::collections::TryReserveError;
use crate::fmt;
use crate::hash::{Hash, Hasher};
use crate::iter::FromIterator;
Expand Down Expand Up @@ -231,11 +232,47 @@ impl Wtf8Buf {
self.bytes.reserve(additional)
}

/// Tries to reserve capacity for at least `additional` more elements to be inserted
/// in the given `Wtf8Buf`. 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.
#[inline]
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.bytes.try_reserve(additional)
}

#[inline]
pub fn reserve_exact(&mut self, additional: usize) {
self.bytes.reserve_exact(additional)
}

/// Tries to reserve the minimum capacity for exactly `additional`
/// elements to be inserted in the given `Wtf8Buf`. 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`]: Wtf8Buf::try_reserve
///
/// # Errors
///
/// If the capacity overflows, or the allocator reports a failure, then an error
/// is returned.
#[inline]
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
self.bytes.try_reserve_exact(additional)
}

#[inline]
pub fn shrink_to_fit(&mut self) {
self.bytes.shrink_to_fit()
Expand Down