From ab52fb26e947e7aa5f79868173442fe1a75aa15f Mon Sep 17 00:00:00 2001 From: Loris Olsem Date: Sun, 11 Oct 2020 14:36:15 +0200 Subject: [PATCH] Fix undefined variable in Storage.Inline --- examples.zig | 32 ++++++++++++++++++++++++++++++++ interface.zig | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/examples.zig b/examples.zig index 94340c9..b54ed33 100644 --- a/examples.zig +++ b/examples.zig @@ -39,6 +39,38 @@ test "Simple NonOwning interface" { comptime try NonOwningTest.run(); } +test "Simple Inline interface with state" { + const InlineTest = struct { + fn run() !void { + const Fooer = Interface(struct { + foo: fn (*SelfType) usize, + }, interface.Storage.Inline(8)); + + const TestFooer = struct { + const Self = @This(); + + state: usize, + + pub fn foo(self: *Self) usize { + const tmp = self.state; + self.state += 1; + return tmp; + } + }; + + var f = TestFooer{ .state = 42 }; + var fooer = try Fooer.init(&f); + defer fooer.deinit(); + + expectEqual(@as(usize, 42), fooer.call("foo", .{})); + expectEqual(@as(usize, 43), fooer.call("foo", .{})); + } + }; + + try InlineTest.run(); +// comptime try InlineTest.run(); // Hits "This is a bug in the Zig compiler." on windows 0.6.0+53c63bdb7 (2020-10-10) +} + test "Comptime only interface" { // return error.SkipZigTest; const TestIFace = Interface(struct { diff --git a/interface.zig b/interface.zig index cf6616e..2757681 100644 --- a/interface.zig +++ b/interface.zig @@ -143,7 +143,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..], std.mem.asBytes(value)[0..ImplSize]); } return TInterface{