Skip to content

core/num: Implement feature integer_cast_extras#154664

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
okaneco:integer_cast_extras
Apr 20, 2026
Merged

core/num: Implement feature integer_cast_extras#154664
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
okaneco:integer_cast_extras

Conversation

@okaneco
Copy link
Copy Markdown
Contributor

@okaneco okaneco commented Apr 1, 2026

Tracking issue #154650
Accepted ACP rust-lang/libs-team#765 (comment)

Implement saturating, checked, and strict casting between signed and unsigned integer primitives of the same bit-width.

Add cast_integer panic function to overflow_panic.rs

@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 Apr 1, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 1, 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, jhpratt, scottmcm

@rustbot

This comment has been minimized.

@okaneco okaneco force-pushed the integer_cast_extras branch 2 times, most recently from 338f8fd to 391c4f0 Compare April 1, 2026 05:57
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 1, 2026

Some changes occurred in src/tools/cargo

cc @ehuss

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 1, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@okaneco okaneco force-pushed the integer_cast_extras branch 2 times, most recently from 37c9674 to 7408601 Compare April 1, 2026 06:09
@okaneco
Copy link
Copy Markdown
Contributor Author

okaneco commented Apr 1, 2026

Sorry about that.
Messed up updating the branch commit in the rebase with a typo then had to fix the submodule changes. Think I got them all.

@rust-log-analyzer

This comment has been minimized.

@okaneco okaneco force-pushed the integer_cast_extras branch from 7408601 to e88d2eb Compare April 1, 2026 06:19
Comment thread library/core/src/num/uint_macros.rs Outdated
#[inline(always)]
pub const fn saturating_cast_signed(self) -> $SignedT {
// Clamp to the signed integer max size, which is ActualT::MAX >> 1.
if self < <$SignedT>::MAX.cast_unsigned() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self < <$SignedT>::MAX.cast_unsigned() {
if self < const { <$SignedT>::MAX.cast_unsigned() } {

Maybe? Not sure if it actually does something tho

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's #[inline(always)] and an as cast under the hood, I think we're okay without doing this.

Comment thread library/core/src/num/int_macros.rs Outdated
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
pub const fn checked_cast_unsigned(self) -> Option<$UnsignedT> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the docs maybe mentions that checked_cast_* do the same things as the existing TryFrom impls?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recently added truncate/extend methods don't mention it on the checked_truncate method so I think we can omit it.

I don't think it's worth bringing up since they have different signatures, and the more relevant information is knowing what the integer min/max ranges are for the types involved. There are possible type inference issues that this function won't have either, along with being usable in const without const traits.

Sometimes I think that Rust docs over-explain or attempt to mention too many possible caveats which hinders readability, even though it's well-intentioned. This is one of the cases where it feels like extraneous information to me but I can see the other side too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's good to have simple cross-references between similar things. A little note here about "If you want to do this but to a generic target type, use TryFrom" or similar would be nice.

Implement saturating, checked, and strict casting between
signed and unsigned integer primitives of the same bit-width.

Add `cast_integer` function to `overflow_panic.rs`
@okaneco okaneco force-pushed the integer_cast_extras branch from e88d2eb to fd3a74d Compare April 1, 2026 19:59
@scottmcm
Copy link
Copy Markdown
Member

Looks fine for unstable
@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 20, 2026

📌 Commit fd3a74d has been approved by scottmcm

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 20, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 20, 2026
…ttmcm

core/num: Implement feature `integer_cast_extras`

Tracking issue rust-lang#154650
Accepted ACP rust-lang/libs-team#765 (comment)

Implement `saturating`, `checked`, and `strict` casting between signed and unsigned integer primitives of the same bit-width.

Add `cast_integer` panic function to `overflow_panic.rs`
rust-bors Bot pushed a commit that referenced this pull request Apr 20, 2026
Rollup of 6 pull requests

Successful merges:

 - #154654 (Move `std::io::ErrorKind` to `core::io`)
 - #155054 (constify `Index(Mut)`, `Deref(Mut)` for `Vec`)
 - #155507 (suggest expect instead of unwrap when arg provided)
 - #154664 (core/num: Implement feature `integer_cast_extras`)
 - #155474 (Rename incremental `cfail`/`cpass` revisions to `bfail`/`bpass`)
 - #155493 (docs(num): fix stale link to `mem::Alignment`)
rust-bors Bot pushed a commit that referenced this pull request Apr 20, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - #155054 (constify `Index(Mut)`, `Deref(Mut)` for `Vec`)
 - #155264 (Add autocast support for `x86amx`)
 - #155507 (suggest expect instead of unwrap when arg provided)
 - #154664 (core/num: Implement feature `integer_cast_extras`)
 - #155238 (compiletest: add a new diff for compare-out-by-lines tests.)
 - #155474 (Rename incremental `cfail`/`cpass` revisions to `bfail`/`bpass`)
 - #155493 (docs(num): fix stale link to `mem::Alignment`)
 - #155529 (Remove `AttributeLintKind` variants - part 3)
 - #155531 (Tweak `is_ascii_punctuation()` docs wording)
 - #155533 (Provide a const new for `GlobalCache`)
@rust-bors rust-bors Bot merged commit 26f47ae into rust-lang:main Apr 20, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 20, 2026
rust-timer added a commit that referenced this pull request Apr 20, 2026
Rollup merge of #154664 - okaneco:integer_cast_extras, r=scottmcm

core/num: Implement feature `integer_cast_extras`

Tracking issue #154650
Accepted ACP rust-lang/libs-team#765 (comment)

Implement `saturating`, `checked`, and `strict` casting between signed and unsigned integer primitives of the same bit-width.

Add `cast_integer` panic function to `overflow_panic.rs`
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 20, 2026
…uwer

Rollup of 10 pull requests

Successful merges:

 - rust-lang/rust#155054 (constify `Index(Mut)`, `Deref(Mut)` for `Vec`)
 - rust-lang/rust#155264 (Add autocast support for `x86amx`)
 - rust-lang/rust#155507 (suggest expect instead of unwrap when arg provided)
 - rust-lang/rust#154664 (core/num: Implement feature `integer_cast_extras`)
 - rust-lang/rust#155238 (compiletest: add a new diff for compare-out-by-lines tests.)
 - rust-lang/rust#155474 (Rename incremental `cfail`/`cpass` revisions to `bfail`/`bpass`)
 - rust-lang/rust#155493 (docs(num): fix stale link to `mem::Alignment`)
 - rust-lang/rust#155529 (Remove `AttributeLintKind` variants - part 3)
 - rust-lang/rust#155531 (Tweak `is_ascii_punctuation()` docs wording)
 - rust-lang/rust#155533 (Provide a const new for `GlobalCache`)
@okaneco okaneco deleted the integer_cast_extras branch April 20, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants