core/num: Implement feature integer_cast_extras#154664
core/num: Implement feature integer_cast_extras#154664okaneco wants to merge 1 commit intorust-lang:mainfrom
integer_cast_extras#154664Conversation
|
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.
338f8fd to
391c4f0
Compare
|
Some changes occurred in src/tools/cargo cc @ehuss |
|
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. |
This comment has been minimized.
This comment has been minimized.
37c9674 to
7408601
Compare
|
Sorry about that. |
This comment has been minimized.
This comment has been minimized.
7408601 to
e88d2eb
Compare
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() { |
There was a problem hiding this comment.
| if self < <$SignedT>::MAX.cast_unsigned() { | |
| if self < const { <$SignedT>::MAX.cast_unsigned() } { |
Maybe? Not sure if it actually does something tho
There was a problem hiding this comment.
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> { |
There was a problem hiding this comment.
Should the docs maybe mentions that checked_cast_* do the same things as the existing TryFrom impls?
There was a problem hiding this comment.
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`
e88d2eb to
fd3a74d
Compare
Tracking issue #154650
Accepted ACP rust-lang/libs-team#765 (comment)
Implement
saturating,checked, andstrictcasting between signed and unsigned integer primitives of the same bit-width.Add
cast_integerpanic function tooverflow_panic.rs