Skip to content

Commit b37e4e0

Browse files
authored
Rollup merge of #97202 - joshtriplett:os-str-capacity-documentation, r=dtolnay
os str capacity documentation This is based on #95394 , with expansion and consolidation to address comments from `@dtolnay` and other `@rust-lang/libs-api` team members.
2 parents ca98305 + 4558207 commit b37e4e0

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

library/std/src/ffi/os_str.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ use crate::sys_common::{AsInner, FromInner, IntoInner};
4545
/// values, encoded in a less-strict variant of UTF-8. This is useful to
4646
/// understand when handling capacity and length values.
4747
///
48+
/// # Capacity of `OsString`
49+
///
50+
/// Capacity uses units of UTF-8 bytes for OS strings which were created from valid unicode, and
51+
/// uses units of bytes in an unspecified encoding for other contents. On a given target, all
52+
/// `OsString` and `OsStr` values use the same units for capacity, so the following will work:
53+
/// ```
54+
/// use std::ffi::{OsStr, OsString};
55+
///
56+
/// fn concat_os_strings(a: &OsStr, b: &OsStr) -> OsString {
57+
/// let mut ret = OsString::with_capacity(a.len() + b.len()); // This will allocate
58+
/// ret.push(a); // This will not allocate further
59+
/// ret.push(b); // This will not allocate further
60+
/// ret
61+
/// }
62+
/// ```
63+
///
4864
/// # Creating an `OsString`
4965
///
5066
/// **From a Rust string**: `OsString` implements
@@ -186,7 +202,7 @@ impl OsString {
186202
/// OS strings without reallocating. If `capacity` is 0, the string will not
187203
/// allocate.
188204
///
189-
/// See main `OsString` documentation information about encoding.
205+
/// See the main `OsString` documentation information about encoding and capacity units.
190206
///
191207
/// # Examples
192208
///
@@ -229,7 +245,7 @@ impl OsString {
229245

230246
/// Returns the capacity this `OsString` can hold without reallocating.
231247
///
232-
/// See `OsString` introduction for information about encoding.
248+
/// See the main `OsString` documentation information about encoding and capacity units.
233249
///
234250
/// # Examples
235251
///
@@ -251,6 +267,8 @@ impl OsString {
251267
///
252268
/// The collection may reserve more space to avoid frequent reallocations.
253269
///
270+
/// See the main `OsString` documentation information about encoding and capacity units.
271+
///
254272
/// # Examples
255273
///
256274
/// ```
@@ -272,6 +290,8 @@ impl OsString {
272290
/// greater than or equal to `self.len() + additional`. Does nothing if
273291
/// capacity is already sufficient.
274292
///
293+
/// See the main `OsString` documentation information about encoding and capacity units.
294+
///
275295
/// # Errors
276296
///
277297
/// If the capacity overflows, or the allocator reports a failure, then an error
@@ -313,6 +333,8 @@ impl OsString {
313333
///
314334
/// [`reserve`]: OsString::reserve
315335
///
336+
/// See the main `OsString` documentation information about encoding and capacity units.
337+
///
316338
/// # Examples
317339
///
318340
/// ```
@@ -340,6 +362,8 @@ impl OsString {
340362
///
341363
/// [`try_reserve`]: OsString::try_reserve
342364
///
365+
/// See the main `OsString` documentation information about encoding and capacity units.
366+
///
343367
/// # Errors
344368
///
345369
/// If the capacity overflows, or the allocator reports a failure, then an error
@@ -373,6 +397,8 @@ impl OsString {
373397

374398
/// Shrinks the capacity of the `OsString` to match its length.
375399
///
400+
/// See the main `OsString` documentation information about encoding and capacity units.
401+
///
376402
/// # Examples
377403
///
378404
/// ```
@@ -399,6 +425,8 @@ impl OsString {
399425
///
400426
/// If the current capacity is less than the lower limit, this is a no-op.
401427
///
428+
/// See the main `OsString` documentation information about encoding and capacity units.
429+
///
402430
/// # Examples
403431
///
404432
/// ```
@@ -773,6 +801,8 @@ impl OsStr {
773801
/// This number is simply useful for passing to other methods, like
774802
/// [`OsString::with_capacity`] to avoid reallocations.
775803
///
804+
/// See the main `OsString` documentation information about encoding and capacity units.
805+
///
776806
/// # Examples
777807
///
778808
/// ```

0 commit comments

Comments
 (0)