-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Doc:std::convert explicitely list generic impls #30901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,24 @@ | |
//! Like many traits, these are often used as bounds for generic functions, to | ||
//! support arguments of multiple types. | ||
//! | ||
//! - Use `as` for reference-to-reference conversions | ||
//! - Use `into` when you want to consume the value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this should be |
||
//! - `from` is the more flexible way, which can convert values and references | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this should be |
||
//! | ||
//! As a library writer, you should prefer implementing `From<T>` rather than | ||
//! `Into<U>`, as `From` provides greater flexibility and offer the equivalent `Into` | ||
//! implementation for free thanks to a blanket implementation in the standard library. | ||
//! | ||
//! **Note: these traits must not fail**. If the conversion can fail, you must use a dedicated | ||
//! method which return an `Option<T>` or a `Result<T, E>`. | ||
//! | ||
//! # Generic impl | ||
//! | ||
//! - `AsRef` and `AsMut` auto-dereference if the inner type is a reference | ||
//! - `From<U> for T` implies `Into<T> for U` | ||
//! - `From` and `Into` are reflexive, which means that all types can `into()` | ||
//! themselves and `from()` themselves | ||
//! | ||
//! See each trait for usage examples. | ||
|
||
#![stable(feature = "rust1", since = "1.0.0")] | ||
|
@@ -30,6 +48,9 @@ use marker::Sized; | |
/// | ||
/// [book]: ../../book/borrow-and-asref.html | ||
/// | ||
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which | ||
/// return an `Option<T>` or a `Result<T, E>`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Both `String` and `&str` implement `AsRef<str>`: | ||
|
@@ -45,6 +66,12 @@ use marker::Sized; | |
/// let s = "hello".to_string(); | ||
/// is_hello(s); | ||
/// ``` | ||
/// | ||
/// # Generic Impls | ||
/// | ||
/// - `AsRef` auto-dereference if the inner type is a reference or a mutable | ||
/// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`) | ||
/// | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait AsRef<T: ?Sized> { | ||
/// Performs the conversion. | ||
|
@@ -53,6 +80,15 @@ pub trait AsRef<T: ?Sized> { | |
} | ||
|
||
/// A cheap, mutable reference-to-mutable reference conversion. | ||
/// | ||
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which | ||
/// return an `Option<T>` or a `Result<T, E>`. | ||
/// | ||
/// # Generic Impls | ||
/// | ||
/// - `AsMut` auto-dereference if the inner type is a reference or a mutable | ||
/// reference (eg: `foo.as_ref()` will work the same if `foo` has type `&mut Foo` or `&&mut Foo`) | ||
/// | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait AsMut<T: ?Sized> { | ||
/// Performs the conversion. | ||
|
@@ -62,6 +98,13 @@ pub trait AsMut<T: ?Sized> { | |
|
||
/// A conversion that consumes `self`, which may or may not be expensive. | ||
/// | ||
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which | ||
/// return an `Option<T>` or a `Result<T, E>`. | ||
/// | ||
/// Library writer should not implement directly this trait, but should prefer the implementation | ||
/// of the `From` trait, which offer greater flexibility and provide the equivalent `Into` | ||
/// implementation for free thanks to a blanket implementation in the standard library. | ||
/// | ||
/// # Examples | ||
/// | ||
/// `String` implements `Into<Vec<u8>>`: | ||
|
@@ -75,6 +118,12 @@ pub trait AsMut<T: ?Sized> { | |
/// let s = "hello".to_string(); | ||
/// is_hello(s); | ||
/// ``` | ||
/// | ||
/// #Generic Impls | ||
/// | ||
/// - `From<T> for U` implies `Into<U> for T` | ||
/// - `into()` is reflexive, which means that `Into<T> for T` is implemented | ||
/// | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait Into<T>: Sized { | ||
/// Performs the conversion. | ||
|
@@ -84,6 +133,9 @@ pub trait Into<T>: Sized { | |
|
||
/// Construct `Self` via a conversion. | ||
/// | ||
/// **Note: this trait must not fail**. If the conversion can fail, use a dedicated method which | ||
/// return an `Option<T>` or a `Result<T, E>`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// `String` implements `From<&str>`: | ||
|
@@ -94,6 +146,11 @@ pub trait Into<T>: Sized { | |
/// | ||
/// assert_eq!(string, other_string); | ||
/// ``` | ||
/// # Generic impls | ||
/// | ||
/// - `From<T> for U` implies `Into<U> for T` | ||
/// - `from()` is reflexive, which means that `From<T> for T` is implemented | ||
/// | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub trait From<T>: Sized { | ||
/// Performs the conversion. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as
is broader than this, actually. it's for any kind of basic cast.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RFC which introduced them doesn't agree (ref) and has a nice rationale about it. If the rfc is still right, maybe explain further the motivation ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is about the
as
keyword, not theAs*
traits, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So yes, I think that all we need is proper capitalization/style here, to disambiguate from the keywords.