Begin migration of calc functions to methods #3489
Draft
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.
This will supersede functions such as
calc.pow(5, 6)
by5.pow(6)
orint.pow(5, 6)
, for example.The
calc
functions will remain available for the time being, however.Implementation details
Some things still have to be sorted out, but here are the main highlights (currently):
calc
; I'm trying to trim it down as much as possible, but there's also this idea of trying not to depend so much on calc in order to eventually be able to remove as much as possible from it in the future..abs()
methods forlength
,ratio
andfr
as well (currently, I've added it to i64, f64 and angle). But it sounds plausible at first, with length being the only possible problem, given it already has anabs
field and ato_absolute
method.calc.min
andcalc.max
are in a bit of a weird state right now. Those methods accept any comparable values, so it's hard to tell what I should do with it; perhaps we should add methods for each comparable value, but that sounds a bit excessive in principle. For now, I've addedint.min
andfloat.min
which accept anything that compares to numbers.Worth noting that I didn't add any warnings to migrated methods; tell me if that'd be desirable.
Docs
I did not update the docs, only adapted existing docs to the new methods. Some more major docs changes (involving a Ctrl+F search of
calc
across all docstrings) are in order though, but I'm not sure how much of it I should do in this PR instead of separately, if at all.Regarding the
#[scope]
macroConstants
I wanted to move
calc.e
,calc.nan
etc. tofloat.e
,float.nan
etc., but currently thef64Ext
trait generated by#[scope]
does not appear to add the constants correctly.Functions
Of note, I had to append
_
to some method names (e.g.pow_
) because the#[scope]
proc macro (or perhaps#[func]
) kept generating some code which tried to usei64::pow
(or similar) directly, instead ofi64Ext::pow
, thus leading to an error of "unexpected parameters" when the signatures didn't match. I didn't have to add an underscore when the function signatures did match. Note that the underscore isn't visible to the end user (int.pow
works). This might be interesting to somehow fix in the future (though feels out of scope for this PR).As a result,
i64Ext
andf64Ext
were generating methods calledpow__data
(from the{ident}_data
format), triggering anon_snake_case
warning. Since these methods are internal, I've worked around it by simply ignoring the warning for the generated trait, but it's worth mentioning.I also added
pub(super)
to the generatedi64Ext
andf64Ext
traits so their implementations can be reused by each other.Finally, the lack of
self: Spanned<Self>
led me to use the callsite span for errors related toself
on multiple occasions. I think that works for now. (I guess we'll be able to more trivially solve that when Rust implements support for arbitrary receivers.)