Rollup merge of #97202 - joshtriplett:os-str-capacity-documentation, r=dtolnay

os str capacity documentation

This is based on https://github.com/rust-lang/rust/pull/95394 , with expansion and consolidation
to address comments from `@dtolnay` and other `@rust-lang/libs-api` team members.
This commit is contained in:
Yuki Okushi 2022-06-16 07:24:38 +09:00 committed by GitHub
commit b37e4e043e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,6 +45,22 @@
/// values, encoded in a less-strict variant of UTF-8. This is useful to /// values, encoded in a less-strict variant of UTF-8. This is useful to
/// understand when handling capacity and length values. /// understand when handling capacity and length values.
/// ///
/// # Capacity of `OsString`
///
/// Capacity uses units of UTF-8 bytes for OS strings which were created from valid unicode, and
/// uses units of bytes in an unspecified encoding for other contents. On a given target, all
/// `OsString` and `OsStr` values use the same units for capacity, so the following will work:
/// ```
/// use std::ffi::{OsStr, OsString};
///
/// fn concat_os_strings(a: &OsStr, b: &OsStr) -> OsString {
/// let mut ret = OsString::with_capacity(a.len() + b.len()); // This will allocate
/// ret.push(a); // This will not allocate further
/// ret.push(b); // This will not allocate further
/// ret
/// }
/// ```
///
/// # Creating an `OsString` /// # Creating an `OsString`
/// ///
/// **From a Rust string**: `OsString` implements /// **From a Rust string**: `OsString` implements
@ -186,7 +202,7 @@ pub fn push<T: AsRef<OsStr>>(&mut self, s: T) {
/// OS strings without reallocating. If `capacity` is 0, the string will not /// OS strings without reallocating. If `capacity` is 0, the string will not
/// allocate. /// allocate.
/// ///
/// See main `OsString` documentation information about encoding. /// See the main `OsString` documentation information about encoding and capacity units.
/// ///
/// # Examples /// # Examples
/// ///
@ -229,7 +245,7 @@ pub fn clear(&mut self) {
/// Returns the capacity this `OsString` can hold without reallocating. /// Returns the capacity this `OsString` can hold without reallocating.
/// ///
/// See `OsString` introduction for information about encoding. /// See the main `OsString` documentation information about encoding and capacity units.
/// ///
/// # Examples /// # Examples
/// ///
@ -251,6 +267,8 @@ pub fn capacity(&self) -> usize {
/// ///
/// The collection may reserve more space to avoid frequent reallocations. /// The collection may reserve more space to avoid frequent reallocations.
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -272,6 +290,8 @@ pub fn reserve(&mut self, additional: usize) {
/// greater than or equal to `self.len() + additional`. Does nothing if /// greater than or equal to `self.len() + additional`. Does nothing if
/// capacity is already sufficient. /// capacity is already sufficient.
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Errors /// # Errors
/// ///
/// If the capacity overflows, or the allocator reports a failure, then an error /// If the capacity overflows, or the allocator reports a failure, then an error
@ -313,6 +333,8 @@ pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
/// ///
/// [`reserve`]: OsString::reserve /// [`reserve`]: OsString::reserve
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -340,6 +362,8 @@ pub fn reserve_exact(&mut self, additional: usize) {
/// ///
/// [`try_reserve`]: OsString::try_reserve /// [`try_reserve`]: OsString::try_reserve
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Errors /// # Errors
/// ///
/// If the capacity overflows, or the allocator reports a failure, then an error /// If the capacity overflows, or the allocator reports a failure, then an error
@ -373,6 +397,8 @@ pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveE
/// Shrinks the capacity of the `OsString` to match its length. /// Shrinks the capacity of the `OsString` to match its length.
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -399,6 +425,8 @@ pub fn shrink_to_fit(&mut self) {
/// ///
/// If the current capacity is less than the lower limit, this is a no-op. /// If the current capacity is less than the lower limit, this is a no-op.
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@ -773,6 +801,8 @@ pub fn is_empty(&self) -> bool {
/// This number is simply useful for passing to other methods, like /// This number is simply useful for passing to other methods, like
/// [`OsString::with_capacity`] to avoid reallocations. /// [`OsString::with_capacity`] to avoid reallocations.
/// ///
/// See the main `OsString` documentation information about encoding and capacity units.
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```