Skip to content

Commit 566d775

Browse files
committed
refactor: remove uses of std.mem.span
This can be used by the caller in cases where the length is needed
1 parent db41f75 commit 566d775

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/tests.zig

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const expectEqualStrings = testing.expectEqualStrings;
1717
const expectError = testing.expectError;
1818
const panic = std.debug.panic;
1919

20+
fn expectEqualStringsSentinel(expected: []const u8, actual: [*:0]const u8) !void {
21+
return expectEqualStrings(expected, std.mem.span(actual));
22+
}
23+
2024
// until issue #1717 we need to use the struct workaround
2125
const add = struct {
2226
fn addInner(l: *Lua) i32 {
@@ -285,7 +289,7 @@ test "type of and getting values" {
285289
try expect(lua.isString(12));
286290
try expect(lua.isBoolean(13));
287291

288-
try expectEqualStrings("hello world 10", try lua.toBytes(12));
292+
try expectEqualStrings("hello world 10", std.mem.span(try lua.toString(12)));
289293

290294
// the created thread should equal the main thread (but created thread has no allocator ref)
291295
try expectEqual(lua.state, (try lua.toThread(7)).state);
@@ -299,16 +303,16 @@ test "typenames" {
299303
var lua = try Lua.init(testing.allocator);
300304
defer lua.deinit();
301305

302-
try expectEqualStrings("no value", lua.typeName(.none));
303-
try expectEqualStrings("nil", lua.typeName(.nil));
304-
try expectEqualStrings("boolean", lua.typeName(.boolean));
305-
try expectEqualStrings("userdata", lua.typeName(.light_userdata));
306-
try expectEqualStrings("number", lua.typeName(.number));
307-
try expectEqualStrings("string", lua.typeName(.string));
308-
try expectEqualStrings("table", lua.typeName(.table));
309-
try expectEqualStrings("function", lua.typeName(.function));
310-
try expectEqualStrings("userdata", lua.typeName(.userdata));
311-
try expectEqualStrings("thread", lua.typeName(.thread));
306+
try expectEqualStringsSentinel("no value", lua.typeName(.none));
307+
try expectEqualStringsSentinel("nil", lua.typeName(.nil));
308+
try expectEqualStringsSentinel("boolean", lua.typeName(.boolean));
309+
try expectEqualStringsSentinel("userdata", lua.typeName(.light_userdata));
310+
try expectEqualStringsSentinel("number", lua.typeName(.number));
311+
try expectEqualStringsSentinel("string", lua.typeName(.string));
312+
try expectEqualStringsSentinel("table", lua.typeName(.table));
313+
try expectEqualStringsSentinel("function", lua.typeName(.function));
314+
try expectEqualStringsSentinel("userdata", lua.typeName(.userdata));
315+
try expectEqualStringsSentinel("thread", lua.typeName(.thread));
312316
}
313317

314318
test "executing string contents" {

src/ziglua.zig

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,8 @@ pub const Lua = struct {
986986
/// If the value was a number the actual value in the stack will be changed to a string
987987
pub fn toString(lua: *Lua, index: i32) ![*:0]const u8 {
988988
var length: usize = undefined;
989-
if (c.lua_tolstring(lua.state, index, &length)) |str| {
990-
return std.mem.span(str);
991-
} else return Error.Fail;
989+
if (c.lua_tolstring(lua.state, index, &length)) |str| return str;
990+
return Error.Fail;
992991
}
993992

994993
/// Converts the value at the given `index` to a Lua thread (wrapped with a `Lua` struct)
@@ -1015,8 +1014,8 @@ pub const Lua = struct {
10151014
}
10161015

10171016
/// Returns the name of the given `LuaType` as a null-terminated slice
1018-
pub fn typeName(lua: *Lua, t: LuaType) [:0]const u8 {
1019-
return std.mem.span(c.lua_typename(lua.state, @enumToInt(t)));
1017+
pub fn typeName(lua: *Lua, t: LuaType) [*:0]const u8 {
1018+
return c.lua_typename(lua.state, @enumToInt(t));
10201019
}
10211020

10221021
/// Returns the pseudo-index that represents the `i`th upvalue of the running function

0 commit comments

Comments
 (0)