The ZType library provides a way to serialize and deserialize various data types into a compact binary format. It supports the following types:
- Boolean (
bool) - Integer (
int) - Floating-point (
float) - Arrays (
[]ZType) - Maps (
std.StringHashMap(ZType))
This is WIP and might not be the most optimal way to do this.
To initialize a ZType instance, use the init function. The function takes an allocator and a value of any supported type.
conststd=@import("std");
constZType=@import("path/to/ztype.zig");
pubfnmain() !void {
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
constallocator=gpa.allocator();
constzbool=tryZType.init(allocator, true);
constzint=tryZType.init(allocator, 42);
constzfloat=tryZType.init(allocator, 3.14);
// TODO: needs to be fixedconstlist_of_elems: [3]ZType= .{zbool, zint, zfloat}
constzarray=tryZType.init(allocator, list_of_elems);
// Deinitialize when donezbool.deinit(allocator);
zint.deinit(allocator);
zfloat.deinit(allocator);
zarray.deinit(allocator);
}To free the memory allocated for a ZType instance, use the deinit function.
ztype.deinit(allocator);
To access the original values stored in a ZType instance, use the appropriate getter functions:
getBool()getInt()getFloat()getArray()getMap()
Each getter function returns an error if the type does not match.
const value = try ztype.getInt();
Here is a complete example demonstrating the usage of the ZType library:
conststd=@import("std");
constZType=@import("path/to/ztype.zig");
pubfnmain() !void {
vargpa=std.heap.GeneralPurposeAllocator(.{}){};
constallocator=gpa.allocator();
constzbool=tryZType.init(allocator, true);
deferzbool.deinit(allocator);
constzint=tryZType.init(allocator, 42);
deferzint.deinit(allocator);
constzfloat=tryZType.init(allocator, 3.14);
deferzfloat.deinit(allocator);
constlist_of_elems: [3]ZType= .{zbool, zint, zfloat}
constzarray=tryZType.init(allocator, list_of_elems);
deferzarray.deinit(allocator);
constbool_value=tryzbool.getBool();
constint_value=tryzint.getInt();
constfloat_value=tryzfloat.getFloat();
constarray_value=tryzarray.getArray(allocator);
std.debug.print("Bool: {}\n", .{bool_value});
std.debug.print("Int: {}\n", .{int_value});
std.debug.print("Float: {}\n", .{float_value});
std.debug.print("Array: {}\n", .{array_value});
}