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.
Traits for lossy conversions #3415
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
base: master
Are you sure you want to change the base?
Traits for lossy conversions #3415
Changes from all commits
272de1f
0426a2a
02f8df9
2347d75
7d7735d
821dc26
8281c7d
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
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.
What happens when we convert
-1_i16
tou32
? Does it become 2^16-1 or 2^32-1?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.
It becomes 232 - 1. The first definition I gave is too imprecise for this situation. The mathematical definition is correct:
I'll have to update the first definition.
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.
Are there any architectures that have specific instructions for this? The assertion that it "must be implemented manually" doesn't seem necessary in this RFC.
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.
I don't know about the hardware support. AFAIK saturating conversions require 1 or 2 extra branches. Mentioning in the reference-level explanation how it is implemented isn't wrong.
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.
Can we still have a
TruncatingInto
trait just for ergonomics e.g. writingfn(x: impl TruncatingInto<u8>)
?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.
I'm still unsure if the traits should be exposed at all. The RFC currently says that the traits are unstable, so you can only use the inherent methods.
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.
Are you sure it is a good idea to make
u32 -> u16
andi16 -> u16
have the same trait? The implication feels quite different.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.
I am not sure at all. I'm open to introducing more granular traits and methods, if most people prefer it. But I find it difficult to judge the community's attitude towards this matter. Of course, people who are happy with the proposal in its current form are underrepresented in this discussion.
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.
For consistency, is it better to allow
i8 -> i8
andi8 -> i16
as well? For example if I have a function that acceptsT where u32: SaturatingFrom<T>
, I wouldn't be able to accept au8
there.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.
I understand that
u8
is not part ofisize
becauseisize
is always a superset, but would it be more consistent to make them symmetric?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.
Are you suggesting to implement saturating and truncating methods for lossless conversions? I don't think that makes sense. Or what kind of symmetry are you looking for?
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.
Is there any justification that people do not already use
cast_lossless
? Or might it simply be because it is too troublesome to do.into()
and specify the proper type (because we don't have.into::<T>()
)?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.
Currently,
cast_lossless
isn't used because of the more verbose syntax. The rationale for this lint is:as
can sometimes perform lossy conversions, so using it even for lossless conversions is bad, because it may silently become lossy when changing types while refactoring.Once lossy
as
casts produce a warning, there will be no benefit to forbidding losslessas
casts.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.
There is already a
trunc
method inf64
, not sure if that would cause confusion.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.
Were there existing libraries doing something similar? I tried writing one myself, but I am the only user there. Would be great if there are some idiomatic libraries to sandbox this before it gets into the language.