core/num: Implement feature integer_cast_extras#154664
core/num: Implement feature integer_cast_extras#154664rust-bors[bot] merged 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
| #[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.
There was a problem hiding this comment.
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`
e88d2eb to
fd3a74d
Compare
|
Looks fine for unstable |
…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`
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`)
…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`)
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`
…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`)
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