-
Notifications
You must be signed in to change notification settings - Fork 142
std::num::Checked{Add,Sub,Mul,Div}
should have type parameters
#15
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
From @clarcharr on September 30, 2017 21:40 Adding onto this, there should be checked versions of |
IMHO these traits should also take things by value for consistency with the original traits. Object safety is already broken because they still rely on the original not-object-safe traits. |
I've decided to limit the clutter of |
For sake of completeness it's worth mentioning that not just the
Same for |
To add another motivation for adding type parameters consider vector-scaling arithmetic: impl CheckedMul<Scalar> for Vector {
fn checked_mul(&self, v: &Self) -> Option<Self> {
// ~~~~
// not possible as we would require `Scalar` here,
// not `Self` (problem caused by `Self: Mul<Self, Output = _>`)
~~~~
}
} impl CheckedMul<Vector> for Scalar {
fn checked_mul(&self, v: &Self) -> Option<Self> {
// ~~~~ ~~~~
// not possible either as we would require `Vector` here,
// not `Self` (problem caused by `Self: Mul<Self, Output = Self>`)
~~~~ ~~~~
}
} |
Since this issue doesn't actually mention what the desired re-definitions would actually look like I'd like to take the opportunity to provide one such recommendation here:
pub trait Checked…<Rhs = Self> {
type Output;
#[must_use]
fn checked_…(self, rhs: Rhs) -> Option<Self::Output>;
}
pub trait UncheckedAdd<Rhs = Self> {
type Output;
#[must_use]
fn unchecked_add(self, rhs: Rhs) -> Self::Output;
}
pub trait Saturating…<Rhs = Self> {
type Output;
#[must_use]
fn saturating_…(self, rhs: Rhs) -> Self::Output;
}
pub trait Wrapping…<Rhs = Self> {
type Output;
#[must_use]
fn wrapping_…(self, rhs: Rhs) -> Self::Output;
}
pub trait Overflowing…<Rhs = Self> {
type Output;
#[must_use]
fn overflowing_…(self, rhs: Rhs) -> (Self::Output, bool);
} |
From @steveklabnik on February 11, 2015 1:35
Monday Apr 14, 2014 at 12:33 GMT
For earlier discussion, see rust-lang/rust#13510
This issue was labelled with: A-libs in the Rust repository
The corresponding
std::ops
traits have both the type of right hand side operand and the type of result, whileChecked*
traits do not (and always assume that both operands and result have the same type). There is no real reason to makeChecked*
traits behave differently fromstd::ops
traits, and having them as is hampers advanced uses of operator overloading: for example, if the scalar type (e.g.DateTime
) and difference type (e.g.Duration
) are distinct from each other then the current scheme wouldn't work at all.Copied from original issue: rust-num/num#59
The text was updated successfully, but these errors were encountered: