-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: Make the as
keyword consider Into
Trait implementations
#2308
RFC: Make the as
keyword consider Into
Trait implementations
#2308
Conversation
as
keyword consider Into
Trait implementationsas
keyword consider Into
Trait implementations
94fe115
to
486cbcc
Compare
486cbcc
to
1285b70
Compare
// is a syntax sugar for | ||
let x = Foo(42); | ||
let y = Into::<Bar>::into(x); | ||
``` |
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 won't work since as
is not equivalent to a call to .into()
- for example, as
can convert from f32
to u32
, while .into()
can't (this is a lossy conversion, so )..into()
shouldn't work here
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 what is the rule of as
conversion and into
conversion ?
I think this is related to this thread about lossy conversions between as
and into
(I linked it in the Rendered).
Here is more informations on the actual |
I would rather deprecate |
Not sure I want this, since there’s a difference between a cast/coercion and a conversion. |
Exactly, and `as` unhelpfully conflates them.. e.g. floating point to
integer conversion and truncating integers.
…On Sun, Jan 21, 2018 at 2:43 PM, Dimitri Sabadie ***@***.***> wrote:
Not sure I want this, since there’s a differenc between a cast/coercion
and a conversion.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2308 (comment)>, or mute
the thread
</~https://github.com/notifications/unsubscribe-auth/AAC3nwkHC6ZdpcLu5F6lT5eiKKVzcUTVks5tM5NtgaJpZM4Rl3p0>
.
|
@durka isn’t that unsafe anyway? |
Oh yeah, no warning nor error. It should be! 😮 |
Here is more information on the error emmited by the compiler on lossy conversions, TLDR; no error nor warnings with the |
The In this RFC I propose to keep the actual warnings and errors that the |
@durka Why not keeping both good parts of these features ? Keeping the simple |
My beef is that `as` isn't currently simple. It does both casts and
coercions. I'd be fine merging `as` and Into as long as that means it would
work with generics such as `fn conv<A: Into<B>>(a: A) -> B { a as B }`.
However, currently, `as` supports conversions that Into doesn't, like lossy
integer truncation and unsizing coercions. So I don't see it as simple as
"just combine them".
…On Mon, Jan 22, 2018 at 5:11 PM, Clément Renault ***@***.***> wrote:
@durka </~https://github.com/durka> Why not keeping both good parts of
these features ? Keeping the simple as keyword and keeping the warnings
and errors of the trait-based conversions like Into ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2308 (comment)>, or mute
the thread
</~https://github.com/notifications/unsubscribe-auth/AAC3n4GY7aHKbuZ-mkODXpEezKUM5D0Tks5tNQeLgaJpZM4Rl3p0>
.
|
That was actually just a little idea in my head, just wanted to have some more discussion about it. I think you are right that’s not as simple as « just combine them » but you talked about deprecating it, what about reusing it ? 😀 |
Well that might present backcompat issues, but if there's a way to
integrate `as` better with the trait system (like a std::ops trait so you
can extend it etc) without horribly complicating things then I'm all for it!
…On Mon, Jan 22, 2018 at 5:37 PM, Clément Renault ***@***.***> wrote:
That was actually just a little idea in my head, just wanted to have some
more discution about it.
I think you are right that’s not as simple as « just combine them » but
you talked about deprecating it, what about reusing it ? 😀
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2308 (comment)>, or mute
the thread
</~https://github.com/notifications/unsubscribe-auth/AAC3n9B_aulSLyU7mqpitPEfVTJkyo83ks5tNQ2WgaJpZM4Rl3p0>
.
|
I think that if we unify cheap
|
In clippy we actually recommend folks use |
Yeah, that’s actually what we do in Haskell: we use typeclasses’ methods, casts / coercions are not directly accessible via an operator baked in the language. |
My understanding of I haven't hacked on the compiler and looked at how |
Slightly different idea: make a |
I've found this to be a useful guarantee, and the existing impls in libstd certainly comply with this, but the |
I forgot about another usage of |
There is an interresting comment about the compiler not emiting enough warnings/errors about unsafe casts/coercions. I propose to use the mem::transmute function to do unsafe conversions like EDIT: transmuting here is probably not possible because of the two possible different memory sizes. |
|
What about an |
True, but that's very untypical (and still safe) |
It never occurred to me that Rust would allow such a thing, given past experience with things like DOS game installers that refuse to complete within a typical DOSBox setup because their signed integer free space variable wrapped around to the negative when encountering DOSBox's representation of the free space on a modern-sized hard drive. Is there a warning (core or clippy) that I can |
Clippy has a pretty nice overview for all the Lints. It has lints for possible truncation, wrap, sign and even precision loss. |
Ahh, so it's handled like any other int-to-int cast would be with regards to lints? (I think of enums as being distinct from regular ints when it comes to the kinds of guarantees you want to enforce on them, so that possibility didn't occur to me.) In that case, I'll have to think about whether I want to bump those lints up to |
Please do not use sarcasm in a RFC’s discussion section; it conveys no useful meaning nor material to step forwards. Casting such a big enum to a
|
My message is not appropriate I have to admit. I just removed it and add a playground to show the actual behavior of casting a big enum to a smaller/un-adapted primitive. This is not unsafe, it's more an unspecified behavior or something like that I would say. I will repeat myself but we must add more warnings in the compiler. Using The |
Tagging T-libs as well, since the most-👍'd alternative is library additions. |
@rfcbot fcp close I'm going to propose closing this RFC -- though not because I'm opposed to the general idea. I just don't think the specifics of this proposal feel right. I've not ready the comment thread in detail, and I'm sure the following points have been made, but I'll repeat:
This seems to violate both of those rules, since the (I would perhaps be in favor of some other proposals -- presuming they could be made to work technically -- such as |
Team member @nikomatsakis has proposed to close this. The next step is review by the rest of the tagged teams:
No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
Closing as nothing new has happened during FCP. Thanks for the RFC, @Kerollmops! |
Permit to use the common
as
keyword with any type that implement theInto
Trait,allowing explicit conversions whose primitives already benefit, more visible than simple function calls.
Rendered
#2308 (comment) In this RFC I propose to keep the actual warnings and errors that the
Into
trait already emit on possible lossy conversions and apply these to the actualas
keyword, combining the two features and removing possible confusions.This keyword is used in the disambiguation of function calls and also in the renaming imports. But it seems those two usage are unrelated to the one I want to change, this is like another keyword with the same name.