Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
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
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
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.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"); const mem = std.mem; const heap = std.heap; const testing = std.testing; pub const ColumnData = struct { slice: []const u8, }; pub const RowData = struct { slice: []ColumnData, }; pub const ColumnSpec = struct { keyspace: ?[]const u8, table: ?[]const u8, name: []const u8, option: OptionID, listset_element_type_option: ?OptionID, }; pub const RowsMetadata = struct { column_specs: []const ColumnSpec, }; pub const OptionID = packed enum(u16) { Int = 0x0009, UUID = 0x000C, Tinyint = 0x0014, List = 0x0020, Set = 0x0022, }; pub const Iterator = struct { const Self = @This(); arena: heap.ArenaAllocator, metadata: RowsMetadata, rows: []const RowData, pos: usize, pub fn deinit(self: Self) void { self.arena.deinit(); } pub fn init(allocator: *mem.Allocator, metadata: RowsMetadata, rows: []const RowData) Self { return Self{ .arena = heap.ArenaAllocator.init(allocator), .metadata = metadata, .rows = rows, .pos = 0, }; } pub fn scan(self: *Self, args: var) !bool { const child = switch (@typeInfo(@TypeOf(args))) { .Pointer => |info| info.child, else => @compileError("Expected a pointer to a tuple or struct, found " ++ @typeName(@TypeOf(args))), }; if (@typeInfo(child) != .Struct) { @compileError("Expected tuple or struct argument, found " ++ @typeName(child) ++ " of type " ++ @tagName(@typeInfo(child))); } if (self.pos >= self.rows.len) { return false; } inline for (@typeInfo(child).Struct.fields) |struct_field, _i| { const i = @as(usize, _i); const field_type_info = @typeInfo(struct_field.field_type); const field_name = struct_field.name; const column_spec = self.metadata.column_specs[i]; const column_data: []const u8 = self.rows[self.pos].slice[i].slice; @field(args, field_name) = try self.readType(column_spec, column_data, struct_field.field_type); } self.pos += 1; return true; } fn readType(self: *Self, column_spec: ColumnSpec, column_data: []const u8, comptime Type: type) !Type { const type_info = @typeInfo(Type); const option = column_spec.option; switch (type_info) { .Int => return try self.readInt(option, column_data, Type), .Pointer => |pointer| switch (pointer.size) { .Slice => { return try self.readSlice(column_spec, column_data, Type); }, else => @compileError("invalid pointer size " ++ @tagName(pointer.size)), }, .Array => return try self.readArray(option, column_data, Type), else => @compileError("field type " ++ @typeName(Type) ++ " not handled yet"), } } fn readInt(self: *Self, option: OptionID, column_data: []const u8, comptime Type: type) !Type { var r: Type = 0; return r; } fn readSlice(self: *Self, column_spec: ColumnSpec, column_data: []const u8, comptime Type: type) !Type { if (column_spec.listset_element_type_option == null) { return error.InvalidColumnSpec; } var slice: Type = undefined; return slice; } }; fn columnSpec(id: OptionID) ColumnSpec { return ColumnSpec{ .keyspace = null, .table = null, .name = "name", .option = id, .listset_element_type_option = null, }; } fn testIteratorScan(allocator: *mem.Allocator, column_specs: []ColumnSpec, data: []const []const u8, row: var) !void { const metadata = RowsMetadata{ .column_specs = column_specs, }; var column_data = std.ArrayList(ColumnData).init(allocator); for (data) |d| { _ = try column_data.append(ColumnData{ .slice = d }); } const row_data = &[_]RowData{ RowData{ .slice = column_data.toOwnedSlice() }, }; var iterator = Iterator.init(allocator, metadata, row_data); testing.expect(try iterator.scan(row)); } pub fn main() anyerror!void { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); const Row = struct { age: u32, set: []u8, }; var row: Row = undefined; var spec1 = columnSpec(.Set); spec1.listset_element_type_option = .Tinyint; var spec2 = columnSpec(.Tinyint); const column_specs = &[_]ColumnSpec{ spec2, spec1 }; const test_data = &[_][]const u8{ "\x00\x00\x20\x00", "\x00\x00\x00\x02\x00\x00\x00\x01\x20\x00\x00\x00\x01\x21", }; try testIteratorScan(&arena.allocator, column_specs, test_data, &row); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree