diff --git a/mlua-sys/src/lua51/compat.rs b/mlua-sys/src/lua51/compat.rs index 2294cd4..8c2d7bf 100644 --- a/mlua-sys/src/lua51/compat.rs +++ b/mlua-sys/src/lua51/compat.rs @@ -90,7 +90,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) -> } else if compat53_findfield(L, objidx, level - 1) != 0 { // try recursively lua_remove(L, -2); // remove table (but keep name) - lua_pushliteral(L, "."); + lua_pushliteral(L, c"."); lua_insert(L, -2); // place '.' between the two names lua_concat(L, 3); return 1; @@ -121,13 +121,13 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, ar: *mut lua_Debug) { lua_pushfstring(L, cstr!("function '%s'"), (*ar).name); } else if *(*ar).what == b'm' as c_char { // main? - lua_pushliteral(L, "main chunk"); + lua_pushliteral(L, c"main chunk"); } else if *(*ar).what == b'C' as c_char { if compat53_pushglobalfuncname(L, ar) != 0 { lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1)); lua_remove(L, -2); // remove name } else { - lua_pushliteral(L, "?"); + lua_pushliteral(L, c"?"); } } else { lua_pushfstring( @@ -377,7 +377,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char) if !msg.is_null() { luaL_error(L, cstr!("stack overflow (%s)"), msg); } else { - lua_pushliteral(L, "stack overflow"); + lua_pushliteral(L, c"stack overflow"); lua_error(L); } } @@ -467,12 +467,12 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const if !msg.is_null() { lua_pushfstring(L, cstr!("%s\n"), msg); } - lua_pushliteral(L, "stack traceback:"); + lua_pushliteral(L, c"stack traceback:"); while lua_getstack(L1, level, &mut ar) != 0 { level += 1; if level == mark { // too many levels? - lua_pushliteral(L, "\n\t..."); // add a '...' + lua_pushliteral(L, c"\n\t..."); // add a '...' level = numlevels - COMPAT53_LEVELS2; // and skip to last ones } else { lua_getinfo(L1, cstr!("Slnt"), &mut ar); @@ -480,7 +480,7 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const if ar.currentline > 0 { lua_pushfstring(L, cstr!("%d:"), ar.currentline); } - lua_pushliteral(L, " in "); + lua_pushliteral(L, c" in "); compat53_pushfuncname(L, &mut ar); lua_concat(L, lua_gettop(L) - top); } @@ -493,16 +493,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 { match lua_type(L, idx) { LUA_TNIL => { - lua_pushliteral(L, "nil"); + lua_pushliteral(L, c"nil"); } LUA_TSTRING | LUA_TNUMBER => { lua_pushvalue(L, idx); } LUA_TBOOLEAN => { if lua_toboolean(L, idx) == 0 { - lua_pushliteral(L, "false"); + lua_pushliteral(L, c"false"); } else { - lua_pushliteral(L, "true"); + lua_pushliteral(L, c"true"); } } t => { diff --git a/mlua-sys/src/lua51/lua.rs b/mlua-sys/src/lua51/lua.rs index f222b38..4789c56 100644 --- a/mlua-sys/src/lua51/lua.rs +++ b/mlua-sys/src/lua51/lua.rs @@ -1,5 +1,6 @@ //! Contains definitions from `lua.h`. +use std::ffi::CStr; use std::marker::{PhantomData, PhantomPinned}; use std::os::raw::{c_char, c_double, c_int, c_void}; use std::ptr; @@ -312,10 +313,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int { } #[inline(always)] -pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) { - use std::ffi::CString; - let c_str = CString::new(s).unwrap(); - lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len()) +pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) { + lua_pushstring_(L, s.as_ptr()); } #[inline(always)] diff --git a/mlua-sys/src/lua52/compat.rs b/mlua-sys/src/lua52/compat.rs index d6a79ac..68c7029 100644 --- a/mlua-sys/src/lua52/compat.rs +++ b/mlua-sys/src/lua52/compat.rs @@ -199,16 +199,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 { match lua_type(L, idx) { LUA_TNIL => { - lua_pushliteral(L, "nil"); + lua_pushliteral(L, c"nil"); } LUA_TSTRING | LUA_TNUMBER => { lua_pushvalue(L, idx); } LUA_TBOOLEAN => { if lua_toboolean(L, idx) == 0 { - lua_pushliteral(L, "false"); + lua_pushliteral(L, c"false"); } else { - lua_pushliteral(L, "true"); + lua_pushliteral(L, c"true"); } } t => { diff --git a/mlua-sys/src/lua52/lua.rs b/mlua-sys/src/lua52/lua.rs index 6714611..b77ef14 100644 --- a/mlua-sys/src/lua52/lua.rs +++ b/mlua-sys/src/lua52/lua.rs @@ -1,5 +1,6 @@ //! Contains definitions from `lua.h`. +use std::ffi::CStr; use std::marker::{PhantomData, PhantomPinned}; use std::os::raw::{c_char, c_double, c_int, c_uchar, c_uint, c_void}; use std::ptr; @@ -395,10 +396,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int { } #[inline(always)] -pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char { - use std::ffi::CString; - let c_str = CString::new(s).unwrap(); - lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len()) +pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) { + lua_pushstring(L, s.as_ptr()); } #[inline(always)] diff --git a/mlua-sys/src/lua53/lua.rs b/mlua-sys/src/lua53/lua.rs index 47010f5..3fd84f5 100644 --- a/mlua-sys/src/lua53/lua.rs +++ b/mlua-sys/src/lua53/lua.rs @@ -1,5 +1,6 @@ //! Contains definitions from `lua.h`. +use std::ffi::CStr; use std::marker::{PhantomData, PhantomPinned}; use std::os::raw::{c_char, c_double, c_int, c_uchar, c_void}; use std::{mem, ptr}; @@ -407,10 +408,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int { } #[inline(always)] -pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char { - use std::ffi::CString; - let c_str = CString::new(s).unwrap(); - lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len()) +pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) { + lua_pushstring(L, s.as_ptr()); } #[inline(always)] diff --git a/mlua-sys/src/lua54/lua.rs b/mlua-sys/src/lua54/lua.rs index 796bec7..afad4a3 100644 --- a/mlua-sys/src/lua54/lua.rs +++ b/mlua-sys/src/lua54/lua.rs @@ -1,5 +1,6 @@ //! Contains definitions from `lua.h`. +use std::ffi::CStr; use std::marker::{PhantomData, PhantomPinned}; use std::os::raw::{c_char, c_double, c_int, c_uchar, c_ushort, c_void}; use std::{mem, ptr}; @@ -434,10 +435,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int { } #[inline(always)] -pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char { - use std::ffi::CString; - let c_str = CString::new(s).unwrap(); - lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len()) +pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) { + lua_pushstring(L, s.as_ptr()); } #[inline(always)] diff --git a/mlua-sys/src/luau/compat.rs b/mlua-sys/src/luau/compat.rs index 0cbed7b..a54b6f5 100644 --- a/mlua-sys/src/luau/compat.rs +++ b/mlua-sys/src/luau/compat.rs @@ -41,7 +41,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) -> } else if compat53_findfield(L, objidx, level - 1) != 0 { // try recursively lua_remove(L, -2); // remove table (but keep name) - lua_pushliteral(L, "."); + lua_pushliteral(L, c"."); lua_insert(L, -2); // place '.' between the two names lua_concat(L, 3); return 1; @@ -75,7 +75,7 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, level: c_int, ar: *mut lua_De lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1)); lua_remove(L, -2); // remove name } else { - lua_pushliteral(L, "?"); + lua_pushliteral(L, c"?"); } } @@ -196,7 +196,7 @@ pub unsafe fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_ pub unsafe fn lua_getuservalue(L: *mut lua_State, mut idx: c_int) -> c_int { luaL_checkstack(L, 2, cstr!("not enough stack slots available")); idx = lua_absindex(L, idx); - lua_pushliteral(L, "__mlua_uservalues"); + lua_pushliteral(L, c"__mlua_uservalues"); if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE { return LUA_TNIL; } @@ -234,13 +234,13 @@ pub unsafe fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void) { pub unsafe fn lua_setuservalue(L: *mut lua_State, mut idx: c_int) { luaL_checkstack(L, 4, cstr!("not enough stack slots available")); idx = lua_absindex(L, idx); - lua_pushliteral(L, "__mlua_uservalues"); + lua_pushliteral(L, c"__mlua_uservalues"); lua_pushvalue(L, -1); if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE { lua_pop(L, 1); lua_createtable(L, 0, 2); // main table lua_createtable(L, 0, 1); // metatable - lua_pushliteral(L, "k"); + lua_pushliteral(L, c"k"); lua_setfield(L, -2, cstr!("__mode")); lua_setmetatable(L, -2); lua_pushvalue(L, -2); @@ -301,7 +301,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char) if !msg.is_null() { luaL_error(L, cstr!("stack overflow (%s)"), msg); } else { - lua_pushliteral(L, "stack overflow"); + lua_pushliteral(L, c"stack overflow"); lua_error(L); } } @@ -440,11 +440,11 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const if !msg.is_null() { lua_pushfstring(L, cstr!("%s\n"), msg); } - lua_pushliteral(L, "stack traceback:"); + lua_pushliteral(L, c"stack traceback:"); while lua_getinfo(L1, level, cstr!(""), &mut ar) != 0 { if level + 1 == mark { // too many levels? - lua_pushliteral(L, "\n\t..."); // add a '...' + lua_pushliteral(L, c"\n\t..."); // add a '...' level = numlevels - COMPAT53_LEVELS2; // and skip to last ones } else { lua_getinfo(L1, level, cstr!("sln"), &mut ar); @@ -452,7 +452,7 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const if ar.currentline > 0 { lua_pushfstring(L, cstr!("%d:"), ar.currentline); } - lua_pushliteral(L, " in "); + lua_pushliteral(L, c" in "); compat53_pushfuncname(L, level, &mut ar); lua_concat(L, lua_gettop(L) - top); } @@ -466,16 +466,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize) if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 { match lua_type(L, idx) { LUA_TNIL => { - lua_pushliteral(L, "nil"); + lua_pushliteral(L, c"nil"); } LUA_TSTRING | LUA_TNUMBER => { lua_pushvalue(L, idx); } LUA_TBOOLEAN => { if lua_toboolean(L, idx) == 0 { - lua_pushliteral(L, "false"); + lua_pushliteral(L, c"false"); } else { - lua_pushliteral(L, "true"); + lua_pushliteral(L, c"true"); } } t => { diff --git a/mlua-sys/src/luau/lauxlib.rs b/mlua-sys/src/luau/lauxlib.rs index 0b75cbe..ddedb2c 100644 --- a/mlua-sys/src/luau/lauxlib.rs +++ b/mlua-sys/src/luau/lauxlib.rs @@ -143,7 +143,7 @@ pub unsafe fn luaL_sandbox(L: *mut lua_State, enabled: c_int) { } // set all builtin metatables to read-only - lua_pushliteral(L, ""); + lua_pushliteral(L, c""); if lua_getmetatable(L, -1) != 0 { lua_setreadonly(L, -1, enabled); lua_pop(L, 2); diff --git a/mlua-sys/src/luau/lua.rs b/mlua-sys/src/luau/lua.rs index a916f80..f97a80c 100644 --- a/mlua-sys/src/luau/lua.rs +++ b/mlua-sys/src/luau/lua.rs @@ -1,5 +1,6 @@ //! Contains definitions from `lua.h`. +use std::ffi::CStr; use std::marker::{PhantomData, PhantomPinned}; use std::os::raw::{c_char, c_double, c_float, c_int, c_uint, c_void}; use std::{mem, ptr}; @@ -405,10 +406,8 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int { } #[inline(always)] -pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) { - use std::ffi::CString; - let c_str = CString::new(s).unwrap(); - lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len()) +pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) { + lua_pushstring_(L, s.as_ptr()); } #[inline(always)] diff --git a/src/function.rs b/src/function.rs index 37b33fa..aa16230 100644 --- a/src/function.rs +++ b/src/function.rs @@ -280,7 +280,7 @@ impl Function { // Traverse upvalues until we find the _ENV one match ffi::lua_getupvalue(state, -1, i) { s if s.is_null() => break, - s if std::ffi::CStr::from_ptr(s as _).to_bytes() == b"_ENV" => break, + s if std::ffi::CStr::from_ptr(s as _) == c"_ENV" => break, _ => ffi::lua_pop(state, 1), } } @@ -319,7 +319,7 @@ impl Function { for i in 1..=255 { match ffi::lua_getupvalue(state, -1, i) { s if s.is_null() => return Ok(false), - s if std::ffi::CStr::from_ptr(s as _).to_bytes() == b"_ENV" => { + s if std::ffi::CStr::from_ptr(s as _) == c"_ENV" => { ffi::lua_pop(state, 1); // Create an anonymous function with the new environment let f_with_env = lua diff --git a/src/state/raw.rs b/src/state/raw.rs index 1f94db8..61dbd63 100644 --- a/src/state/raw.rs +++ b/src/state/raw.rs @@ -416,7 +416,7 @@ impl RawLua { } #[cfg(any(feature = "lua52", feature = "lua51", feature = "luajit"))] { - ffi::lua_pushliteral(state, "attempt to yield from a hook"); + ffi::lua_pushliteral(state, c"attempt to yield from a hook"); ffi::lua_error(state); } } diff --git a/src/userdata.rs b/src/userdata.rs index b3be682..b8479ce 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -220,9 +220,7 @@ impl MetaMethod { pub(crate) const fn as_cstr(self) -> &'static CStr { match self { #[rustfmt::skip] - MetaMethod::Type => unsafe { - CStr::from_bytes_with_nul_unchecked(if cfg!(feature = "luau") { b"__type\0" } else { b"__name\0" }) - }, + MetaMethod::Type => if cfg!(feature = "luau") { c"__type" } else { c"__name" }, _ => unreachable!(), } } diff --git a/tests/conversion.rs b/tests/conversion.rs index e75a3a0..d724fa8 100644 --- a/tests/conversion.rs +++ b/tests/conversion.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; -use std::ffi::{CStr, CString, OsString}; +use std::ffi::{CString, OsString}; use std::path::PathBuf; use bstr::BString; @@ -450,8 +450,8 @@ fn test_conv_cstring() -> Result<()> { let s2: CString = lua.globals().get("s")?; assert_eq!(s, s2); - let cs = CStr::from_bytes_with_nul(b"hello\0").unwrap(); - lua.globals().set("cs", cs)?; + let cs = c"hello"; + lua.globals().set("cs", c"hello")?; let cs2: CString = lua.globals().get("cs")?; assert_eq!(cs, cs2.as_c_str());