Skip to content

Commit 95c623e

Browse files
committed
Use c string literal where appropriate
1 parent 0a1a1fe commit 95c623e

File tree

13 files changed

+49
-56
lines changed

13 files changed

+49
-56
lines changed

mlua-sys/src/lua51/compat.rs

+10-10
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

+3-4
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

+3-3
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

+3-4
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

+3-4
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

+3-4
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

+12-12
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) ->
4343
} else if compat53_findfield(L, objidx, level - 1) != 0 {
4444
// try recursively
4545
lua_remove(L, -2); // remove table (but keep name)
46-
lua_pushliteral(L, ".");
46+
lua_pushliteral(L, c".");
4747
lua_insert(L, -2); // place '.' between the two names
4848
lua_concat(L, 3);
4949
return 1;
@@ -77,7 +77,7 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, level: c_int, ar: *mut lua_De
7777
lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1));
7878
lua_remove(L, -2); // remove name
7979
} else {
80-
lua_pushliteral(L, "?");
80+
lua_pushliteral(L, c"?");
8181
}
8282
}
8383

@@ -198,7 +198,7 @@ pub unsafe fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_
198198
pub unsafe fn lua_getuservalue(L: *mut lua_State, mut idx: c_int) -> c_int {
199199
luaL_checkstack(L, 2, cstr!("not enough stack slots available"));
200200
idx = lua_absindex(L, idx);
201-
lua_pushliteral(L, "__mlua_uservalues");
201+
lua_pushliteral(L, c"__mlua_uservalues");
202202
if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE {
203203
return LUA_TNIL;
204204
}
@@ -236,13 +236,13 @@ pub unsafe fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void) {
236236
pub unsafe fn lua_setuservalue(L: *mut lua_State, mut idx: c_int) {
237237
luaL_checkstack(L, 4, cstr!("not enough stack slots available"));
238238
idx = lua_absindex(L, idx);
239-
lua_pushliteral(L, "__mlua_uservalues");
239+
lua_pushliteral(L, c"__mlua_uservalues");
240240
lua_pushvalue(L, -1);
241241
if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE {
242242
lua_pop(L, 1);
243243
lua_createtable(L, 0, 2); // main table
244244
lua_createtable(L, 0, 1); // metatable
245-
lua_pushliteral(L, "k");
245+
lua_pushliteral(L, c"k");
246246
lua_setfield(L, -2, cstr!("__mode"));
247247
lua_setmetatable(L, -2);
248248
lua_pushvalue(L, -2);
@@ -316,7 +316,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char)
316316
if !msg.is_null() {
317317
luaL_error(L, cstr!("stack overflow (%s)"), msg);
318318
} else {
319-
lua_pushliteral(L, "stack overflow");
319+
lua_pushliteral(L, c"stack overflow");
320320
lua_error(L);
321321
}
322322
}
@@ -455,19 +455,19 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const
455455
if !msg.is_null() {
456456
lua_pushfstring(L, cstr!("%s\n"), msg);
457457
}
458-
lua_pushliteral(L, "stack traceback:");
458+
lua_pushliteral(L, c"stack traceback:");
459459
while lua_getinfo(L1, level, cstr!(""), &mut ar) != 0 {
460460
if level + 1 == mark {
461461
// too many levels?
462-
lua_pushliteral(L, "\n\t..."); // add a '...'
462+
lua_pushliteral(L, c"\n\t..."); // add a '...'
463463
level = numlevels - COMPAT53_LEVELS2; // and skip to last ones
464464
} else {
465465
lua_getinfo(L1, level, cstr!("sln"), &mut ar);
466466
lua_pushfstring(L, cstr!("\n\t%s:"), ar.short_src);
467467
if ar.currentline > 0 {
468468
lua_pushfstring(L, cstr!("%d:"), ar.currentline);
469469
}
470-
lua_pushliteral(L, " in ");
470+
lua_pushliteral(L, c" in ");
471471
compat53_pushfuncname(L, level, &mut ar);
472472
lua_concat(L, lua_gettop(L) - top);
473473
}
@@ -481,16 +481,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
481481
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
482482
match lua_type(L, idx) {
483483
LUA_TNIL => {
484-
lua_pushliteral(L, "nil");
484+
lua_pushliteral(L, c"nil");
485485
}
486486
LUA_TSTRING | LUA_TNUMBER => {
487487
lua_pushvalue(L, idx);
488488
}
489489
LUA_TBOOLEAN => {
490490
if lua_toboolean(L, idx) == 0 {
491-
lua_pushliteral(L, "false");
491+
lua_pushliteral(L, c"false");
492492
} else {
493-
lua_pushliteral(L, "true");
493+
lua_pushliteral(L, c"true");
494494
}
495495
}
496496
t => {

mlua-sys/src/luau/lauxlib.rs

+1-1
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

+3-4
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

+2-2
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

src/state/raw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl RawLua {
401401
}
402402
#[cfg(any(feature = "lua52", feature = "lua51", feature = "luajit"))]
403403
{
404-
ffi::lua_pushliteral(state, "attempt to yield from a hook");
404+
ffi::lua_pushliteral(state, c"attempt to yield from a hook");
405405
ffi::lua_error(state);
406406
}
407407
}
@@ -473,7 +473,7 @@ impl RawLua {
473473
protect_lua!(state, 0, 0, |state| {
474474
if ffi::luaL_getsubtable(state, ffi::LUA_REGISTRYINDEX, HOOKS_KEY) == 0 {
475475
// Table just created, initialize it
476-
ffi::lua_pushliteral(state, "k");
476+
ffi::lua_pushliteral(state, c"k");
477477
ffi::lua_setfield(state, -2, cstr!("__mode")); // hooktable.__mode = "k"
478478
ffi::lua_pushvalue(state, -1);
479479
ffi::lua_setmetatable(state, -2); // metatable(hooktable) = hooktable

src/userdata.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ impl MetaMethod {
220220
pub(crate) const fn as_cstr(self) -> &'static CStr {
221221
match self {
222222
#[rustfmt::skip]
223-
MetaMethod::Type => unsafe {
224-
CStr::from_bytes_with_nul_unchecked(if cfg!(feature = "luau") { b"__type\0" } else { b"__name\0" })
225-
},
223+
MetaMethod::Type => if cfg!(feature = "luau") { c"__type" } else { c"__name" },
226224
_ => unreachable!(),
227225
}
228226
}

tests/conversion.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
3-
use std::ffi::{CStr, CString, OsString};
3+
use std::ffi::{CString, OsString};
44
use std::path::PathBuf;
55

66
use bstr::BString;
@@ -450,8 +450,8 @@ fn test_conv_cstring() -> Result<()> {
450450
let s2: CString = lua.globals().get("s")?;
451451
assert_eq!(s, s2);
452452

453-
let cs = CStr::from_bytes_with_nul(b"hello\0").unwrap();
454-
lua.globals().set("cs", cs)?;
453+
let cs = c"hello";
454+
lua.globals().set("cs", c"hello")?;
455455
let cs2: CString = lua.globals().get("cs")?;
456456
assert_eq!(cs, cs2.as_c_str());
457457

0 commit comments

Comments
 (0)