Skip to content
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

Rollup of 7 pull requests #116492

Merged
merged 25 commits into from
Oct 6, 2023
Merged
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift click to select a range
13e5875
add some docs to hooks/mod.rs
RalfJung Sep 30, 2023
23efab4
Fix typo in attrs.rs
eltociear Oct 4, 2023
80bf688
Remove an unnecessary `pub(crate)`.
nnethercote Oct 5, 2023
4091936
Make the comment order match variant declaration order.
nnethercote Oct 5, 2023
449b84c
Remove `map_layouts`.
nnethercote Oct 6, 2023
73420fc
Fix a comment.
nnethercote Oct 6, 2023
29ed8e4
Remove the `MaybeTransmutableQuery<&'l Dfa<...>, C>` impl.
nnethercote Oct 6, 2023
442a66d
Remove unneeded dependency.
nnethercote Oct 3, 2023
108e541
Remove unused `FileName::CfgSpec`.
nnethercote Oct 4, 2023
e49a147
Factor out `insert_or_error`.
nnethercote Oct 4, 2023
4b51a3e
Remove unneeded dependency.
nnethercote Oct 6, 2023
093b435
Remove unneeded features.
nnethercote Oct 6, 2023
e7dabc9
Remove unnecessary `pub`.
nnethercote Oct 6, 2023
b80e653
Attempt to describe the intent behind the `From` trait further
scottmcm Aug 6, 2023
44f92c1
Don't mention "recover the original" in `From` docs
scottmcm Oct 6, 2023
1651f1f
Elaborate some caveats to lossless
scottmcm Oct 6, 2023
5432d13
Reuse existing `Some`s in `Option::(x)or`
scottmcm Oct 6, 2023
c95015c
Minor doc clarification in Once::call_once
peterjoel Oct 6, 2023
4dfa5e5
Rollup merge of #114564 - scottmcm:when-to-from, r=dtolnay
matthiaskrgr Oct 6, 2023
fdb136a
Rollup merge of #116297 - RalfJung:hooks, r=oli-obk
matthiaskrgr Oct 6, 2023
81192f2
Rollup merge of #116423 - eltociear:patch-22, r=flip1995
matthiaskrgr Oct 6, 2023
25fbd13
Rollup merge of #116466 - nnethercote:rustc_transmute, r=oli-obk
matthiaskrgr Oct 6, 2023
9796dfd
Rollup merge of #116474 - nnethercote:rustc_assorted, r=spastorino
matthiaskrgr Oct 6, 2023
f8dae0c
Rollup merge of #116481 - scottmcm:tweak-combinators, r=cuviper
matthiaskrgr Oct 6, 2023
7f0cf8c
Rollup merge of #116484 - peterjoel:once-doc-clarify, r=cuviper
matthiaskrgr Oct 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 479,46 @@ pub trait Into<T>: Sized {
/// - `From<T> for U` implies [`Into`]`<U> for T`
/// - `From` is reflexive, which means that `From<T> for T` is implemented
///
/// # When to implement `From`
///
/// While there's no technical restrictions on which conversions can be done using
/// a `From` implementation, the general expectation is that the conversions
/// should typically be restricted as follows:
///
/// * The conversion is *infallible*: if the conversion can fail, use [`TryFrom`]
/// instead; don't provide a `From` impl that panics.
///
/// * The conversion is *lossless*: semantically, it should not lose or discard
/// information. For example, `i32: From<u16>` exists, where the original
/// value can be recovered using `u16: TryFrom<i32>`. And `String: From<&str>`
/// exists, where you can get something equivalent to the original value via
/// `Deref`. But `From` cannot be used to convert from `u32` to `u16`, since
/// that cannot succeed in a lossless way. (There's some wiggle room here for
/// information not considered semantically relevant. For example,
/// `Box<[T]>: From<Vec<T>>` exists even though it might not preserve capacity,
/// like how two vectors can be equal despite differing capacities.)
///
/// * The conversion is *value-preserving*: the conceptual kind and meaning of
/// the resulting value is the same, even though the Rust type and technical
/// representation might be different. For example `-1_i8 as u8` is *lossless*,
/// since `as` casting back can recover the original value, but that conversion
/// is *not* available via `From` because `-1` and `255` are different conceptual
/// values (despite being identical bit patterns technically). But
/// `f32: From<i16>` *is* available because `1_i16` and `1.0_f32` are conceptually
/// the same real number (despite having very different bit patterns technically).
/// `String: From<char>` is available because they're both *text*, but
/// `String: From<u32>` is *not* available, since `1` (a number) and `"1"`
/// (text) are too different. (Converting values to text is instead covered
/// by the [`Display`](crate::fmt::Display) trait.)
///
/// * The conversion is *obvious*: it's the only reasonable conversion between
/// the two types. Otherwise it's better to have it be a named method or
/// constructor, like how [`str::as_bytes`] is a method and how integers have
/// methods like [`u32::from_ne_bytes`], [`u32::from_le_bytes`], and
/// [`u32::from_be_bytes`], none of which are `From` implementations. Whereas
/// there's only one reasonable way to wrap an [`Ipv6Addr`](crate::net::Ipv6Addr)
/// into an [`IpAddr`](crate::net::IpAddr), thus `IpAddr: From<Ipv6Addr>` exists.
///
/// # Examples
///
/// [`String`] implements `From<&str>`:
Expand Down