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

color: Replace color components union with property functions #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

0xEAB
Copy link
Contributor

@0xEAB 0xEAB commented Dec 17, 2023

In its current form, this is a breaking change suitable for arsd 12 or a later major release.

getter+setter vs. union

Color.components is turned into ubyte[4].
Unlike an anonymous union, this works with CTFE as well.

r, g, b, a and asUint are turned into getter + setter functions.

A sufficiently good optimizing compiler will generate the same code for these as it would previously have with the union. (Note that this cannot be confirmed with DMD’s built-in profiler as it’s implementation will poison any getter/setter functions.)

alias this

The alias this stems from my codebase which did use it to simplify certain implementation details.
I’m not sure whether it’s a good thing or even worth it.

/+ code sample +/
foreach (immutable idx, ref component; pixelDestination) {}
/+ vs +/
foreach (immutable idx, ref component; pixelDestination.components) {}

@0xEAB
Copy link
Contributor Author

0xEAB commented Dec 17, 2023

I’d like to hear your opinion on this, @adamdruppe.

My motivation behind this change is to get arsd to act more or less like a drop-in replacement and make it more convenient to use.
While I tried to make it work like the previous implementation for most cases, I don’t have any data on this change’s actual impact on backwards compatibility, to be honest.

// Unlike an anonymous union, this works with CTFE as well.
@safe pure nothrow @nogc {
// individual rgb components
pragma(inline, true) ref inout(ubyte) r() inout scope return { return components[0]; } /// red
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i doubt the pragma is worth it, but since you return ref here anyway, no need for a setter; the ref return can be assigned.

As long as this compiles on my oldest supported compiler i think im ok with it, i gotta find where i used these but i don't think anything would break here, it should work fine everywhere and compile to the same thing.

Copy link
Contributor Author

@0xEAB 0xEAB Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve added the setters specifically after something from arsd would no longer compile

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure how to reproduce this, however

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve commented out the setters.

color.d Outdated Show resolved Hide resolved
@0xEAB
Copy link
Contributor Author

0xEAB commented Dec 30, 2023

I’ve updated this patch.
Not sure what to do with it. No longer sure whether it adds value.

@0xEAB 0xEAB marked this pull request as ready for review April 19, 2024 20:05
@0xEAB
Copy link
Contributor Author

0xEAB commented May 5, 2024

No longer sure whether it adds value.

Well, it removes an anonymous union. Could be an advantage.

@0xEAB
Copy link
Contributor Author

0xEAB commented May 5, 2024

@adamdruppe
Any comments/suggestions? :)

@adamdruppe
Copy link
Owner

i kinda forgot about this one lol lemme look at it again

@adamdruppe
Copy link
Owner

well looking it back over it looks ok enough. i can go ahead and merge if you wanna

@0xEAB
Copy link
Contributor Author

0xEAB commented May 23, 2024

Any suggestions to improve it?

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

2 participants