-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Conversation
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this without the Copy
dependency 😄 . Some comments below
@@ -728,14 +725,13 @@ mod ensure { | |||
/// assert_eq!(underflow(), Err(ArithmeticError::Underflow)); | |||
/// ``` | |||
fn ensure_from(other: T) -> Result<Self, ArithmeticError> { | |||
Self::try_from(other).map_err(|_| error::equivalent(other)) | |||
let err = error::equivalent(&other); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the performance of this, it seems like the assembly resulting code of this can be easily reordered by the compiler and should not be penalty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not empirically (not sure how to do it well).
To extend my argument, error::equivalent
has no side effects, and if the resulting value is only read under a condition, the compiler can only execute that for that condition. I understand that this works for any T that is not really consumed by the try_from()
, which means that T is in fact a Copy
type. The compiler could not optimize this if T is not Copy
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay ;)
Co-authored-by: Luis Enrique Muñoz Martín <[email protected]> Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Nice tests 🚀
bot merge |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
* Remove Copy from EnsureOp and EnsureOpAssign Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Remove Copy from EnsureFrom and EnsureInto Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Fix default impl Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Reuse assignment code in Ensure trait Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Require Ensure for all BaseArithmetic types Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Fix assign impls Co-authored-by: Luis Enrique Muñoz Martín <[email protected]> Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Add tests Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Add success doc tests Signed-off-by: Oliver Tale-Yazdi <[email protected]> Signed-off-by: Oliver Tale-Yazdi <[email protected]>
* Remove Copy from EnsureOp and EnsureOpAssign Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Remove Copy from EnsureFrom and EnsureInto Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Fix default impl Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Reuse assignment code in Ensure trait Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Require Ensure for all BaseArithmetic types Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Fix assign impls Co-authored-by: Luis Enrique Muñoz Martín <[email protected]> Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Add tests Signed-off-by: Oliver Tale-Yazdi <[email protected]> * Add success doc tests Signed-off-by: Oliver Tale-Yazdi <[email protected]> Signed-off-by: Oliver Tale-Yazdi <[email protected]>
Follow up on #12967 (comment) to make
BaseArithmetic
work withEnsure
.It re-uses the code from
EnsureOpAssign
inEnsureOp
, instead of the other way around, which incurred theCopy
requirement.It does contain an artificial dependence of
EnsureOp
onEnsureOpAssign
to re-use code, but that should be fine since the trait bounds would be the same anyway.Removes the
Copy
requirement from:EnsureOp
andEnsureOpAssign
EnsureFrom
andEnsureInto
: Not sure about this one since we now generate the error in every case. Could be slightly slower.Ensure
forBaseArithmetic
cc @lemunozm