Skip to content

Commit 940467d

Browse files
committed
Use c string literal where appropriate
1 parent 272cfdb commit 940467d

File tree

13 files changed

+48
-55
lines changed

13 files changed

+48
-55
lines changed

mlua-sys/src/lua51/compat.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) ->
9090
} else if compat53_findfield(L, objidx, level - 1) != 0 {
9191
// try recursively
9292
lua_remove(L, -2); // remove table (but keep name)
93-
lua_pushliteral(L, ".");
93+
lua_pushliteral(L, c".");
9494
lua_insert(L, -2); // place '.' between the two names
9595
lua_concat(L, 3);
9696
return 1;
@@ -121,13 +121,13 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, ar: *mut lua_Debug) {
121121
lua_pushfstring(L, cstr!("function '%s'"), (*ar).name);
122122
} else if *(*ar).what == b'm' as c_char {
123123
// main?
124-
lua_pushliteral(L, "main chunk");
124+
lua_pushliteral(L, c"main chunk");
125125
} else if *(*ar).what == b'C' as c_char {
126126
if compat53_pushglobalfuncname(L, ar) != 0 {
127127
lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1));
128128
lua_remove(L, -2); // remove name
129129
} else {
130-
lua_pushliteral(L, "?");
130+
lua_pushliteral(L, c"?");
131131
}
132132
} else {
133133
lua_pushfstring(
@@ -377,7 +377,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char)
377377
if !msg.is_null() {
378378
luaL_error(L, cstr!("stack overflow (%s)"), msg);
379379
} else {
380-
lua_pushliteral(L, "stack overflow");
380+
lua_pushliteral(L, c"stack overflow");
381381
lua_error(L);
382382
}
383383
}
@@ -467,20 +467,20 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const
467467
if !msg.is_null() {
468468
lua_pushfstring(L, cstr!("%s\n"), msg);
469469
}
470-
lua_pushliteral(L, "stack traceback:");
470+
lua_pushliteral(L, c"stack traceback:");
471471
while lua_getstack(L1, level, &mut ar) != 0 {
472472
level += 1;
473473
if level == mark {
474474
// too many levels?
475-
lua_pushliteral(L, "\n\t..."); // add a '...'
475+
lua_pushliteral(L, c"\n\t..."); // add a '...'
476476
level = numlevels - COMPAT53_LEVELS2; // and skip to last ones
477477
} else {
478478
lua_getinfo(L1, cstr!("Slnt"), &mut ar);
479479
lua_pushfstring(L, cstr!("\n\t%s:"), ar.short_src.as_ptr());
480480
if ar.currentline > 0 {
481481
lua_pushfstring(L, cstr!("%d:"), ar.currentline);
482482
}
483-
lua_pushliteral(L, " in ");
483+
lua_pushliteral(L, c" in ");
484484
compat53_pushfuncname(L, &mut ar);
485485
lua_concat(L, lua_gettop(L) - top);
486486
}
@@ -493,16 +493,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
493493
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
494494
match lua_type(L, idx) {
495495
LUA_TNIL => {
496-
lua_pushliteral(L, "nil");
496+
lua_pushliteral(L, c"nil");
497497
}
498498
LUA_TSTRING | LUA_TNUMBER => {
499499
lua_pushvalue(L, idx);
500500
}
501501
LUA_TBOOLEAN => {
502502
if lua_toboolean(L, idx) == 0 {
503-
lua_pushliteral(L, "false");
503+
lua_pushliteral(L, c"false");
504504
} else {
505-
lua_pushliteral(L, "true");
505+
lua_pushliteral(L, c"true");
506506
}
507507
}
508508
t => {

mlua-sys/src/lua51/lua.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains definitions from `lua.h`.
22
3+
use std::ffi::CStr;
34
use std::marker::{PhantomData, PhantomPinned};
45
use std::os::raw::{c_char, c_double, c_int, c_void};
56
use std::ptr;
@@ -312,10 +313,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
312313
}
313314

314315
#[inline(always)]
315-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
316-
use std::ffi::CString;
317-
let c_str = CString::new(s).unwrap();
318-
lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
316+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
317+
lua_pushstring_(L, s.as_ptr());
319318
}
320319

321320
#[inline(always)]

mlua-sys/src/lua52/compat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
199199
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
200200
match lua_type(L, idx) {
201201
LUA_TNIL => {
202-
lua_pushliteral(L, "nil");
202+
lua_pushliteral(L, c"nil");
203203
}
204204
LUA_TSTRING | LUA_TNUMBER => {
205205
lua_pushvalue(L, idx);
206206
}
207207
LUA_TBOOLEAN => {
208208
if lua_toboolean(L, idx) == 0 {
209-
lua_pushliteral(L, "false");
209+
lua_pushliteral(L, c"false");
210210
} else {
211-
lua_pushliteral(L, "true");
211+
lua_pushliteral(L, c"true");
212212
}
213213
}
214214
t => {

mlua-sys/src/lua52/lua.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains definitions from `lua.h`.
22
3+
use std::ffi::CStr;
34
use std::marker::{PhantomData, PhantomPinned};
45
use std::os::raw::{c_char, c_double, c_int, c_uchar, c_uint, c_void};
56
use std::ptr;
@@ -395,10 +396,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
395396
}
396397

397398
#[inline(always)]
398-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
399-
use std::ffi::CString;
400-
let c_str = CString::new(s).unwrap();
401-
lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
399+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
400+
lua_pushstring(L, s.as_ptr());
402401
}
403402

404403
#[inline(always)]

mlua-sys/src/lua53/lua.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains definitions from `lua.h`.
22
3+
use std::ffi::CStr;
34
use std::marker::{PhantomData, PhantomPinned};
45
use std::os::raw::{c_char, c_double, c_int, c_uchar, c_void};
56
use std::{mem, ptr};
@@ -407,10 +408,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
407408
}
408409

409410
#[inline(always)]
410-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
411-
use std::ffi::CString;
412-
let c_str = CString::new(s).unwrap();
413-
lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len())
411+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
412+
lua_pushstring(L, s.as_ptr());
414413
}
415414

416415
#[inline(always)]

mlua-sys/src/lua54/lua.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains definitions from `lua.h`.
22
3+
use std::ffi::CStr;
34
use std::marker::{PhantomData, PhantomPinned};
45
use std::os::raw::{c_char, c_double, c_int, c_uchar, c_ushort, c_void};
56
use std::{mem, ptr};
@@ -434,10 +435,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
434435
}
435436

436437
#[inline(always)]
437-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
438-
use std::ffi::CString;
439-
let c_str = CString::new(s).unwrap();
440-
lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len())
438+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
439+
lua_pushstring(L, s.as_ptr());
441440
}
442441

443442
#[inline(always)]

mlua-sys/src/luau/compat.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) ->
4141
} else if compat53_findfield(L, objidx, level - 1) != 0 {
4242
// try recursively
4343
lua_remove(L, -2); // remove table (but keep name)
44-
lua_pushliteral(L, ".");
44+
lua_pushliteral(L, c".");
4545
lua_insert(L, -2); // place '.' between the two names
4646
lua_concat(L, 3);
4747
return 1;
@@ -75,7 +75,7 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, level: c_int, ar: *mut lua_De
7575
lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1));
7676
lua_remove(L, -2); // remove name
7777
} else {
78-
lua_pushliteral(L, "?");
78+
lua_pushliteral(L, c"?");
7979
}
8080
}
8181

@@ -196,7 +196,7 @@ pub unsafe fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_
196196
pub unsafe fn lua_getuservalue(L: *mut lua_State, mut idx: c_int) -> c_int {
197197
luaL_checkstack(L, 2, cstr!("not enough stack slots available"));
198198
idx = lua_absindex(L, idx);
199-
lua_pushliteral(L, "__mlua_uservalues");
199+
lua_pushliteral(L, c"__mlua_uservalues");
200200
if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE {
201201
return LUA_TNIL;
202202
}
@@ -234,13 +234,13 @@ pub unsafe fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void) {
234234
pub unsafe fn lua_setuservalue(L: *mut lua_State, mut idx: c_int) {
235235
luaL_checkstack(L, 4, cstr!("not enough stack slots available"));
236236
idx = lua_absindex(L, idx);
237-
lua_pushliteral(L, "__mlua_uservalues");
237+
lua_pushliteral(L, c"__mlua_uservalues");
238238
lua_pushvalue(L, -1);
239239
if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE {
240240
lua_pop(L, 1);
241241
lua_createtable(L, 0, 2); // main table
242242
lua_createtable(L, 0, 1); // metatable
243-
lua_pushliteral(L, "k");
243+
lua_pushliteral(L, c"k");
244244
lua_setfield(L, -2, cstr!("__mode"));
245245
lua_setmetatable(L, -2);
246246
lua_pushvalue(L, -2);
@@ -301,7 +301,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char)
301301
if !msg.is_null() {
302302
luaL_error(L, cstr!("stack overflow (%s)"), msg);
303303
} else {
304-
lua_pushliteral(L, "stack overflow");
304+
lua_pushliteral(L, c"stack overflow");
305305
lua_error(L);
306306
}
307307
}
@@ -440,19 +440,19 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const
440440
if !msg.is_null() {
441441
lua_pushfstring(L, cstr!("%s\n"), msg);
442442
}
443-
lua_pushliteral(L, "stack traceback:");
443+
lua_pushliteral(L, c"stack traceback:");
444444
while lua_getinfo(L1, level, cstr!(""), &mut ar) != 0 {
445445
if level + 1 == mark {
446446
// too many levels?
447-
lua_pushliteral(L, "\n\t..."); // add a '...'
447+
lua_pushliteral(L, c"\n\t..."); // add a '...'
448448
level = numlevels - COMPAT53_LEVELS2; // and skip to last ones
449449
} else {
450450
lua_getinfo(L1, level, cstr!("sln"), &mut ar);
451451
lua_pushfstring(L, cstr!("\n\t%s:"), ar.short_src);
452452
if ar.currentline > 0 {
453453
lua_pushfstring(L, cstr!("%d:"), ar.currentline);
454454
}
455-
lua_pushliteral(L, " in ");
455+
lua_pushliteral(L, c" in ");
456456
compat53_pushfuncname(L, level, &mut ar);
457457
lua_concat(L, lua_gettop(L) - top);
458458
}
@@ -466,16 +466,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
466466
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
467467
match lua_type(L, idx) {
468468
LUA_TNIL => {
469-
lua_pushliteral(L, "nil");
469+
lua_pushliteral(L, c"nil");
470470
}
471471
LUA_TSTRING | LUA_TNUMBER => {
472472
lua_pushvalue(L, idx);
473473
}
474474
LUA_TBOOLEAN => {
475475
if lua_toboolean(L, idx) == 0 {
476-
lua_pushliteral(L, "false");
476+
lua_pushliteral(L, c"false");
477477
} else {
478-
lua_pushliteral(L, "true");
478+
lua_pushliteral(L, c"true");
479479
}
480480
}
481481
t => {

mlua-sys/src/luau/lauxlib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub unsafe fn luaL_sandbox(L: *mut lua_State, enabled: c_int) {
143143
}
144144

145145
// set all builtin metatables to read-only
146-
lua_pushliteral(L, "");
146+
lua_pushliteral(L, c"");
147147
if lua_getmetatable(L, -1) != 0 {
148148
lua_setreadonly(L, -1, enabled);
149149
lua_pop(L, 2);

mlua-sys/src/luau/lua.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains definitions from `lua.h`.
22
3+
use std::ffi::CStr;
34
use std::marker::{PhantomData, PhantomPinned};
45
use std::os::raw::{c_char, c_double, c_float, c_int, c_uint, c_void};
56
use std::{mem, ptr};
@@ -405,10 +406,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
405406
}
406407

407408
#[inline(always)]
408-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
409-
use std::ffi::CString;
410-
let c_str = CString::new(s).unwrap();
411-
lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
409+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
410+
lua_pushstring_(L, s.as_ptr());
412411
}
413412

414413
#[inline(always)]

src/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl Function {
280280
// Traverse upvalues until we find the _ENV one
281281
match ffi::lua_getupvalue(state, -1, i) {
282282
s if s.is_null() => break,
283-
s if std::ffi::CStr::from_ptr(s as _).to_bytes() == b"_ENV" => break,
283+
s if std::ffi::CStr::from_ptr(s as _) == c"_ENV" => break,
284284
_ => ffi::lua_pop(state, 1),
285285
}
286286
}
@@ -319,7 +319,7 @@ impl Function {
319319
for i in 1..=255 {
320320
match ffi::lua_getupvalue(state, -1, i) {
321321
s if s.is_null() => return Ok(false),
322-
s if std::ffi::CStr::from_ptr(s as _).to_bytes() == b"_ENV" => {
322+
s if std::ffi::CStr::from_ptr(s as _) == c"_ENV" => {
323323
ffi::lua_pop(state, 1);
324324
// Create an anonymous function with the new environment
325325
let f_with_env = lua

0 commit comments

Comments
 (0)