Skip to content

Commit bb8a4ab

Browse files
authored
Rollup merge of #91467 - ChrisDenton:confusing-os-string, r=Mark-Simulacrum
Emphasise that an OsStr[ing] is not necessarily a platform string Fixes #53261 Since that issue was filed, #56141 added a further clarification to the `OsString` docs. However the ffi docs may still leave the impression that an `OsStr` is in the platform native form. This PR aims to further emphasise that an `OsStr` is not necessarily a platform string.
2 parents 871cf2b + 49aa5ba commit bb8a4ab

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

library/std/src/ffi/mod.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@
8181
//! [`OsStr`] and Rust strings work similarly to those for [`CString`]
8282
//! and [`CStr`].
8383
//!
84-
//! * [`OsString`] represents an owned string in whatever
85-
//! representation the operating system prefers. In the Rust standard
86-
//! library, various APIs that transfer strings to/from the operating
84+
//! * [`OsString`] losslessly represents an owned platform string. However, this
85+
//! representation is not necessarily in a form native to the platform.
86+
//! In the Rust standard library, various APIs that transfer strings to/from the operating
8787
//! system use [`OsString`] instead of plain strings. For example,
8888
//! [`env::var_os()`] is used to query environment variables; it
8989
//! returns an <code>[Option]<[OsString]></code>. If the environment variable
@@ -92,9 +92,9 @@
9292
//! your code can detect errors in case the environment variable did
9393
//! not in fact contain valid Unicode data.
9494
//!
95-
//! * [`OsStr`] represents a borrowed reference to a string in a
96-
//! format that can be passed to the operating system. It can be
97-
//! converted into a UTF-8 Rust string slice in a similar way to
95+
//! * [`OsStr`] losslessly represents a borrowed reference to a platform string.
96+
//! However, this representation is not necessarily in a form native to the platform.
97+
//! It can be converted into a UTF-8 Rust string slice in a similar way to
9898
//! [`OsString`].
9999
//!
100100
//! # Conversions
@@ -113,16 +113,19 @@
113113
//!
114114
//! ## On Windows
115115
//!
116+
//! An [`OsStr`] can be losslessly converted to a native Windows string. And
117+
//! a native Windows string can be losslessly converted to an [`OsString`].
118+
//!
116119
//! On Windows, [`OsStr`] implements the
117120
//! <code>std::os::windows::ffi::[OsStrExt][windows.OsStrExt]</code> trait,
118121
//! which provides an [`encode_wide`] method. This provides an
119-
//! iterator that can be [`collect`]ed into a vector of [`u16`].
122+
//! iterator that can be [`collect`]ed into a vector of [`u16`]. After a nul
123+
//! characters is appended, this is the same as a native Windows string.
120124
//!
121125
//! Additionally, on Windows [`OsString`] implements the
122126
//! <code>std::os::windows:ffi::[OsStringExt][windows.OsStringExt]</code>
123-
//! trait, which provides a [`from_wide`] method. The result of this
124-
//! method is an [`OsString`] which can be round-tripped to a Windows
125-
//! string losslessly.
127+
//! trait, which provides a [`from_wide`] method to convert a native Windows
128+
//! string (without the terminating nul character) to an [`OsString`].
126129
//!
127130
//! [Unicode scalar value]: https://www.unicode.org/glossary/#unicode_scalar_value
128131
//! [Unicode code point]: https://www.unicode.org/glossary/#code_point

0 commit comments

Comments
 (0)