Uh oh!
There was an error while loading. Please reload this page.
Add integer truncation and extension methods - #154356
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.
rustbot
commented
Mar 25, 2026
r? @scottmcm rustbot has assigned @scottmcm. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
joshtriplett
commented
Mar 25, 2026
r? jhpratt |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
2ae8ba8 to
f0c314eComparejuntyr
commented
Mar 26, 2026
Has Rust guaranteed before that [u|i]size will always be smaller than 128bit? |
joshtriplett
commented
Mar 26, 2026
@jhpratt I added const trait support. I didn't end up squashing it, because it was sufficiently fiddly that I think there's genuine value in having it as a separate commit from the initial implementation. Would you mind checking it, and r+ing if you think it looks reasonable? (Not applying your r=me because you had said to squash it first.) |
jhpratt
commented
Mar 26, 2026
I'll take a look at this in the coming day! It is worth double-checking the |
Imo this could be reduced some, but not necessarily to a single commit
And after looking, |
d85ff87 to
79a1e89Comparejoshtriplett
commented
Mar 28, 2026
@jhpratt I've rebased this, squashing in the fixes to previous commits. And yes, the libs-api nomination is to evaluate the implementations allowing conversion between u128 and usize. (Frankly, I think we ought to evaluate the possibility of conditional implementations between u64 and usize, but that's a separate matter for another day.) |
jhpratt
commented
Mar 28, 2026
Works for me. Conditional implementations could work, but would prove hazardous to libraries that try to work across platforms. To the extent that there would probably be a clippy lint for relying on such an implementation. |
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, `usize` is considered larger than `u16` or `u8`. This is consistent with `From`/`Into` conversions. Adding these methods results in needing to update the output of one test that gets a new method-name similarity result. Co-authored-by: Amanieu d'Antras <amanieu@gmail.com>
79a1e89 to
68c8339Comparejoshtriplett
commented
Mar 31, 2026
@jhpratt We discussed this in today's @rust-lang/libs-api meeting. We agreed to drop the We also talked about strategies for shipping the conditional impls (for these and for From/Into), but that can happen separately. |
jhpratt
commented
Apr 1, 2026
Perfect. Happy to finally see this uplifted! @bors r+ rollup |
…cate-extend, r=jhpratt Add integer truncation and extension methods Tracking issue: rust-lang#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, `u128` is considered larger than or equal to the size of `usize`, and `usize` is considered larger than `u16` or `u8`. 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 in `std`. I didn't modify `std` to 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]`).
Uh oh!
There was an error while loading. Please reload this page.
Rollup merge of #154356 - joshtriplett:libs-api-integer-truncate-extend, r=jhpratt Add integer truncation and extension methods 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, `u128` is considered larger than or equal to the size of `usize`, and `usize` is considered larger than `u16` or `u8`. 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 in `std`. I didn't modify `std` to 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]`).
Mark-Simulacrum
commented
Apr 6, 2026
@rust-timer build e6fe002 |
This comment has been minimized.
This comment has been minimized.
rust-timer
commented
Apr 6, 2026
Finished benchmarking commit (e6fe002): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 1.0%, secondary 0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -4.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 483.835s -> 483.687s (-0.03%) |
RalfJung
commented
Apr 10, 2026
Friendly reminder to please update the PR description when the nature of a PR is changed like that. :) Ideally this happens before landing since the PR description ends up in the git history. |
View all comments
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, nothing is considered larger than or equal to the size of
usize, andusizeis considered larger thanu16oru8. 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]).