mirror of
https://github.com/fairyglade/ly.git
synced 2026-11-05 09:41:43 +00:00
Now compiles! (Tested with Zig 0.17.0-dev.2375+d8aab4878) ## What are the changes about? Updates dependencies & code for Zig 0.17.0 compatibility. ## What existing issue(s) does this resolve? N/A ## Pre-requisites - [x] I have tested & confirmed the changes work locally - [x] I have read and fully adhere to the rules set in the contributing guidelines found in `CONTRIBUTING.md` Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/1028
53 lines
1.5 KiB
Zig
53 lines
1.5 KiB
Zig
const std = @import("std");
|
|
|
|
const ErrInt = @Int(.unsigned, @bitSizeOf(anyerror));
|
|
const PaddingInt = @Int(.unsigned, 8 - (@bitSizeOf(ErrInt) + @bitSizeOf(bool)) % 8);
|
|
|
|
const ErrorHandler = packed struct {
|
|
has_error: bool = false,
|
|
err_int: ErrInt = 0,
|
|
padding: PaddingInt = 0,
|
|
};
|
|
|
|
const SharedError = @This();
|
|
|
|
data: []align(std.heap.page_size_min) u8,
|
|
write_error_event_fn: ?*const fn (anyerror, *anyopaque) anyerror!void,
|
|
ctx: ?*anyopaque,
|
|
|
|
pub fn init(
|
|
write_error_event_fn: ?*const fn (anyerror, *anyopaque) anyerror!void,
|
|
ctx: ?*anyopaque,
|
|
) !SharedError {
|
|
const data = try std.posix.mmap(null, @sizeOf(ErrorHandler), .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED, .ANONYMOUS = true }, -1, 0);
|
|
|
|
return .{
|
|
.data = data,
|
|
.write_error_event_fn = write_error_event_fn,
|
|
.ctx = ctx,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *SharedError) void {
|
|
std.posix.munmap(self.data);
|
|
}
|
|
|
|
pub fn writeError(self: SharedError, err: anyerror) void {
|
|
var writer: std.Io.Writer = .fixed(self.data);
|
|
writer.writeStruct(ErrorHandler{ .has_error = true, .err_int = @intFromError(err) }, .native) catch {};
|
|
|
|
if (self.write_error_event_fn) |write_error_event_fn| {
|
|
@call(.auto, write_error_event_fn, .{ err, self.ctx.? }) catch {};
|
|
}
|
|
}
|
|
|
|
pub fn readError(self: SharedError) ?anyerror {
|
|
var reader: std.Io.Reader = .fixed(self.data);
|
|
const err_handler = try reader.takeStruct(ErrorHandler, .native);
|
|
|
|
if (err_handler.has_error)
|
|
return @errorFromInt(err_handler.err_int);
|
|
|
|
return null;
|
|
}
|