Tidy up const_ops
, rename to const_arith_ops
#143949
Open
+391
−186
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Tracking issue: #143802
This is split into three commits for ease of reviewability:
forward_ref_*
macros to accept multiple attributes (in anticipation of needingrust_const_unstable
attributes) and also require attributes in these macros. Since the default attribute only helps for the initial implementations, it means it's easy to get wrong for future implementations, as shown for the saturating implementations which were incorrect before.const_ops
feature toconst_arith_ops
and adds constness and trivial implementations to the remaining traits incore::ops::arith
.A few random other notes on the implementation specifically:
macro_named_capture_groups
#142999 implemented, we can't make the constness in theforward_ref_*
macros work nicely without copy-pasting the macro body. Instead of doing that, I decided to go for a cursed extraconst
argument, which is then used for the actualconst
keyword. I explicitly mention named macro capture groups as a way of doing this better in the comments.forward_ref_*
macro calls because in some places rustfmt wanted them to be unindented, and in others it was allowed because they were themselves inside of macro bodies. I chose the consistent indenting even though I (personally) think it looks worse.As far as the actual changes go, this renames the
const_ops
feature toconst_arith_ops
(anticipating, e.g.,const_bit_ops
) and constifies the following additional traits:Neg
AddAssign
SubAssign
MulAssign
DivAssign
RemAssign
In terms of constified implementations of these traits, it adds the reference-forwarded versions of all the arithmetic operators, which are defined by the macros in
library/core/src/internal_macros.rs
. I'm not going to fully enumerate these because we'd be here all day, but sufficed to say, it effectively allows adding an&
to one or both sides of an operator for primitives.Additionally, I constified the implementations for
Wrapping
,Saturating
, andDuration
as well, since all of them forward to already-const-stable methods. Specifically forDuration
, the ref-forwarding is not present.