GitXplorerGitXplorer
g

zcale

public
4 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
d1e3ea37d81b4e12922c8ecdd0aa0af519344016

typo

gggwpez committed 9 months ago
Verified
e24c30f63d17b5030138445a41df60ba8acaeabf

readme

gggwpez committed 9 months ago
Verified
1aedea72c6c3bad4b8c1311c735e0834a7910c2c

RGB

gggwpez committed 9 months ago
Verified
ab1ea3c94f661567f96594c868eb9affb480afe0

Mention Compact

gggwpez committed 9 months ago
Verified
a0b02e762c79d5c636ff8a5104ec339cd4a93804

Init

gggwpez committed 9 months ago

README

The README file for this repository.

Run Tests

zig test src/main_tests.zig

Example

See tests for examples.

const std = @import("std");
const test_allocator = std.testing.allocator;

var buffer = std.ArrayList(u8).init(test_allocator);
defer list.deinit();
var scale = scaleWriter(list.writer());

// Encode a specific type:
scale.writeU32(0x12345678);
// Encode an inferred type:
scale.write(0x12345678);

// Encode a compact integer:
scale.writeCompactU32(0x12345678);
// Encode a compact integer by using the Compact wrapper:
scale.write(Compact{ .U16 = 0x1234 });

// Encode a struct:
const RGB = struct {
	r: u8,
	g: u8,
	b: u8,
};

scale.write(RGB{ .a = 100, .b = 200, .c = 100 });

// Also works for slices, options, enums etc:
scale.write([_]u8{1, 2, 3, 4});
scale.write(@as(?u32, null));

Custom encoder for a struct (or union):

const Struct = struct {
	a: u8,

	// You better hope to not typo this function name/args/visibility ;)
	pub fn scaleEncode(self: *const @This(), writer: anytype) !void {
		try writer.write(123);
		try writer.write(self.a);
		try writer.write(32);
	}
};

scale.write(Struct{ .a = 1 });