Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conversion of primitive integer types in const context #226

Closed
wants to merge 2 commits into from

Conversation

U007D
Copy link

@U007D U007D commented Oct 23, 2021

Support primitive integer conversion via new const_conversion feature or when building with a version of the compiler where const_trait_impl has stabilized.

@U007D U007D changed the title const conversion Support conversion of primitive integer types in const context Oct 24, 2021
ctrlcctrlv added a commit to MFEK/num-traits.rlib that referenced this pull request Jan 13, 2022
Combines these commits—

* const conversion support for primitive integer types

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* fix clippy errors

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* Prevent a warning when running tests and not `no_std`

Author: Fredrick Brennan <copypaste@kittens.ph>

Co-authored-by: Fredrick Brennan <copypaste@kittens.ph>
Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>
@ctrlcctrlv
Copy link
Contributor

ctrlcctrlv commented Jan 13, 2022

(Merged into MFEK@8271720 due to #189 (comment). You likely don't care and the maintainer does plan to return, just explaining the merge message above.)

@ctrlcctrlv
Copy link
Contributor

Oh, here's something you might indeed care about though @U007D—you probably want to add MFEK@54b6a41 to your branch because otherwise your PR adds a warning.

@petoknm
Copy link

petoknm commented Jan 15, 2022

It would be nice to have const FromPrimitive as well, but I can't get it to work.

I tried to just add const to the FromPrimitive impl in the macro expansion. Any idea why it goes wrong?
Here's the error for u32.

Compile error
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
   --> num-traits.rlib-c0584f9c87376f82/8271720/src/cast.rs:625:17
    |
563 | / macro_rules! impl_from_primitive {
564 | |     ($T:ty, $to_ty:ident) => {
565 | |         #[allow(deprecated)]
566 | |         impl const FromPrimitive for $T {
...   |
625 | |                 n.$to_ty()
    | |                 ^^^^^^^^^^
...   |
628 | |     };
629 | | }
    | |_- in this expansion of `impl_from_primitive!`
...
641 |   impl_from_primitive!(u32, to_u32);
    |   --------------------------------- in this macro invocation

As far as I can see, it should work, because the <u32 as ToPrimitive>::to_u32() is a const fn.
Feature const_conversion is enabled.

@cuviper
Copy link
Member

cuviper commented Jan 15, 2022

Hi -- I'm not inclined to merge any unstable features in the API, especially because I don't want the maintenance burden if that syntax changes at all. So I'm going to close this for now, but you're of course welcome to keep experimenting in a fork, and please feel free to reopen or file a new PR when the language has stabilized this.

@cuviper cuviper closed this Jan 15, 2022
ctrlcctrlv added a commit to MFEK/num-traits.rlib that referenced this pull request Mar 28, 2023
Combines these commits—

* const conversion support for primitive integer types

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* fix clippy errors

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* Prevent a warning when running tests and not `no_std`

Author: Fredrick Brennan <copypaste@kittens.ph>

Co-authored-by: Fredrick Brennan <copypaste@kittens.ph>
Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>
ctrlcctrlv added a commit to MFEK/num-traits.rlib that referenced this pull request Mar 28, 2023
Combines these commits—

* const conversion support for primitive integer types

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* fix clippy errors

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* Prevent a warning when running tests and not `no_std`

Author: Fredrick Brennan <copypaste@kittens.ph>

Co-authored-by: Fredrick Brennan <copypaste@kittens.ph>
Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>
ctrlcctrlv added a commit to MFEK/num-traits.rlib that referenced this pull request Mar 28, 2023
Combines these commits—

* const conversion support for primitive integer types

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* fix clippy errors

Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>

* Prevent a warning when running tests and not `no_std`

Author: Fredrick Brennan <copypaste@kittens.ph>

Co-authored-by: Fredrick Brennan <copypaste@kittens.ph>
Signed-off-by: Fredrick Brennan <copypaste@kittens.ph>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants