Skip to content

Commit 8c66b13

Browse files
committed
More usage of cstr! macro
1 parent fa8274f commit 8c66b13

File tree

3 files changed

+47
-67
lines changed

3 files changed

+47
-67
lines changed

src/lmem.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,7 @@ pub unsafe extern "C" fn luaM_growaux_(
8484
/* cannot double it? */
8585
if *size >= limit {
8686
/* cannot grow even a little? */
87-
luaG_runerror(
88-
L,
89-
b"too many %s (limit is %d)\0" as *const u8 as *const c_char,
90-
what,
91-
limit,
92-
);
87+
luaG_runerror(L, cstr!("too many %s (limit is %d)"), what, limit);
9388
}
9489
newsize = limit; /* still have at least one free place */
9590
} else {
@@ -110,10 +105,7 @@ pub unsafe extern "C" fn luaM_growaux_(
110105

111106
#[no_mangle]
112107
pub unsafe extern "C" fn luaM_toobig(L: *mut lua_State) -> ! {
113-
luaG_runerror(
114-
L,
115-
b"memory allocation error: block too big\0" as *const u8 as *const c_char,
116-
);
108+
luaG_runerror(L, cstr!("memory allocation error: block too big"));
117109
}
118110

119111
/*

src/ltable.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::hash::{Hash, Hasher};
77
use std::mem::{self, size_of};
88
use std::ptr;
99

10-
use libc::{c_char, c_int, c_uint, c_void};
10+
use libc::{c_int, c_uint, c_void};
1111

1212
use crate::ldebug::luaG_runerror;
1313
use crate::ldo::{luaD_rawrunprotected, luaD_throw};
@@ -252,7 +252,7 @@ unsafe extern "C" fn findindex(L: *mut lua_State, t: *mut Table, key: StkId) ->
252252
nx = gnext(n);
253253
if nx == 0 {
254254
/* key not found */
255-
luaG_runerror(L, b"invalid key to 'next'\0" as *const u8 as *const c_char);
255+
luaG_runerror(L, cstr!("invalid key to 'next'"));
256256
} else {
257257
n = n.offset(nx as isize);
258258
}
@@ -406,7 +406,7 @@ unsafe extern "C" fn setnodevector(L: *mut lua_State, t: *mut Table, mut size: c
406406
} else {
407407
let lsize = luaO_ceillog2(size);
408408
if lsize as u64 > MAXHBITS {
409-
luaG_runerror(L, b"table overflow\0" as *const u8 as *const c_char);
409+
luaG_runerror(L, cstr!("table overflow"));
410410
}
411411
size = 1 << lsize; // 2^lsize
412412
(*t).node = luaM_newvector::<Node>(L, size as usize);
@@ -583,7 +583,7 @@ pub unsafe extern "C" fn luaH_newkey(
583583
tt_: 0,
584584
};
585585
if ttisnil(key) {
586-
luaG_runerror(L, b"table index is nil\0" as *const u8 as *const c_char);
586+
luaG_runerror(L, cstr!("table index is nil"));
587587
} else if ttisfloat(key) {
588588
let mut k: lua_Integer = 0;
589589
if luaV_tointeger(key, &mut k, 0) != 0 {
@@ -592,7 +592,7 @@ pub unsafe extern "C" fn luaH_newkey(
592592
/* insert it as an integer */
593593
key = &mut aux;
594594
} else if fltvalue(key).is_nan() {
595-
luaG_runerror(L, b"table index is NaN\0" as *const u8 as *const c_char);
595+
luaG_runerror(L, cstr!("table index is NaN"));
596596
}
597597
}
598598
let mut mp = mainposition(t, key);

src/ltm.rs

+40-52
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
** Tag methods
33
*/
44

5-
use std::mem;
65
use std::ptr;
76

87
use libc::{c_char, c_int, c_uint};
@@ -62,54 +61,53 @@ unsafe fn ttypename(i: usize) -> *const c_char {
6261
luaT_typenames_[i + 1]
6362
}
6463

65-
static mut udatatypename: [c_char; 9] =
66-
unsafe { *mem::transmute::<&[u8; 9], &[c_char; 9]>(b"userdata\0") };
64+
const udatatypename: *const c_char = cstr!("userdata");
6765

6866
#[no_mangle]
69-
pub static mut luaT_typenames_: [*const c_char; LUA_TOTALTAGS] = unsafe {
67+
pub static mut luaT_typenames_: [*const c_char; LUA_TOTALTAGS] = {
7068
[
71-
b"no value\0" as *const u8 as *const c_char,
72-
b"nil\0" as *const u8 as *const c_char,
73-
b"boolean\0" as *const u8 as *const c_char,
74-
udatatypename.as_ptr(),
75-
b"number\0" as *const u8 as *const c_char,
76-
b"string\0" as *const u8 as *const c_char,
77-
b"table\0" as *const u8 as *const c_char,
78-
b"function\0" as *const u8 as *const c_char,
79-
udatatypename.as_ptr(),
80-
b"thread\0" as *const u8 as *const c_char,
81-
b"proto\0" as *const u8 as *const c_char, /* this last case is used for tests only */
69+
cstr!("no value"),
70+
cstr!("nil"),
71+
cstr!("boolean"),
72+
udatatypename,
73+
cstr!("number"),
74+
cstr!("string"),
75+
cstr!("table"),
76+
cstr!("function"),
77+
udatatypename,
78+
cstr!("thread"),
79+
cstr!("proto"), /* this last case is used for tests only */
8280
]
8381
};
8482

8583
#[no_mangle]
8684
pub unsafe extern "C" fn luaT_init(L: *mut lua_State) {
8785
static mut luaT_eventname: [*const c_char; 24] = [
8886
/* ORDER TM */
89-
b"__index\0" as *const u8 as *const c_char,
90-
b"__newindex\0" as *const u8 as *const c_char,
91-
b"__gc\0" as *const u8 as *const c_char,
92-
b"__mode\0" as *const u8 as *const c_char,
93-
b"__len\0" as *const u8 as *const c_char,
94-
b"__eq\0" as *const u8 as *const c_char,
95-
b"__add\0" as *const u8 as *const c_char,
96-
b"__sub\0" as *const u8 as *const c_char,
97-
b"__mul\0" as *const u8 as *const c_char,
98-
b"__mod\0" as *const u8 as *const c_char,
99-
b"__pow\0" as *const u8 as *const c_char,
100-
b"__div\0" as *const u8 as *const c_char,
101-
b"__idiv\0" as *const u8 as *const c_char,
102-
b"__band\0" as *const u8 as *const c_char,
103-
b"__bor\0" as *const u8 as *const c_char,
104-
b"__bxor\0" as *const u8 as *const c_char,
105-
b"__shl\0" as *const u8 as *const c_char,
106-
b"__shr\0" as *const u8 as *const c_char,
107-
b"__unm\0" as *const u8 as *const c_char,
108-
b"__bnot\0" as *const u8 as *const c_char,
109-
b"__lt\0" as *const u8 as *const c_char,
110-
b"__le\0" as *const u8 as *const c_char,
111-
b"__concat\0" as *const u8 as *const c_char,
112-
b"__call\0" as *const u8 as *const c_char,
87+
cstr!("__index"),
88+
cstr!("__newindex"),
89+
cstr!("__gc"),
90+
cstr!("__mode"),
91+
cstr!("__len"),
92+
cstr!("__eq"),
93+
cstr!("__add"),
94+
cstr!("__sub"),
95+
cstr!("__mul"),
96+
cstr!("__mod"),
97+
cstr!("__pow"),
98+
cstr!("__div"),
99+
cstr!("__idiv"),
100+
cstr!("__band"),
101+
cstr!("__bor"),
102+
cstr!("__bxor"),
103+
cstr!("__shl"),
104+
cstr!("__shr"),
105+
cstr!("__unm"),
106+
cstr!("__bnot"),
107+
cstr!("__lt"),
108+
cstr!("__le"),
109+
cstr!("__concat"),
110+
cstr!("__call"),
113111
];
114112
let mut i = 0;
115113
while i < TM_N {
@@ -177,7 +175,7 @@ pub unsafe extern "C" fn luaT_objtypename(L: *mut lua_State, o: *const TValue) -
177175
mt = (*uvalue(o)).metatable;
178176
}
179177
if !mt.is_null() {
180-
let name = luaH_getshortstr(mt, luaS_new(L, b"__name\0" as *const u8 as *const c_char));
178+
let name = luaH_getshortstr(mt, luaS_new(L, cstr!("__name")));
181179
if ttisstring(name) {
182180
/* is '__name' a string? */
183181
return getstr(tsvalue(name)); /* use it as type name */
@@ -257,21 +255,11 @@ pub unsafe extern "C" fn luaT_trybinTM(
257255
if tonumber(p1, &mut dummy) != 0 && tonumber(p2, &mut dummy) != 0 {
258256
luaG_tointerror(L, p1, p2);
259257
} else {
260-
luaG_opinterror(
261-
L,
262-
p1,
263-
p2,
264-
b"perform bitwise operation on\0" as *const u8 as *const c_char,
265-
);
258+
luaG_opinterror(L, p1, p2, cstr!("perform bitwise operation on"));
266259
}
267260
}
268261
_ => {
269-
luaG_opinterror(
270-
L,
271-
p1,
272-
p2,
273-
b"perform arithmetic on\0" as *const u8 as *const c_char,
274-
);
262+
luaG_opinterror(L, p1, p2, cstr!("perform arithmetic on"));
275263
}
276264
}
277265
}

0 commit comments

Comments
 (0)