Skip to content

Add integer truncation and extension methods#154356

Open
joshtriplett wants to merge 3 commits intorust-lang:mainfrom
joshtriplett:libs-api-integer-truncate-extend
Open

Add integer truncation and extension methods#154356
joshtriplett wants to merge 3 commits intorust-lang:mainfrom
joshtriplett:libs-api-integer-truncate-extend

Conversation

@joshtriplett
Copy link
Member

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. This is consistent with From/Into conversions. 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]).

joshtriplett and others added 3 commits March 24, 2026 20:24
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>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 25, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 25, 2026

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, scottmcm

@joshtriplett joshtriplett added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 25, 2026
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Executing "/scripts/stage_2_test_set1.sh"
+ /scripts/stage_2_test_set1.sh
PR_CI_JOB set; skipping tidy
+ '[' 1 == 1 ']'
+ echo 'PR_CI_JOB set; skipping tidy'
+ SKIP_TIDY='--skip tidy'
+ ../x.py --stage 2 test --skip tidy --skip compiler --skip src
##[group]Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.04s
##[endgroup]
downloading https://static.rust-lang.org/dist/2026-03-05/rustfmt-nightly-aarch64-unknown-linux-gnu.tar.xz
---
10    = help: items from traits can only be used if the trait is in scope
11 help: trait `SettingsExt` which provides `ext` is implemented but not in scope; perhaps you want to import it

12    |
13 LL + use auto::SettingsExt;
14    |
+ help: there is a method `extend` with a similar name
+    |
+ LL |     a.extend();
+    |          +++
---
To only update this specific test, also pass `--test-args imports/issue-114682-3.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/imports/issue-114682-3.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/imports/issue-114682-3" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/imports/issue-114682-3/auxiliary"
stdout: none
--- stderr -------------------------------
error[E0599]: no method named `ext` found for type `u8` in the current scope
##[error]  --> /checkout/tests/ui/imports/issue-114682-3.rs:20:7
   |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants