Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master update #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions examples.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ test "Simple NonOwning interface" {
var fooer = try Fooer.init(&f);
defer fooer.deinit();

expectEqual(@as(usize, 42), fooer.call("foo", .{}));
expectEqual(@as(usize, 43), fooer.call("foo", .{}));
try expectEqual(@as(usize, 42), fooer.call("foo", .{}));
try expectEqual(@as(usize, 43), fooer.call("foo", .{}));
}
};

Expand All @@ -56,7 +56,7 @@ test "Comptime only interface" {
};

comptime var iface = try TestIFace.init(TestType{ .state = 0 });
expectEqual(@as(u8, 42), iface.call("foo", .{42}));
try expectEqual(@as(u8, 42), iface.call("foo", .{42}));
}

test "Owning interface with optional function and a non-method function" {
Expand All @@ -65,7 +65,7 @@ test "Owning interface with optional function and a non-method function" {
const TestOwningIface = Interface(struct {
someFn: ?fn (*const SelfType, usize, usize) usize,
otherFn: fn (*SelfType, usize) anyerror!void,
thirdFn: fn(usize) usize,
thirdFn: fn (usize) usize,
}, interface.Storage.Owning);

const TestStruct = struct {
Expand All @@ -92,8 +92,8 @@ test "Owning interface with optional function and a non-method function" {
defer iface_instance.deinit();

try iface_instance.call("otherFn", .{100});
expectEqual(@as(usize, 42), iface_instance.call("someFn", .{ 0, 42 }).?);
expectEqual(@as(usize, 101), iface_instance.call("thirdFn", .{ 100 }));
try expectEqual(@as(usize, 42), iface_instance.call("someFn", .{ 0, 42 }).?);
try expectEqual(@as(usize, 101), iface_instance.call("thirdFn", .{100}));
}
};

Expand All @@ -118,7 +118,7 @@ test "Interface with virtual async function implemented by an async function" {
self.frame = @frame();
}
self.state += 1;
suspend;
suspend {}
self.state += 1;
}
};
Expand All @@ -127,11 +127,11 @@ test "Interface with virtual async function implemented by an async function" {
var instance = try AsyncIFace.init(&i);
_ = async instance.call("foo", .{});

expectEqual(@as(usize, 0), i.state);
try expectEqual(@as(usize, 0), i.state);
resume i.frame;
expectEqual(@as(usize, 1), i.state);
try expectEqual(@as(usize, 1), i.state);
resume i.frame;
expectEqual(@as(usize, 2), i.state);
try expectEqual(@as(usize, 2), i.state);
}

test "Interface with virtual async function implemented by a blocking function" {
Expand All @@ -143,6 +143,7 @@ test "Interface with virtual async function implemented by a blocking function"
const Self = @This();

pub fn readBytes(self: Self, outBuf: []u8) void {
_ = self;
for (outBuf) |*c| {
c.* = 3;
}
Expand All @@ -154,5 +155,5 @@ test "Interface with virtual async function implemented by a blocking function"
var buf: [256]u8 = undefined;
try await async instance.call("readBytes", .{buf[0..]});

expectEqual([_]u8{3} ** 256, buf);
try expectEqual([_]u8{3} ** 256, buf);
}
16 changes: 11 additions & 5 deletions interface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = std.debug.assert;
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;

pub const SelfType = opaque{};
pub const SelfType = opaque {};

fn makeSelfPtr(ptr: anytype) *SelfType {
if (comptime !trait.isSingleItemPtr(@TypeOf(ptr))) {
Expand Down Expand Up @@ -65,7 +65,9 @@ pub const Storage = struct {
return self.erased_ptr;
}

pub fn deinit(comptime self: Comptime) void {}
pub fn deinit(comptime self: Comptime) void {
_ = self;
}
};

pub const NonOwning = struct {
Expand All @@ -88,7 +90,9 @@ pub const Storage = struct {
return self.erased_ptr;
}

pub fn deinit(self: NonOwning) void {}
pub fn deinit(self: NonOwning) void {
_ = self;
}
};

pub const Owning = struct {
Expand Down Expand Up @@ -143,7 +147,7 @@ pub const Storage = struct {
.mem = undefined,
};
if (ImplSize > 0) {
std.mem.copy(u8, self.mem[0..], @ptrCast([*]const u8, &args[0])[0..ImplSize]);
std.mem.copy(u8, self.mem[0..], @ptrCast([*]const u8, &value)[0..ImplSize]);
}

return TInterface{
Expand All @@ -158,7 +162,9 @@ pub const Storage = struct {
return makeSelfPtr(&self.mem[0]);
}

pub fn deinit(self: Self) void {}
pub fn deinit(self: Self) void {
_ = self;
}
};
}

Expand Down