-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Remove Copy from Ensure* traits
#13043
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]>
lemunozm
left a comment
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
| /// ``` | ||
| 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]>
lemunozm
left a comment
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 |
sam0x17
left a comment
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
BaseArithmeticwork withEnsure.It re-uses the code from
EnsureOpAssigninEnsureOp, instead of the other way around, which incurred theCopyrequirement.It does contain an artificial dependence of
EnsureOponEnsureOpAssignto re-use code, but that should be fine since the trait bounds would be the same anyway.Removes the
Copyrequirement from:EnsureOpandEnsureOpAssignEnsureFromandEnsureInto: Not sure about this one since we now generate the error in every case. Could be slightly slower.EnsureforBaseArithmeticcc @lemunozm