Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); fn newIntToEnum( comptime EnumTag: type, tag_int: anytype, ) std.meta.IntToEnumError!EnumTag { const enum_info = @typeInfo(EnumTag).Enum; if (!enum_info.is_exhaustive) { if (std.math.cast(enum_info.tag_type, tag_int)) |tag| { return @as(EnumTag, @enumFromInt(tag)); } return error.InvalidEnumTag; } if (comptime enum_info.tag_type == comptime_int) { inline for (enum_info.fields) |f| { const this_tag_value = @field(EnumTag, f.name); if (tag_int == @intFromEnum(this_tag_value)) { return this_tag_value; } return error.InvalidEnumTag; } } // We don't directly iterate over the fields of EnumTag, as that // would require an inline loop. Instead, we create an array of // values that is comptime-known, but can be iterated at runtime // without requiring an inline loop. This generates better machine // code. const values = comptime blk: { var result: [enum_info.fields.len]enum_info.tag_type = undefined; for (&result, enum_info.fields) |*dst, src| { dst.* = src.value; } break :blk result; }; for (values) |v| { if (tag_int == v) return @enumFromInt(tag_int); } return error.InvalidEnumTag; } const Contiguous = enum(u8) { a = 0, b, c, d, e, f, g, }; const NonContiguous = enum(u8) { a = 1, b = 100, c = 255, d = 120, e = 50, f = 35, g = 2, }; pub fn main() !void { // Random value to prevent the compiler from being smart. const rawValue = std.process.argsAlloc(std.heap.page_allocator) catch unreachable; const value = rawValue[0][2]; std.mem.doNotOptimizeAway( try @call(.never_inline, newIntToEnum, .{ Contiguous, value, }), ); std.mem.doNotOptimizeAway( try @call(.never_inline, std.meta.intToEnum, .{ Contiguous, value, }), ); std.mem.doNotOptimizeAway( try @call(.never_inline, newIntToEnum, .{ NonContiguous, value, }), ); std.mem.doNotOptimizeAway( try @call(.never_inline, std.meta.intToEnum, .{ NonContiguous, value, }), ); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree