Skip to content

Commit ddc5d7b

Browse files
committed
Auto merge of #41424 - frewsxcv:rollup, r=frewsxcv
Rollup of 2 pull requests - Successful merges: #40812, #40987 - Failed merges:
2 parents 968ae7b + 9d91822 commit ddc5d7b

File tree

2 files changed

+142
-61
lines changed

2 files changed

+142
-61
lines changed

src/libcore/convert.rs

+133-50
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@
1717
//! Like many traits, these are often used as bounds for generic functions, to
1818
//! support arguments of multiple types.
1919
//!
20-
//! - Impl the `As*` traits for reference-to-reference conversions
21-
//! - Impl the [`Into`] trait when you want to consume the value in the conversion
20+
//! - Implement the `As*` traits for reference-to-reference conversions
21+
//! - Implement the [`Into`] trait when you want to consume the value in the conversion
2222
//! - The [`From`] trait is the most flexible, useful for value _and_ reference conversions
2323
//! - The [`TryFrom`] and [`TryInto`] traits behave like [`From`] and [`Into`], but allow for the
2424
//! conversion to fail
2525
//!
2626
//! As a library author, you should prefer implementing [`From<T>`][`From`] or
2727
//! [`TryFrom<T>`][`TryFrom`] rather than [`Into<U>`][`Into`] or [`TryInto<U>`][`TryInto`],
2828
//! as [`From`] and [`TryFrom`] provide greater flexibility and offer
29-
//! equivalent [`Into`] or [`TryInto`] implementations for free, thanks to a blanket implementation
30-
//! in the standard library.
29+
//! equivalent [`Into`] or [`TryInto`] implementations for free, thanks to a
30+
//! blanket implementation in the standard library.
3131
//!
32-
//! # Generic impl
32+
//! # Generic Implementations
3333
//!
3434
//! - [`AsRef`] and [`AsMut`] auto-dereference if the inner type is a reference
3535
//! - [`From`]`<U> for T` implies [`Into`]`<T> for U`
3636
//! - [`TryFrom`]`<U> for T` implies [`TryInto`]`<T> for U`
37-
//! - [`From`] and [`Into`] are reflexive, which means that all types can `into()`
38-
//! themselves and `from()` themselves
37+
//! - [`From`] and [`Into`] are reflexive, which means that all types can
38+
//! `into` themselves and `from` themselves
3939
//!
4040
//! See each trait for usage examples.
4141
//!
@@ -50,20 +50,42 @@
5050

5151
use str::FromStr;
5252

