Skip to content

Commit 59006a2

Browse files
committed
Remove ziglua.opaqueCast
This was added back when @ptrCast() and @alignCast() both required a type parameter and did not do RLS. Now it is just easier to use @ptrCast() and @alignCast() instead of using a wrapper function.
1 parent 95245a9 commit 59006a2

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/lib.zig

+9-15
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ pub const Lua = struct {
592592
// just like malloc() returns a pointer "which is suitably aligned for any built-in type",
593593
// the memory allocated by this function should also be aligned for any type that Lua may
594594
// desire to allocate. use the largest alignment for the target
595-
const allocator_ptr = opaqueCast(Allocator, data.?);
595+
const allocator_ptr: *Allocator = @ptrCast(@alignCast(data.?));
596596

597597
if (@as(?[*]align(alignment) u8, @ptrCast(@alignCast(ptr)))) |prev_ptr| {
598598
const prev_slice = prev_ptr[0..osize];
@@ -1266,7 +1266,7 @@ pub const Lua = struct {
12661266
// safe to .? because this function throws a Lua error on out of memory
12671267
// so the returned pointer should never be null
12681268
const ptr = c.lua_newuserdata(lua.state, @sizeOf(T)).?;
1269-
return opaqueCast(T, ptr);
1269+
return @ptrCast(@alignCast(ptr));
12701270
}
12711271

12721272
/// This function allocates a new userdata of the given type with user_values associated Lua values.
@@ -1276,7 +1276,7 @@ pub const Lua = struct {
12761276
// safe to .? because this function throws a Lua error on out of memory
12771277
// so the returned pointer should never be null
12781278
const ptr = c.lua_newuserdatauv(lua.state, @sizeOf(T), user_values).?;
1279-
return opaqueCast(T, ptr);
1279+
return @ptrCast(@alignCast(ptr));
12801280
}
12811281

12821282
pub const newUserdata = switch (lang) {
@@ -1313,7 +1313,7 @@ pub const Lua = struct {
13131313
// safe to .? because this function throws a Lua error on out of memory
13141314
// so the returned pointer should never be null
13151315
const ptr = c.lua_newuserdatatagged(lua.state, @sizeOf(T), tag).?;
1316-
return opaqueCast(T, ptr);
1316+
return @ptrCast(@alignCast(ptr));
13171317
}
13181318

13191319
/// Returns the tag of a userdata at the given index
@@ -1334,7 +1334,7 @@ pub const Lua = struct {
13341334
// safe to .? because this function throws a Lua error on out of memory
13351335
// so the returned pointer should never be null
13361336
const ptr = c.lua_newuserdatadtor(lua.state, @sizeOf(T), @ptrCast(dtor_fn)).?;
1337-
return opaqueCast(T, ptr);
1337+
return @ptrCast(@alignCast(ptr));
13381338
}
13391339

13401340
/// Pops a key from the stack, and pushes a key-value pair from the table at the given index
@@ -1987,7 +1987,7 @@ pub const Lua = struct {
19871987
/// Returns an error if the value is not a userdata.
19881988
/// See https://www.lua.org/manual/5.4/manual.html#lua_touserdata
19891989
pub fn toUserdata(lua: *Lua, comptime T: type, index: i32) !*T {
1990-
if (c.lua_touserdata(lua.state, index)) |ptr| return opaqueCast(T, ptr);
1990+
if (c.lua_touserdata(lua.state, index)) |ptr| return @ptrCast(@alignCast(ptr));
19911991
return error.Fail;
19921992
}
19931993

@@ -2007,7 +2007,7 @@ pub const Lua = struct {
20072007
}
20082008

20092009
pub fn toUserdataTagged(lua: *Lua, comptime T: type, index: i32, tag: i32) !*T {
2010-
if (c.lua_touserdatatagged(lua.state, index, tag)) |ptr| return opaqueCast(T, ptr);
2010+
if (c.lua_touserdatatagged(lua.state, index, tag)) |ptr| return @ptrCast(@alignCast(ptr));
20112011
return error.Fail;
20122012
}
20132013

@@ -2467,7 +2467,7 @@ pub const Lua = struct {
24672467
/// See https://www.lua.org/manual/5.4/manual.html#luaL_checkudata
24682468
pub fn checkUserdata(lua: *Lua, comptime T: type, arg: i32, name: [:0]const u8) *T {
24692469
// the returned pointer will not be null
2470-
return opaqueCast(T, c.luaL_checkudata(lua.state, arg, name.ptr).?);
2470+
return @ptrCast(@alignCast(c.luaL_checkudata(lua.state, arg, name.ptr).?));
24712471
}
24722472

24732473
/// Checks whether the function argument `arg` is a userdata of the type `name`
@@ -2858,7 +2858,7 @@ pub const Lua = struct {
28582858
/// See https://www.lua.org/manual/5.4/manual.html#luaL_testudata
28592859
pub fn testUserdata(lua: *Lua, comptime T: type, arg: i32, name: [:0]const u8) !*T {
28602860
if (c.luaL_testudata(lua.state, arg, name.ptr)) |ptr| {
2861-
return opaqueCast(T, ptr);
2861+
return @ptrCast(@alignCast(ptr));
28622862
} else return error.Fail;
28632863
}
28642864

@@ -3585,12 +3585,6 @@ pub const Buffer = struct {
35853585

35863586
// Helper functions to make the ziglua API easier to use
35873587

3588-
/// Casts the opaque pointer to a pointer of the given type with the proper alignment
3589-
/// Useful for casting pointers from the Lua API like userdata or other data
3590-
pub inline fn opaqueCast(comptime T: type, ptr: *anyopaque) *T {
3591-
return @ptrCast(@alignCast(ptr));
3592-
}
3593-
35943588
pub const ZigFn = fn (lua: *Lua) i32;
35953589
pub const ZigHookFn = fn (lua: *Lua, event: Event, info: *DebugInfo) void;
35963590
pub const ZigContFn = fn (lua: *Lua, status: Status, ctx: Context) i32;

src/tests.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ test "dump and load" {
940940
const writer = struct {
941941
fn inner(l: *Lua, buf: []const u8, data: *anyopaque) bool {
942942
_ = l;
943-
var arr = ziglua.opaqueCast(std.ArrayList(u8), data);
943+
var arr: *std.ArrayList(u8) = @ptrCast(@alignCast(data));
944944
arr.appendSlice(buf) catch return false;
945945
return true;
946946
}
@@ -964,7 +964,7 @@ test "dump and load" {
964964
const reader = struct {
965965
fn inner(l: *Lua, data: *anyopaque) ?[]const u8 {
966966
_ = l;
967-
const arr = ziglua.opaqueCast(std.ArrayList(u8), data);
967+
const arr: *std.ArrayList(u8) = @ptrCast(@alignCast(data));
968968
return arr.items;
969969
}
970970
}.inner;

0 commit comments

Comments
 (0)