1
+ #![ allow( clippy:: missing_safety_doc) ]
2
+
1
3
use std:: os:: raw:: { c_char, c_int, c_long, c_void} ;
2
4
3
5
extern "C" {
@@ -9,28 +11,26 @@ extern "C" {
9
11
pub fn lua_pcall ( state : * mut c_void , nargs : c_int , nresults : c_int , errfunc : c_int ) -> c_int ;
10
12
}
11
13
14
+ pub unsafe fn lua_getglobal ( state : * mut c_void , k : * const c_char ) {
15
+ lua_getfield ( state, -10002 /* LUA_GLOBALSINDEX */ , k) ;
16
+ }
17
+
18
+ pub unsafe fn to_string < ' a > ( state : * mut c_void , index : c_int ) -> & ' a str {
19
+ let mut len: c_long = 0 ;
20
+ let str_ptr = lua_tolstring ( state, index, & mut len) ;
21
+ let bytes = std:: slice:: from_raw_parts ( str_ptr as * const u8 , len as usize ) ;
22
+ std:: str:: from_utf8 ( bytes) . unwrap ( )
23
+ }
24
+
12
25
#[ cfg( test) ]
13
26
mod tests {
14
- use std:: { ptr, slice, str} ;
15
-
16
27
use super :: * ;
17
28
18
- pub unsafe fn lua_getglobal ( state : * mut c_void , k : * const c_char ) {
19
- lua_getfield ( state, -10002 /* LUA_GLOBALSINDEX */ , k) ;
20
- }
21
-
22
- pub unsafe fn to_string < ' a > ( state : * mut c_void , index : c_int ) -> & ' a str {
23
- let mut len: c_long = 0 ;
24
- let str_ptr = lua_tolstring ( state, index, & mut len) ;
25
- let bytes = slice:: from_raw_parts ( str_ptr as * const u8 , len as usize ) ;
26
- str:: from_utf8 ( bytes) . unwrap ( )
27
- }
28
-
29
29
#[ test]
30
30
fn test_lua ( ) {
31
31
unsafe {
32
32
let state = luaL_newstate ( ) ;
33
- assert ! ( state != ptr :: null_mut ( ) ) ;
33
+ assert ! ( !state . is_null ( ) ) ;
34
34
35
35
luaL_openlibs ( state) ;
36
36
@@ -57,7 +57,7 @@ mod tests {
57
57
fn test_lua52compat ( ) {
58
58
unsafe {
59
59
let state = luaL_newstate ( ) ;
60
- assert ! ( state != ptr :: null_mut ( ) ) ;
60
+ assert ! ( !state . is_null ( ) ) ;
61
61
62
62
luaL_openlibs ( state) ;
63
63
@@ -78,13 +78,9 @@ mod tests {
78
78
79
79
let lua52compat = {
80
80
lua_getglobal ( state, "lua52compat\0 " . as_ptr ( ) . cast ( ) ) ;
81
- to_string ( state, -1 )
81
+ to_string ( state, -1 ) == "yes"
82
82
} ;
83
-
84
- #[ cfg( feature = "lua52compat" ) ]
85
- assert_eq ! ( lua52compat, "yes" ) ;
86
- #[ cfg( not( feature = "lua52compat" ) ) ]
87
- assert_eq ! ( lua52compat, "no" ) ;
83
+ assert_eq ! ( lua52compat, cfg!( feature = "lua52compat" ) ) ;
88
84
}
89
85
}
90
86
}
0 commit comments