Skip to content

Commit 91f6445

Browse files
Overhaul the documentation for OsString / OsStr
1 parent 155b4b1 commit 91f6445

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

src/libstd/ffi/mod.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,40 @@
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 string slice in a format that
96-
//! can be passed to the operating system. It can be converted into
97-
//! an UTF-8 Rust string slice in a similar way to `OsString`.
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 an UTF-8 Rust string slice in a similar way to
98+
//! `OsString`.
99+
//!
100+
//! # Conversions
101+
//!
102+
//! ## On Unix
103+
//!
104+
//! On Unix, [`OsStr`] implements the `std::os::unix:ffi::`[`OsStrExt`][unix.OsStrExt] trait, which
105+
//! augments it with two methods, [`from_bytes`] and [`as_bytes`]. These do inexpensive conversions
106+
//! from and to UTF-8 byte slices.
107+
//!
108+
//! Additionally, on Unix [`OsString`] implements the
109+
//! `std::os::unix:ffi::`[`OsStringExt`][unix.OsStringExt] trait,
110+
//! which provides [`from_vec`] and [`into_vec`] methods that consume
111+
//! their arguments, and take or produce vectors of [`u8`].
112+
//!
113+
//! ## On Windows
114+
//!
115+
//! On Windows, [`OsStr`] implements the `std::os::windows::ffi::`[`OsStrExt`][windows.OsStrExt]
116+
//! trait, which provides an [`encode_wide`] method. This provides an iterator that can be
117+
//! [`collect`]ed into a vector of [`u16`].
118+
//!
119+
//! Additionally, on Windows [`OsString`] implements the
120+
//! `std::os::windows:ffi::`[`OsStringExt`][windows.OsStringExt] trait, which provides a
121+
//! [`from_wide`] method. The result of this method is an `OsString` which can be round-tripped to
122+
//! a Windows string losslessly.
98123
//!
99124
//! [`String`]: ../string/struct.String.html
100125
//! [`str`]: ../primitive.str.html
101126
//! [`char`]: ../primitive.char.html
127+
//! [`u8`]: ../primitive.u8.html
128+
//! [`u16`]: ../primitive.u16.html
102129
//! [Unicode scalar value]: http://www.unicode.org/glossary/#unicode_scalar_value
103130
//! [Unicode code point]: http://www.unicode.org/glossary/#code_point
104131
//! [`CString`]: struct.CString.html
@@ -108,6 +135,18 @@
108135
//! [`env::set_var()`]: ../env/fn.set_var.html
109136
//! [`env::var_os()`]: ../env/fn.var_os.html
110137
//! [`Result<>`]: ../result/enum.Result.html
138+
//! [unix.OsStringExt]: ../os/unix/ffi/trait.OsStringExt.html
139+
//! [`from_vec`]: ../os/unix/ffi/trait.OsStringExt.html#tymethod.from_vec
140+
//! [`into_vec`]: ../os/unix/ffi/trait.OsStringExt.html#tymethod.into_vec
141+
//! [unix.OsStrExt]: ../os/unix/ffi/trait.OsStrExt.html
142+
//! [`from_bytes`]: ../os/unix/ffi/trait.OsStrExt.html#tymethod.from_bytes
143+
//! [`as_bytes`]: ../os/unix/ffi/trait.OsStrExt.html#tymethod.as_bytes
144+
//! [`OsStrExt`]: ../os/unix/ffi/trait.OsStrExt.html
145+
//! [windows.OsStrExt]: ../os/windows/ffi/trait.OsStrExt.html
146+
//! [`encode_wide`]: ../os/windows/ffi/trait.OsStrExt.html#tymethod.encode_wide
147+
//! [`collect`]: ../iter/trait.Iterator.html#method.collect
148+
//! [windows.OsStringExt]: ../os/windows/ffi/trait.OsStringExt.html
149+
//! [`from_wide`]: ../os/windows/ffi/trait.OsStringExt.html#tymethod.from_wide
111150
112151
#![stable(feature = "rust1", since = "1.0.0")]
113152

src/libstd/ffi/os_str.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,65 @@ use sys_common::{AsInner, IntoInner, FromInner};
3333
///
3434
/// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust
3535
/// and platform-native string values, and in particular allowing a Rust string
36-
/// to be converted into an "OS" string with no cost.
36+
/// to be converted into an "OS" string with no cost if possible.
37+
///
38+
/// `OsString` is to [`OsStr`] as [`String`] is to [`&str`]: the former
39+
/// in each pair are owned strings; the latter are borrowed
40+
/// references.
41+
///
42+
/// # Creating an `OsString`
43+
///
44+
/// **From a Rust string**: `OsString` implements
45+
/// [`From`]`<`[`String`]`>`, so you can use `my_string.`[`from`] to
46+
/// create an `OsString` from a normal Rust string.
47+
///
48+
/// **From slices:** Just like you can start with an empty Rust
49+
/// [`String`] and then [`push_str`][String.push_str] `&str`
50+
/// sub-string slices into it, you can create an empty `OsString` with
51+
/// the [`new`] method and then push string slices into it with the
52+
/// [`push`] method.
53+
///
54+
/// # Extracting a borrowed reference to the whole OS string
55+
///
56+
/// You can use the [`as_os_str`] method to get an `&`[`OsStr`] from
57+
/// an `OsString`; this is effectively a borrowed reference to the
58+
/// whole string.
59+
///
60+
/// # Conversions
61+
///
62+
/// See the [module's toplevel documentation about conversions][conversions] for a discussion on the traits which
63+
/// `OsString` implements for conversions from/to native representations.
3764
///
3865
/// [`OsStr`]: struct.OsStr.html
66+
/// [`From`]: ../convert/trait.From.html
67+
/// [`from`]: ../convert/trait.From.html#tymethod.from
68+
/// [`String`]: ../string/struct.String.html
69+
/// [`&str`]: ../primitive.str.html
70+
/// [`u8`]: ../primitive.u8.html
71+
/// [`u16`]: ../primitive.u16.html
72+
/// [String.push_str]: ../string/struct.String.html#method.push_str
73+
/// [`new`]: #struct.OsString.html#method.new
74+
/// [`push`]: #struct.OsString.html#method.push
75+
/// [`as_os_str`]: #struct.OsString.html#method.as_os_str
3976
#[derive(Clone)]
4077
#[stable(feature = "rust1", since = "1.0.0")]
4178
pub struct OsString {
4279
inner: Buf
4380
}
4481

45-
/// Slices into OS strings (see [`OsString`]).
82+
/// Borrowed reference to an OS string (see [`OsString`]).
83+
///
84+
/// This type represents a borrowed reference to a string in the operating system's preferred
85+
/// representation.
86+
///
87+
/// `OsStr` is to [`OsString`] as [`String`] is to [`&str`]: the former in each pair are borrowed
88+
/// references; the latter are owned strings.
89+
///
90+
/// See the [module's toplevel documentation about conversions][conversions] for a discussion on the traits which
91+
/// `OsStr` implements for conversions from/to native representations.
4692
///
4793
/// [`OsString`]: struct.OsString.html
94+
/// [conversions]: index.html#conversions
4895
#[stable(feature = "rust1", since = "1.0.0")]
4996
pub struct OsStr {
5097
inner: Slice

0 commit comments

Comments
 (0)