Skip to content

Commit 6c479c3

Browse files
author
Christian
committed
Formatting changes, including better wrapping and creating short summary lines.
1 parent 70ce4b1 commit 6c479c3

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/libcore/convert.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
//! Traits for conversions between types.
12
//!
23
//! The traits in this module provide a way to convert from one type to another type.
3-
//!
44
//! Each trait serves a different purpose:
55
//!
66
//! - Implement the [`AsRef`] trait for cheap reference-to-reference conversions
@@ -99,12 +99,14 @@ use fmt;
9999
pub const fn identity<T>(x: T) -> T { x }
100100

101101
/// Used to do a cheap reference-to-reference conversion.
102+
///
102103
/// This trait is similar to [`AsMut`] which is used for converting between mutable references.
103104
/// If you need to do a costly conversion it is better to implement [`From`] with type
104105
/// `&T` or write a custom function.
105106
///
106107
///
107108
/// `AsRef` is very similar to, but serves a slightly different purpose than [`Borrow`]:
109+
///
108110
/// - Use `AsRef` when the goal is to simply convert into a reference
109111
/// - Use `Borrow` when the goal is related to writing code that is agnostic to
110112
/// the type of borrow and whether it is a reference or value
@@ -127,6 +129,7 @@ pub const fn identity<T>(x: T) -> T { x }
127129
///
128130
/// By using trait bounds we can accept arguments of different types as long as they can be
129131
/// converted a the specified type `T`.
132+
///
130133
/// For example: By creating a generic function that takes an `AsRef<str>` we express that we
131134
/// want to accept all references that can be converted to &str as an argument.
132135
/// Since both [`String`] and `&str` implement `AsRef<str>` we can accept both as input argument.
@@ -153,6 +156,7 @@ pub trait AsRef<T: ?Sized> {
153156
}
154157

155158
/// Used to do a cheap mutable-to-mutable reference conversion.
159+
///
156160
/// This trait is similar to [`AsRef`] but used for converting between mutable
157161
/// references. If you need to do a costly conversion it is better to
158162
/// implement [`From`] with type `&mut T` or write a custom function.
@@ -199,9 +203,9 @@ pub trait AsMut<T: ?Sized> {
199203
///
200204
/// One should only implement [`Into`] if a conversion to a type outside the current crate is
201205
/// required. Otherwise one should always prefer implementing [`From`] over [`Into`] because
202-
/// implementing [`From`] automatically provides one with a implementation of [`Into`]
203-
/// thanks to the blanket implementation in the standard library.
204-
/// [`From`] cannot do these type of conversions because of Rust's orphaning rules.
206+
/// implementing [`From`] automatically provides one with a implementation of [`Into`] thanks to
207+
/// the blanket implementation in the standard library. [`From`] cannot do these type of
208+
/// conversions because of Rust's orphaning rules.
205209
///
206210
/// **Note: This trait must not fail**. If the conversion can fail, use [`TryInto`].
207211
///
@@ -211,6 +215,7 @@ pub trait AsMut<T: ?Sized> {
211215
/// - [`Into`]` is reflexive, which means that `Into<T> for T` is implemented
212216
///
213217
/// # Implementing `Into` for conversions to external types
218+
///
214219
/// If the destination type is not part of the current crate
215220
/// then you can't implement [`From`] directly.
216221
/// For example, take this code:
@@ -239,6 +244,7 @@ pub trait AsMut<T: ?Sized> {
239244
/// It is important to understand that `Into` does not provide a [`From`] implementation
240245
/// (as [`From`] does with `Into`). Therefore, you should always try to implement [`From`]
241246
/// and then fall back to `Into` if [`From`] can't be implemented.
247+
///
242248
/// Prefer using `Into` over [`From`] when specifying trait bounds on a generic function
243249
/// to ensure that types that only implement `Into` can be used as well.
244250
///
@@ -280,19 +286,19 @@ pub trait Into<T>: Sized {
280286
/// One should always prefer implementing [`From`] over [`Into`]
281287
/// because implementing [`From`] automatically provides one with a implementation of [`Into`]
282288
/// thanks to the blanket implementation in the standard library.
289+
///
283290
/// Only implement [`Into`] if a conversion to a type outside the current crate is required.
284291
/// [`From`] cannot do these type of conversions because of Rust's orphaning rules.
285292
/// See [`Into`] for more details.
286293
///
287294
/// Prefer using [`Into`] over using [`From`] when specifying trait bounds on a generic function.
288295
/// This way, types that directly implement [`Into`] can be used as arguments as well.
289296
///
290-
/// The [`From`] is also very useful when performing error handling.
291-
/// When constructing a function that is capable of failing, the return type
292-
/// will generally be of the form `Result<T, E>`.
297+
/// The [`From`] is also very useful when performing error handling. When constructing a function
298+
/// that is capable of failing, the return type will generally be of the form `Result<T, E>`.
293299
/// The `From` trait simplifies error handling by allowing a function to return a single error type
294-
/// that encapsulate multiple error types. See the "Examples" section
295-
/// and [the book][book] for more details.
300+
/// that encapsulate multiple error types. See the "Examples" section and [the book][book] for more
301+
/// details.
296302
///
297303
/// **Note: This trait must not fail**. If the conversion can fail, use [`TryFrom`].
298304
///
@@ -313,13 +319,12 @@ pub trait Into<T>: Sized {
313319
/// assert_eq!(string, other_string);
314320
/// ```
315321
///
316-
/// While performing error handling it is often useful to implement `From`
317-
/// for your own error type. By converting underlying error types to our own custom error type
318-
/// that encapsulates the underlying error type, we can return a single error type
319-
/// without losing information on the underlying cause. The '?' operator automatically converts
320-
/// the underlying error type to our custom error type by calling `Into<CliError>::into`
321-
/// which is automatically provided when implementing `From`.
322-
/// The compiler then infers which implementation of `Into` should be used.
322+
/// While performing error handling it is often useful to implement `From` for your own error type.
323+
/// By converting underlying error types to our own custom error type that encapsulates the
324+
/// underlying error type, we can return a single error type without losing information on the
325+
/// underlying cause. The '?' operator automatically converts the underlying error type to our
326+
/// custom error type by calling `Into<CliError>::into` which is automatically provided when
327+
/// implementing `From`. The compiler then infers which implementation of `Into` should be used.
323328
///
324329
/// ```
325330
/// use std::fs;

0 commit comments

Comments
 (0)