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

Fixed #33 #35

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

Fixed #33 #35

wants to merge 1 commit into from

Conversation

yaroslavche
Copy link
Owner

No description provided.

@yaroslavche
Copy link
Owner Author

yaroslavche commented Sep 1, 2024

@DaanBiesterbos WDYT?

Just to note, this is a concept implementation. We still need to verify how it behaves with an empty enum, and it might be worth restricting such a case. Additionally, it might make sense to create a new class for this specific functionality. Documentation will need to be updated accordingly. I’ll take care of these steps, but I'm interested in your thoughts on the current implementation.

@yaroslavche
Copy link
Owner Author

I've found an issue when handling int-backed enums with non-sequential or random bit values. Specifically, the previous implementation assumed that enum values were sequential, which caused incorrect behavior when using enums like:

enum RandomBackedInt: int {
    case Bit1 = 1 << 0;
    case Bit2 = 1 << 1;
    case Bit3 = 1 << 3;
}

In this scenario, the bitmask operations would fail because the enum values were not properly mapped. Additionally, we need to add a check to ensure that the IntBackedEnum uses correct bit values. I'll fix this.

* @throws UnknownEnumException
*/
public function __construct(
private readonly string $enum,
int $mask = 0,
bool $isIntBacked = false,

Choose a reason for hiding this comment

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

I noticed we instantiate the class in static functions. (like the all() method for example). We might run into consistency issues? It might be worth considering to treat all int backed enums as "isIntBacked". But that change would not be backward compatible, as people might be using int backed enum with the existing implementation.

That's why I was thinking about a subclass like IntBackedEnumBitmask.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thank you for your thoughts. You’re right that treating all int-backed enums as "isIntBacked" could lead to consistency issues and wouldn't be backward compatible. That's why the current approach includes the boolean parameter to ensure flexibility. However, creating a subclass like IntBackedEnumBitmask could be a cleaner solution in the long term.

if (!is_int($case->value)) {
throw new InvalidEnumException('Enum must be an int-backed enum with integer values');
}
}
$this->map[$case->name] = Bits::indexToBit($index);
}
$this->bitmask = new BitMask($mask, count($this->enum::cases()) - 1);
Copy link

@DaanBiesterbos DaanBiesterbos Sep 2, 2024

Choose a reason for hiding this comment

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

Not sure what the difference is, but I had an issue here with the most significant bit in my implementation. Not sure exactly why, but removing the second argument solved the issue for me. Perhaps you can speculate what could be the cause of this?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Regarding the issue with the most significant bit, it’s possible that the problem stems from how the second argument (the mask) interacts with the bit positions in your enum. If the bitmask value overlaps or conflicts with the most significant bit, removing the second argument might have bypassed the issue. I’ll investigate further to pinpoint the exact cause.

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.

2 participants