53-
/// A cheap, reference-to-reference conversion.
53+
/// A cheap reference-to-reference conversion. Used to convert a value to a
54+
/// reference value within generic code.
5455
///
55-
/// `AsRef` is very similar to, but different than, [`Borrow`]. See
56-
/// [the book][book] for more.
56+
/// `AsRef` is very similar to, but serves a slightly different purpose than,
57+
/// [`Borrow`].
58+
///
59+
/// `AsRef` is to be used when wishing to convert to a reference of another
60+
/// type.
61+
/// `Borrow` is more related to the notion of taking the reference. It is
62+
/// useful when wishing to abstract over the type of reference
63+
/// (`&T`, `&mut T`) or allow both the referenced and owned type to be treated
64+
/// in the same manner.
65+
///
66+
/// The key difference between the two traits is the intention:
67+
///
68+
/// - Use `AsRef` when goal is to simply convert into a reference
69+
/// - Use `Borrow` when goal is related to writing code that is agnostic to the
70+
/// type of borrow and if is reference or value
71+
///
72+
/// See [the book][book] for a more detailed comparison.
5773
///
5874
/// [book]: ../../book/borrow-and-asref.html
5975
/// [`Borrow`]: ../../std/borrow/trait.Borrow.html
6076
///
61-
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
62-
/// returns an [`Option<T>`] or a [`Result<T, E>`].
77+
/// **Note: this trait must not fail**. If the conversion can fail, use a
78+
/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`].
6379
///
6480
/// [`Option<T>`]: ../../std/option/enum.Option.html
6581
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
6682
///
83+
/// # Generic Implementations
84+
///
85+
/// - `AsRef` auto-dereferences if the inner type is a reference or a mutable
86+
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
87+
/// `&mut Foo` or `&&mut Foo`)
88+
///
6789
/// # Examples
6890
///
6991
/// Both [`String`] and `&str` implement `AsRef<str>`:
@@ -82,11 +104,6 @@ use str::FromStr;
82104
/// is_hello(s);
83105
/// ```
84106
///
85-
/// # Generic Impls
86-
///
87-
/// - `AsRef` auto-dereferences if the inner type is a reference or a mutable
88-
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
89-
///
90107
#[stable(feature = "rust1", since = "1.0.0")]
91108
pub trait AsRef<T: ?Sized> {
92109
/// Performs the conversion.
@@ -96,12 +113,21 @@ pub trait AsRef<T: ?Sized> {
96113

97114
/// A cheap, mutable reference-to-mutable reference conversion.
98115
///
99-
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which
100-
/// returns an [`Option<T>`] or a [`Result<T, E>`].
116+
/// This trait is similar to `AsRef` but used for converting between mutable
117+
/// references.
118+
///
119+
/// **Note: this trait must not fail**. If the conversion can fail, use a
120+
/// dedicated method which returns an [`Option<T>`] or a [`Result<T, E>`].
101121
///
102122
/// [`Option<T>`]: ../../std/option/enum.Option.html
103123
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
104124
///
125+
/// # Generic Implementations
126+
///
127+
/// - `AsMut` auto-dereferences if the inner type is a reference or a mutable
128+
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type
129+
/// `&mut Foo` or `&&mut Foo`)
130+
///
105131
/// # Examples
106132
///
107133
/// [`Box<T>`] implements `AsMut<T>`:
@@ -118,10 +144,6 @@ pub trait AsRef<T: ?Sized> {
118144
/// assert_eq!(*boxed_num, 1);
119145
/// ```
120146
///
121-
/// # Generic Impls
122-
///
123-
/// - `AsMut` auto-dereferences if the inner type is a reference or a mutable
124-
/// reference (e.g.: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`)
125147
///
126148
#[stable(feature = "rust1", since = "1.0.0")]
127149
pub trait AsMut<T: ?Sized> {
@@ -130,14 +152,22 @@ pub trait AsMut<T: ?Sized> {
130152
fn as_mut(&mut self) -> &mut T;
131153
}
132154

133-
/// A conversion that consumes `self`, which may or may not be expensive.
155+
/// A conversion that consumes `self`, which may or may not be expensive. The
156+
/// reciprocal of [`From`][From].
157+
///
158+
/// **Note: this trait must not fail**. If the conversion can fail, use
159+
/// [`TryInto`] or a dedicated method which returns an [`Option<T>`] or a
160+
/// [`Result<T, E>`].
134161
///
135-
/// **Note: this trait must not fail**. If the conversion can fail, use [`TryInto`] or a dedicated
136-
/// method which returns an [`Option<T>`] or a [`Result<T, E>`].
162+
/// Library authors should not directly implement this trait, but should prefer
163+
/// implementing the [`From`][From] trait, which offers greater flexibility and
164+
/// provides an equivalent `Into` implementation for free, thanks to a blanket
165+
/// implementation in the standard library.
137166
///
138-
/// Library authors should not directly implement this trait, but should prefer implementing
139-
/// the [`From`][From] trait, which offers greater flexibility and provides an equivalent `Into`
140-
/// implementation for free, thanks to a blanket implementation in the standard library.
167+
/// # Generic Implementations
168+
///
169+
/// - [`From<T>`][From]` for U` implies `Into<U> for T`
170+
/// - [`into`] is reflexive, which means that `Into<T> for T` is implemented
141171
///
142172
/// # Examples
143173
///
@@ -153,11 +183,6 @@ pub trait AsMut<T: ?Sized> {
153183
/// is_hello(s);
154184
/// ```
155185
///
156-
/// # Generic Impls
157-
///
158-
/// - [`From<T>`][From]` for U` implies `Into<U> for T`
159-
/// - [`into`] is reflexive, which means that `Into<T> for T` is implemented
160-
///
161186
/// [`TryInto`]: trait.TryInto.html
162187
/// [`Option<T>`]: ../../std/option/enum.Option.html
163188
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
@@ -171,10 +196,31 @@ pub trait Into<T>: Sized {
171196
fn into(self) -> T;
172197
}
173198

174-
/// Construct `Self` via a conversion.
199+
/// Simple and safe type conversions in to `Self`. It is the reciprocal of
200+
/// `Into`.
201+
///
202+
/// This trait is useful when performing error handling as described by
203+
/// [the book][book] and is closely related to the `?` operator.
204+
///
205+
/// When constructing a function that is capable of failing the return type
206+
/// will generally be of the form `Result<T, E>`.
207+
///
208+
/// The `From` trait allows for simplification of error handling by providing a
209+
/// means of returning a single error type that encapsulates numerous possible
210+
/// erroneous situations.
211+
///
212+
/// This trait is not limited to error handling, rather the general case for
213+
/// this trait would be in any type conversions to have an explicit definition
214+
/// of how they are performed.
215+
///
216+
/// **Note: this trait must not fail**. If the conversion can fail, use
217+
/// [`TryFrom`] or a dedicated method which returns an [`Option<T>`] or a
218+
/// [`Result<T, E>`].
175219
///
176-
/// **Note: this trait must not fail**. If the conversion can fail, use [`TryFrom`] or a dedicated
177-
/// method which returns an [`Option<T>`] or a [`Result<T, E>`].
220+
/// # Generic Implementations
221+
///
222+
/// - `From<T> for U` implies [`Into<U>`]` for T`
223+
/// - [`from`] is reflexive, which means that `From<T> for T` is implemented
178224
///
179225
/// # Examples
180226
///
@@ -186,29 +232,60 @@ pub trait Into<T>: Sized {
186232
///
187233
/// assert_eq!(string, other_string);
188234
/// ```
189-
/// # Generic impls
190235
///
191-
/// - `From<T> for U` implies [`Into<U>`]` for T`
192-
/// - [`from`] is reflexive, which means that `From<T> for T` is implemented
236+
/// An example usage for error handling:
237+
///
238+
/// ```
239+
/// use std::io::{self, Read};
240+
/// use std::num;
241+
///
242+
/// enum CliError {
243+
/// IoError(io::Error),
244+
/// ParseError(num::ParseIntError),
245+
/// }
246+
///
247+
/// impl From<io::Error> for CliError {
248+
/// fn from(error: io::Error) -> Self {
249+
/// CliError::IoError(error)
250+
/// }
251+
/// }
252+
///
253+
/// impl From<num::ParseIntError> for CliError {
254+
/// fn from(error: num::ParseIntError) -> Self {
255+
/// CliError::ParseError(error)
256+
/// }
257+
/// }
258+
///
259+
/// fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
260+
/// let mut file = std::fs::File::open("test")?;
261+
/// let mut contents = String::new();
262+
/// file.read_to_string(&mut contents)?;
263+
/// let num: i32 = contents.trim().parse()?;
264+
/// Ok(num)
265+
/// }
266+
/// ```
193267
///
194268
/// [`TryFrom`]: trait.TryFrom.html
195269
/// [`Option<T>`]: ../../std/option/enum.Option.html
196270
/// [`Result<T, E>`]: ../../std/result/enum.Result.html
197271
/// [`String`]: ../../std/string/struct.String.html
198272
/// [`Into<U>`]: trait.Into.html
199273
/// [`from`]: trait.From.html#tymethod.from
274+
/// [book]: ../../book/error-handling.html
200275
#[stable(feature = "rust1", since = "1.0.0")]
201276
pub trait From<T>: Sized {
202277
/// Performs the conversion.
203278
#[stable(feature = "rust1", since = "1.0.0")]
204279
fn from(T) -> Self;
205280
}
206281

207-
/// An attempted conversion that consumes `self`, which may or may not be expensive.
282+
/// An attempted conversion that consumes `self`, which may or may not be
283+
/// expensive.
208284
///
209-
/// Library authors should not directly implement this trait, but should prefer implementing
210-
/// the [`TryFrom`] trait, which offers greater flexibility and provides an equivalent `TryInto`
211-
/// implementation for free, thanks to a blanket implementation in the standard library.
285+
/// Library authors should not directly implement this trait, but should prefer
286+
/// implementing the [`TryFrom`] trait, which offers greater flexibility and
287+
/// provides an equivalent `TryInto` implementation for free, thanks to a
288+
/// blanket implementation in the standard library.
212289
///
213290
/// [`TryFrom`]: trait.TryFrom.html
214291
#[unstable(feature = "try_from", issue = "33417")]
@@ -236,15 +313,17 @@ pub trait TryFrom<T>: Sized {
236313

237314
// As lifts over &
238315
#[stable(feature = "rust1", since = "1.0.0")]
239-
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U> {
316+
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a T where T: AsRef<U>
317+
{
240318
fn as_ref(&self) -> &U {
241319
<T as AsRef<U>>::as_ref(*self)
242320
}
243321
}
244322

245323
// As lifts over &mut
246324
#[stable(feature = "rust1", since = "1.0.0")]
247-
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
325+
impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U>
326+
{
248327
fn as_ref(&self) -> &U {
249328
<T as AsRef<U>>::as_ref(*self)
250329
}
@@ -260,7 +339,8 @@ impl<'a, T: ?Sized, U: ?Sized> AsRef<U> for &'a mut T where T: AsRef<U> {
260339

261340
// AsMut lifts over &mut
262341
#[stable(feature = "rust1", since = "1.0.0")]
263-
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
342+
impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U>
343+
{
264344
fn as_mut(&mut self) -> &mut U {
265345
(*self).as_mut()
266346
}
@@ -276,7 +356,8 @@ impl<'a, T: ?Sized, U: ?Sized> AsMut<U> for &'a mut T where T: AsMut<U> {
276356

277357
// From implies Into
278358
#[stable(feature = "rust1", since = "1.0.0")]
279-
impl<T, U> Into<U> for T where U: From<T> {
359+
impl<T, U> Into<U> for T where U: From<T>
360+
{
280361
fn into(self) -> U {
281362
U::from(self)
282363
}
@@ -291,7 +372,8 @@ impl<T> From<T> for T {
291372

292373
// TryFrom implies TryInto
293374
#[unstable(feature = "try_from", issue = "33417")]
294-
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
375+
impl<T, U> TryInto<U> for T where U: TryFrom<T>
376+
{
295377
type Error = U::Error;
296378

297379
fn try_into(self) -> Result<U, U::Error> {
@@ -327,7 +409,8 @@ impl AsRef<str> for str {
327409

328410
// FromStr implies TryFrom<&str>
329411
#[unstable(feature = "try_from", issue = "33417")]
330-
impl<'a, T> TryFrom<&'a str> for T where T: FromStr {
412+
impl<'a, T> TryFrom<&'a str> for T where T: FromStr
413+
{
331414
type Error = <T as FromStr>::Err;
332415

333416
fn try_from(s: &'a str) -> Result<T, Self::Error> {

src/libstd/process.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
7373
/// spawning process and can itself be constructed using a builder-style
7474
/// interface.
7575
///
76+
/// There is no implementation of [`Drop`] for child processes,
77+
/// so if you do not ensure the `Child` has exited then it will continue to
78+
/// run, even after the `Child` handle to the child process has gone out of
79+
/// scope.
80+
///
81+
/// Calling [`wait`](#method.wait) (or other functions that wrap around it) will make
82+
/// the parent process wait until the child has actually exited before
83+
/// continuing.
84+
///
7685
/// # Examples
7786
///
7887
/// ```should_panic
@@ -89,17 +98,6 @@ use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
8998
/// assert!(ecode.success());
9099
/// ```
91100
///
92-
/// # Note
93-
///
94-
/// Take note that there is no implementation of [`Drop`] for child processes,
95-
/// so if you do not ensure the `Child` has exited then it will continue to
96-
/// run, even after the `Child` handle to the child process has gone out of
97-
/// scope.
98-
///
99-
/// Calling [`wait`][`wait`] (or other functions that wrap around it) will make
100-
/// the parent process wait until the child has actually exited before
101-
/// continuing.
102-
///
103101
/// [`Command`]: struct.Command.html
104102
/// [`Drop`]: ../../core/ops/trait.Drop.html
105103
/// [`wait`]: #method.wait

0 commit comments

Comments
 (0)