Rollup merge of #154356 - joshtriplett:libs-api-integer-truncate-extend, r=jhpratt
Add integer truncation and extension methods Tracking issue: https://github.com/rust-lang/rust/issues/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]`).
J
Jacob Pratt committed
60e85c7ec3e873b956f9d1d686e3753a0bfc889d
Committed by GitHub <[email protected]>
on 4/1/2026, 7:35:10 AM