Add integer truncation and extension methods#154356
Open
joshtriplett wants to merge 3 commits intorust-lang:mainfrom
Open
Add integer truncation and extension methods#154356joshtriplett wants to merge 3 commits intorust-lang:mainfrom
joshtriplett wants to merge 3 commits intorust-lang:mainfrom
Conversation
This allows traits in `core` to be sealed as well. This could be the same trait as `std` via a re-export, but that would require `core::sealed` to be `pub` (even if unstable). Keep them as separate traits for now.
This provides `.truncate()`, `.saturating_truncate()`, `.checked_truncate()`, and `.extend()`. These only work within the same signedness (use `.cast_signed()` and `.cast_unsigned()` to change sign). The truncation methods only work to smaller (or equal) types. `.extend()` only works to larger (or equal) types. For the purposes of truncation and extending, `u128` is considered larger than or equal to the size of `usize`, and `usize` is considered larger than `u16` or `u8`. This is consistent with `From`/`Into` conversions. Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
Collaborator
|
r? @scottmcm rustbot has assigned @scottmcm. Use Why was this reviewer chosen?The reviewer was selected based on:
|
Collaborator
|
The job Click to see the possible cause of the failure (guessed by this bot) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Tracking issue: #154330
This provides
.truncate(),.saturating_truncate(),.checked_truncate(), and.extend().These only work within the same signedness (use
.cast_signed()and.cast_unsigned()to change sign).The truncation methods only work to smaller (or equal) types.
.extend()only works to larger (or equal) types.For the purposes of truncation and extending,
u128is considered larger than or equal to the size ofusize, andusizeis considered larger thanu16oru8. This is consistent withFrom/Intoconversions. We might, in the future, want to consider ways to expand this.Much of this was pair-programmed with @Amanieu.
In order to seal the new traits, this PR also adds a
core::sealed::Sealed, like the one instd. I didn't modifystdto re-export the same one, since by definition it isn't nameable, and since doing that would require that it be nameable (even if it was#[unstable]).