Skip to content

Commit f942c1d

Browse files
committed
feat(zig-compat): add compatibility layer for multiple Zig versions
- introduce compat.zig to handle differences in Zig standard library - replace FixedBufferStream with cross-version BufferStream implementation - support Zig 0.14.1, 0.15.1, and master versions in build and CI configurations - update tests to use version-agnostic buffer stream methods - add dynamic module and test creation based on Zig version
1 parent f61843d commit f942c1d

File tree

4 files changed

+270
-192
lines changed

4 files changed

+270
-192
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
os: [ubuntu-latest]
16-
version: [0.14.0, 0.15.1, ""]
16+
version: [0.14.1, 0.15.1, master]
1717
fail-fast: false
1818
runs-on: ${{ matrix.os }}
1919
steps:

build.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ pub fn build(b: *std.Build) void {
2121
.root_source_file = b.path(b.pathJoin(&.{ "src", "test.zig" })),
2222
.target = target,
2323
.optimize = optimize,
24+
}) else if (builtin.zig_version.minor >= 16) b.addTest(.{
25+
.root_module = b.createModule(.{
26+
.root_source_file = b.path(b.pathJoin(&.{ "src", "test.zig" })),
27+
.target = target,
28+
.optimize = optimize,
29+
}),
2430
}) else b.addTest(.{
2531
.root_module = b.addModule("test", .{
2632
.root_source_file = b.path(b.pathJoin(&.{ "src", "test.zig" })),
@@ -39,6 +45,13 @@ fn generateDocs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget)
3945
.root_source_file = b.path(b.pathJoin(&.{ "src", "msgpack.zig" })),
4046
.target = target,
4147
.optimize = optimize,
48+
}) else if (builtin.zig_version.minor >= 16) b.addObject(.{
49+
.name = "zig-msgpack",
50+
.root_module = b.createModule(.{
51+
.root_source_file = b.path(b.pathJoin(&.{ "src", "msgpack.zig" })),
52+
.target = target,
53+
.optimize = optimize,
54+
}),
4255
}) else b.addObject(.{
4356
.name = "zig-msgpack",
4457
.root_module = b.addModule("msgpack", .{

src/compat.zig

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Compatibility layer for different Zig versions
2+
const std = @import("std");
3+
const builtin = @import("builtin");
4+
const current_zig = builtin.zig_version;
5+
6+
// BufferStream implementation for Zig 0.16+
7+
// This mimics the behavior of the old FixedBufferStream
8+
pub const BufferStream = if (current_zig.minor >= 16) struct {
9+
buffer: []u8,
10+
pos: usize,
11+
12+
const Self = @This();
13+
14+
pub const WriteError = error{NoSpaceLeft};
15+
pub const ReadError = error{EndOfStream};
16+
17+
pub fn init(buffer: []u8) Self {
18+
return .{
19+
.buffer = buffer,
20+
.pos = 0,
21+
};
22+
}
23+
24+
pub fn write(self: *Self, bytes: []const u8) WriteError!usize {
25+
const available = self.buffer.len - self.pos;
26+
if (bytes.len > available) return error.NoSpaceLeft;
27+
@memcpy(self.buffer[self.pos..][0..bytes.len], bytes);
28+
self.pos += bytes.len;
29+
return bytes.len;
30+
}
31+
32+
pub fn read(self: *Self, dest: []u8) ReadError!usize {
33+
// Read from current position in buffer
34+
const available = self.buffer.len - self.pos;
35+
if (available == 0) return 0;
36+
37+
const to_read = @min(dest.len, available);
38+
@memcpy(dest[0..to_read], self.buffer[self.pos..][0..to_read]);
39+
self.pos += to_read;
40+
return to_read;
41+
}
42+
43+
pub fn reset(self: *Self) void {
44+
self.pos = 0;
45+
}
46+
47+
pub fn seekTo(self: *Self, pos: usize) !void {
48+
self.pos = pos;
49+
}
50+
51+
pub fn getPos(self: Self) usize {
52+
return self.pos;
53+
}
54+
55+
pub fn getEndPos(self: Self) usize {
56+
return self.pos;
57+
}
58+
} else std.io.FixedBufferStream([]u8);
59+
60+
pub const fixedBufferStream = if (current_zig.minor >= 16)
61+
BufferStream.init
62+
else
63+
std.io.fixedBufferStream;

0 commit comments

Comments
 (0)