13
13
use crate :: error:: { Error , Result } ;
14
14
use crate :: ffi;
15
15
use crate :: types:: LuaRef ;
16
- use crate :: util:: { assert_stack, StackGuard } ;
17
16
18
17
/// Handle to an internal Lua string.
19
18
///
@@ -40,6 +39,7 @@ impl<'lua> String<'lua> {
40
39
/// # Ok(())
41
40
/// # }
42
41
/// ```
42
+ #[ inline]
43
43
pub fn to_str ( & self ) -> Result < & str > {
44
44
str:: from_utf8 ( self . as_bytes ( ) ) . map_err ( |e| Error :: FromLuaConversionError {
45
45
from : "string" ,
@@ -66,6 +66,7 @@ impl<'lua> String<'lua> {
66
66
/// # Ok(())
67
67
/// # }
68
68
/// ```
69
+ #[ inline]
69
70
pub fn to_string_lossy ( & self ) -> Cow < ' _ , str > {
70
71
StdString :: from_utf8_lossy ( self . as_bytes ( ) )
71
72
}
@@ -87,28 +88,25 @@ impl<'lua> String<'lua> {
87
88
/// # Ok(())
88
89
/// # }
89
90
/// ```
91
+ #[ inline]
90
92
pub fn as_bytes ( & self ) -> & [ u8 ] {
91
93
let nulled = self . as_bytes_with_nul ( ) ;
92
94
& nulled[ ..nulled. len ( ) - 1 ]
93
95
}
94
96
95
97
/// Get the bytes that make up this string, including the trailing nul byte.
96
98
pub fn as_bytes_with_nul ( & self ) -> & [ u8 ] {
97
- let lua = self . 0 . lua ;
99
+ let ref_thread = self . 0 . lua . ref_thread ( ) ;
98
100
unsafe {
99
- let _sg = StackGuard :: new ( lua. state ) ;
100
- assert_stack ( lua. state , 1 ) ;
101
-
102
- lua. push_ref ( & self . 0 ) ;
103
101
mlua_debug_assert ! (
104
- ffi:: lua_type( lua . state , - 1 ) == ffi:: LUA_TSTRING ,
102
+ ffi:: lua_type( ref_thread , self . 0 . index ) == ffi:: LUA_TSTRING ,
105
103
"string ref is not string type"
106
104
) ;
107
105
108
106
let mut size = 0 ;
109
107
// This will not trigger a 'm' error, because the reference is guaranteed to be of
110
108
// string type
111
- let data = ffi:: lua_tolstring ( lua . state , - 1 , & mut size) ;
109
+ let data = ffi:: lua_tolstring ( ref_thread , self . 0 . index , & mut size) ;
112
110
113
111
slice:: from_raw_parts ( data as * const u8 , size + 1 )
114
112
}
@@ -121,8 +119,8 @@ impl<'lua> String<'lua> {
121
119
/// Typically this function is used only for hashing and debug information.
122
120
#[ inline]
123
121
pub fn to_pointer ( & self ) -> * const c_void {
124
- let lua = self . 0 . lua ;
125
- unsafe { ffi:: lua_topointer ( lua . ref_thread ( ) , self . 0 . index ) }
122
+ let ref_thread = self . 0 . lua . ref_thread ( ) ;
123
+ unsafe { ffi:: lua_topointer ( ref_thread, self . 0 . index ) }
126
124
}
127
125
}
128
126
0 commit comments