Discussion: Upcoming @backingInt() and @fromBackingInt() convenience builtins

I just saw these in the master branch standard library and documentation:

They seem to use a new standard library construct: std.meta.BackingInt()

Here’s an example of a function where they’re already being used: std.log.logEnabled()

While this is nothing that couldn’t have been implemented in userland with some imaginative use of switch(@typeInfo(@TypeOf(arg))), it’s exciting to see new convenience builtins, which will surely be cited in the release notes as “part of the ongoing effort to improve ergonomics for game development”.

I bet a lot more people will be more willing to play around with packed structs and enums with these new builtins in place, particularly stuff like non-exhaustive enums as indices for concepts like Data-Oriented Design.

12 Likes

I already had opportunity to use it.
https://codeberg.org/andrewkraevskii/steam_train/src/commit/e0248bee3cbfce75d25da73f28c1228acf160195/src/wasm4.zig#L62
Previously it would have been written as @as(u32, @bitcast(self))

4 Likes

Having dabbled in embedded Zig recently, this will be a very welcome addition!

1 Like

The commit also mentions:

These builtins are meant to replace the now-deprecated @intFromEnum and
@enumFromInt builtins.

Fantastic that the behavior of these builtins can be generalized to packed structs/bitfields. And even without these new features, @backingInt would still be a better name than @intFromEnum.

2 Likes

Interesting! I’m trying to grasp how to program an Arduino UNO with zig but I can’t even cross-compile the most basic thing so far.

Do you have code I could read somewhere?

Did you try microzig? AFAIK it has AVR/UNO support.

I had a look but the documentation is very scarce, and it looks more like a big framework targeting many platforms - not ideal for a learning experiment. Also I didn’t find anything targeting UNO but maybe I simply missed it.

I found something.

Also you might need latest 0.17-dev to compile avr. It contains some fixes IIRC.

1 Like

Can’t help with Arduino as I’m working with an ARM chip, which just works with Zig 0.16.0. I’ve got this library code published, but it’s very incomplete and there’s no entry point as the only consumer I’ve got at the moment is specific to proprietary hardware under an NDA.

https://codeberg.org/spiffyk/zig-sam-e5x-d5x

1 Like