Skip to content

core/num: Implement feature integer_cast_extras#154664

Open
okaneco wants to merge 1 commit intorust-lang:mainfrom
okaneco:integer_cast_extras
Open

core/num: Implement feature integer_cast_extras#154664
okaneco wants to merge 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
#[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.

#[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.

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
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 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