Skip to content

Conversation

kellerkindt
Copy link
Contributor

@kellerkindt kellerkindt commented Aug 10, 2021

Tracking #87920

Unresolved Questions

  • impl Div for Saturating<T> falls back on inner integer division - which seems alright?
    • add saturating_div? (to respect division by -1)
  • There is no ::saturating_shl and ::saturating_shr. (How to) implement Shl, ShlAssign, Shr and ShrAssign?
  • saturating_neg is only implemented on signed integer types
  • Is the implementation copied over from the Wrapping-type correct for Saturating?
    • Saturating::rotate_left
    • Saturating::rotate_right
    • Not
    • BitXorOr and BitXorOrAssign
    • BitOr and BitOrAssign
    • BitAnd and BitAndAssign
    • Saturating::swap_bytes
    • Saturating::reverse_bits

@rust-highfive
Copy link
Contributor

r? @kennytm

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 10, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@kennytm
Copy link
Member

kennytm commented Aug 12, 2021

i'm pretty sure the implementation of BitAnd, rotate_{left,right}, swap_bytes and reverse_bits are correct. the question is whether these bit-level operations are useful for Saturating, and whether they can live independent of Shl and Shr (see discussion from #87921 (comment)).

while the methods can have a separate library feature, the trait impls (BitAnd, BitOr, BitXor, Shl, Shr) must be stabilized together with Saturating.

@kellerkindt
Copy link
Contributor Author

I added naive shift (and remainder) impls. What needs to ben done next?

@kennytm kennytm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Aug 12, 2021
@kennytm
Copy link
Member

kennytm commented Aug 12, 2021

saturating_eng is only implemented on signed integer types

i don't think it makes sense to impl Neg for Saturating<unsigned> 😅

@kellerkindt
Copy link
Contributor Author

saturating_eng is only implemented on signed integer types

i don't think it makes sense to impl Neg for Saturating<unsigned> sweat_smile

Well... you are actually right about that 😅
fixed

@SkiFire13
Copy link
Contributor

impl Div for Saturating<T> falls back on inner integer division - which seems alright?

I think this is wrong in case of iN::MIN / -1. I would expect a saturating operation to evaluate to iN::MAX, instead it overflows and evaluates to iN::MIN. This also seems a reason to have a saturating_div for signed integers.

@rust-log-analyzer

This comment has been minimized.

Comment on lines +269 to +288
/// # Examples
///
/// Basic usage:
///
/// ```
/// #![feature(saturating_int_impl, saturating_div)]
/// use std::num::Saturating;
///
#[doc = concat!("assert_eq!(Saturating(2", stringify!($t), "), Saturating(5", stringify!($t), ") / Saturating(2));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MAX), Saturating(", stringify!($t), "::MAX) / Saturating(1));")]
#[doc = concat!("assert_eq!(Saturating(", stringify!($t), "::MIN), Saturating(", stringify!($t), "::MIN) / Saturating(1));")]
/// ```
///
/// ```should_panic
/// #![feature(saturating_int_impl, saturating_div)]
/// use std::num::Saturating;
///
#[doc = concat!("let _ = Saturating(0", stringify!($t), ") / Saturating(0);")]
/// ```
#[unstable(feature = "saturating_int_impl", issue = "87920")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure whether to keep this doc(-tests)

@kellerkindt
Copy link
Contributor Author

What are the next steps? :)

Copy link
Member

@kennytm kennytm left a comment

Choose a reason for hiding this comment

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

rest LGTM

Comment on lines 90 to 94
if other < 0 {
Saturating(self.0.shr((-other & self::shift_max::$t as $f) as u32))
} else {
Saturating(self.0.shl((other & self::shift_max::$t as $f) as u32))
}
Copy link
Member

Choose a reason for hiding this comment

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

🤔 this implementation is strange.

For other >= 0, the implementation is the same as self.0 << other. This is fine.

For other < 0, however, this becomes self.0 >> -other, which is never possible for the << operation.

I expected this either performs Ⓐ self.0 << other, or Ⓑ self.0.wrapping_shl(other), or Ⓒ self.0 << other.clamp(0, self::shift_max), or Ⓓ don't implement Shl<signed> at all.

Copy link
Contributor Author

@kellerkindt kellerkindt Aug 28, 2021

Choose a reason for hiding this comment

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

I've gone for Ⓓ because it does not seem clear (to me) what the correct impl should look like. I'd rather have someone figure it out later, than implementing it wrong now.

@kennytm
Copy link
Member

kennytm commented Aug 28, 2021

thanks!

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 28, 2021

📌 Commit ce636f2 has been approved by kennytm

@bors bors 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 Aug 28, 2021
@bors
Copy link
Collaborator

bors commented Aug 28, 2021

⌛ Testing commit ce636f2 with merge 677b517...

@bors
Copy link
Collaborator

bors commented Aug 29, 2021

☀️ Test successful - checks-actions
Approved by: kennytm
Pushing 677b517 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 29, 2021
@bors bors merged commit 677b517 into rust-lang:master Aug 29, 2021
@rustbot rustbot added this to the 1.56.0 milestone Aug 29, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 22, 2021
Stabilize feature `saturating_div` for rust 1.58.0

The tracking issue is rust-lang#89381

This seems like a reasonable simple change(?). The feature `saturating_div` was added as part of the ongoing effort to implement a `Saturating` integer type (see rust-lang#87921). The implementation has been discussed [here](rust-lang#87921 (comment)) and [here](rust-lang#87921 (comment)). It extends the list of saturating operations on integer types (like `saturating_add`, `saturating_sub`, `saturating_mul`, ...) by the function `fn saturating_div(self, rhs: Self) -> Self`.

The stabilization of the feature `saturating_int_impl` (for the `Saturating` type) needs to have this stabilized first.

Closes rust-lang#89381
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 22, 2021
Stabilize feature `saturating_div` for rust 1.58.0

The tracking issue is rust-lang#89381

This seems like a reasonable simple change(?). The feature `saturating_div` was added as part of the ongoing effort to implement a `Saturating` integer type (see rust-lang#87921). The implementation has been discussed [here](rust-lang#87921 (comment)) and [here](rust-lang#87921 (comment)). It extends the list of saturating operations on integer types (like `saturating_add`, `saturating_sub`, `saturating_mul`, ...) by the function `fn saturating_div(self, rhs: Self) -> Self`.

The stabilization of the feature `saturating_int_impl` (for the `Saturating` type) needs to have this stabilized first.

Closes rust-lang#89381
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants