-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
std: use decl literals to improve endian ergonomics #23103
base: master
Are you sure you want to change the base?
Conversation
@@ -813,6 +813,9 @@ pub const FloatMode = enum { | |||
pub const Endian = enum { | |||
big, | |||
little, | |||
|
|||
pub const native = builtin.target.cpu.arch.endian(); | |||
pub const foreign: Endian = @enumFromInt(1 - @intFromEnum(native)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure this is only valid as long as mixed endian is never added. Also, translating from an enum to an int, just to go back to an enum feels kinda weird. Why not if (native == .big) .little else .big
Otherwise, I'm a fan I wished for .native
, just this morning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mixed-endian CPUs aren't really a thing, but even if they were, there are more fundamental issues to solve such as what to do about packed struct
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
if (native == .big) .little else .big
Better yet: put that in a function Endian.flip
(or Endian.invert
or something idk) and call it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when would that be necessary outside right here?
Clever. I didn't realize decl literals kinda give you the ability to add enum value aliases. This is a problem for zigwin32 because there are many enums that have value conflicts (multiple names for the same value). Before your PR I was just adding comments for each conflicting value but I've updated them to be added as declarations instead (marlersoft/zigwin32@7b434a4). |
also uses enhancement to deprecate
std.mem.{read,write}PackedInt{Native,Foreign}