Skip to content

Instantly share code, notes, and snippets.

View Durobot's full-sized avatar

Alexei Kireev Durobot

  • Chisinau, Republic of Moldova
View GitHub Profile
@Durobot
Durobot / zon_enumerate_children.zig
Last active June 21, 2024 09:26
Quick and dirty enumerator of field names in ZON structs
const std = @import("std");
const zon_fld_path_len_limit = 20;
const ZonGetFieldsError = error
{
PathLimitReached,
PathElementNotStruct,
NotFound,
BadSeparatorPosition,
@Durobot
Durobot / test_ret_anon_struct_4.zig
Created February 19, 2024 11:25
(This version adds support for enum printing, and type shortening) An example of a function (template) that modifies certain fields of arbitrary struct given to it, returning an anonymous struct indicating which fields were modified, in Zig. Also pretty-print functions for structs (with nested structs and arrays).
const std = @import("std");
pub fn main() void
{
const Eggs = struct
{
const Ham = struct
{
const Milk = struct { cow: u8, soy: i8, almond: bool };
@Durobot
Durobot / test_ret_anon_struct_3.zig
Last active February 13, 2024 11:58
(This version supports nested structs and arrays) An example of a function (template) that modifies certain fields of arbitrary struct given to it, returning an anonymous struct indicating which fields were modified, in Zig. Also pretty-print functions for structs (with nested structs and arrays).
const std = @import("std");
pub fn main() void
{
const Eggs = struct
{
const Ham = struct
{
const Milk = struct { cow: u8, soy: i8, almond: bool };
@Durobot
Durobot / print_struct.zig
Last active February 21, 2024 15:08
Pretty-print structs, call printStruct(my_struct, <true|false>, 0). Also you can do std.json.fmt(my_struct, .{ .whitespace = .indent_4 }), as it turns out
fn printStruct(s: anytype, shorten_types: bool, indent: comptime_int) void
{
const s_type_info = @typeInfo(@TypeOf(s));
if (s_type_info != .Struct)
@compileError("fn printStruct: `s` is " ++ @typeName(s) ++ " , expected a struct");
const ind_str = " " ** indent;
const ind_str_2 = " " ** (indent + 1);
std.debug.print("{s}{{\n", .{ ind_str });
var name_buf = [_]u8 { 0 } ** 100;
@Durobot
Durobot / test_ret_anon_struct_2.zig
Created February 10, 2024 08:02
(This version supports nested structs) An example of a function (template) that modifies certain fields of arbitrary struct given to it, returning an anonymous struct indicating which fields were modified, in Zig
const std = @import("std");
pub fn main() void
{
const Eggs = struct
{
const Ham = struct
{
const Milk = struct { cow: u8, soy: i8, almond: bool };
@Durobot
Durobot / test_ret_anon_struct.zig
Created February 7, 2024 20:23
An example of a function (template) that modifies certain fields of arbitrary struct given to it, returning an anonymous struct indicating which fields were modified, in Zig
const std = @import("std");
pub fn main() void
{
const Eggs = struct
{
comptime x: u8 = 123, // kinda like const
tea: u32,
lemon: f32,
@Durobot
Durobot / std_log.md
Created December 14, 2023 07:29 — forked from kassane/std_log.md
Quick overview of Zig's `std.log`

A simple overview of Zig's std.log

Logging functionality that supports:

  • If a log message should be printed is determined at comptime, meaning zero overhead for unprinted messages (so just leave the code peppered with debug logs, but when it makes sense scope them; so downstream users can filter them out)
  • Scoped log messages
  • Different log levels per scope
  • Overrideable log output (write to file, database, etc.)
  • All the standard std.fmt formatting magic

Basic Usage:

@Durobot
Durobot / gist:c59bc2578d22696b4ae2ab144faadb79
Created August 9, 2023 09:32
`zig build` errors when trying to build minimal cimgui example ported to Zig, removed external quotation marks
$ zig build
zig build-exe glfw-cimgui-00 Debug native: error: error(compilation): clang failed with stderr: In file included from <built-in>:458:
<command line>:9:32: warning: missing terminating '"' character [-Winvalid-pp-token]
In file included from /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp:110:
/home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.h:31:1: error: expected unqualified-id
<command line>:9:31: note: expanded from macro 'IMGUI_IMPL_API'
In file included from /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp:110:
/home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.h:32:1: error: expected unqualified-id
<command line>:9:31: note: expanded from macro 'IMGUI_IMPL_API'
In file included from /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp:110:
@Durobot
Durobot / gist:07e57310fbf96f845d87402775f861c1
Last active August 9, 2023 08:38
`zig build` errors when trying to build minimal cimgui example ported to Zig
$ zig build
zig build-exe glfw-cimgui-00 Debug native: error: error(compilation): clang failed with stderr: In file included from /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp:110:
/home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.h:31:1: error: expected unqualified-id
<command line>:9:24: note: expanded from macro 'IMGUI_IMPL_API'
In file included from /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp:110:
/home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.h:32:1: error: expected unqualified-id
<command line>:9:24: note: expanded from macro 'IMGUI_IMPL_API'
In file included from /home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.cpp:110:
/home/archie/projects/cimgui/imgui/backends/imgui_impl_opengl3.h:33:1: error: expected unqualified-id
<command line>:9:24: note: expanded from macro 'IMGUI_IMPL_API'
@Durobot
Durobot / build_zig.sh
Created August 9, 2023 08:09
Minimal Zig cimgui example compilation script
#!/bin/sh
BUILD_PATH='./build'
CIMGUI_PATH='../../cimgui'
CIMGUI_GEN_OUT_PATH="${CIMGUI_PATH}/generator/output" # cimgui_impl.h lives here
IMGUI_PATH="${CIMGUI_PATH}/imgui"
BACKEND_PATH="${IMGUI_PATH}/backends"
DEFINE_FLAGS_1=(-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS)
DEFINE_FLAGS_2=(-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS=1 -DIMGUI_IMPL_API="extern \"C\" " -DIMGUI_IMPL_OPENGL_LOADER_GL3W -Dcimgui_EXPORTS)