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

hex_color!() macro is not const #5160

Open
Ora9 opened this issue Sep 24, 2024 · 1 comment
Open

hex_color!() macro is not const #5160

Ora9 opened this issue Sep 24, 2024 · 1 comment
Labels
bug Something is broken docs and examples Improving and adding documentation and examples ecolor

Comments

@Ora9
Copy link

Ora9 commented Sep 24, 2024

It would be nice if hex_color!() macro would be const to be used in constant declaration and to define colors at compile time

Btw, the doc says that it's the case on Color32::from_hex method :
« To parse hex colors at compile-time (e.g. for use in const contexts) use the macro crate::hex_color! instead. »

But when trying on a test project with the latest egui git master, i get this error :

error[E0015]: cannot call non-const fn `Color32::from_rgba_unmultiplied` in constants
 --> src/main.rs:1:30
  |
1 | const TEST: egui::Color32 = egui::hex_color!("#ff0202");
  |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: calls in constants are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `egui::hex_color` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0015`.

This is related to #1596, where this comment was saying that it wasn't possible at the time to make the macro const due to floating-point conversions. And that this could be soon fixed
But this was in 2022, and looking at the code (here and here there are some functions that are not const yet (linear_f32_from_gamma_u8 and friends)
I'm quite new to rust, but is it still not possible to make the macro const ? That would be really handy

@Ora9 Ora9 added the bug Something is broken label Sep 24, 2024
@YgorSouza
Copy link
Contributor

Const floating-point arithmetic is going to be stabilized in Rust 1.82, which comes out in 3 weeks. But egui is still targeting 1.76 for now, and likely won't switch immediately. When it does switch, I suppose a const version of from_rgba_unmultiplied could be written to make the macro const. The current implementation cannot be made const because it uses a lookup table to improve performance, and that can only be initialized at runtime.

Until then, maybe the documentation could be updated to reflect the fact that the macro is not actually const, but only has the advantage of generating a compilation error if the string literal is invalid.

@emilk emilk added the docs and examples Improving and adding documentation and examples label Sep 25, 2024
emilk pushed a commit that referenced this issue Sep 26, 2024
It cannot be made const with the current version of Rust, and that is
counterintuitive since it does compile-time checks, so we make that
clear in the documentation. It might be possible to make it const once
MSRV is bumped to 1.82.

* See <#5160>
* [x] I have followed the instructions in the PR template
hacknus pushed a commit to hacknus/egui that referenced this issue Oct 30, 2024
It cannot be made const with the current version of Rust, and that is
counterintuitive since it does compile-time checks, so we make that
clear in the documentation. It might be possible to make it const once
MSRV is bumped to 1.82.

* See <emilk#5160>
* [x] I have followed the instructions in the PR template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken docs and examples Improving and adding documentation and examples ecolor
Projects
None yet
Development

No branches or pull requests

4 participants