@@ -8,6 +8,7 @@ use std::fmt;
88use std:: hash:: Hash ;
99use std:: os:: raw:: { c_char, c_void} ;
1010
11+ use crate :: Either ;
1112use crate :: error:: { Error , Result } ;
1213use crate :: function:: Function ;
1314use crate :: state:: Lua ;
@@ -1028,8 +1029,8 @@ impl AnyUserData {
10281029
10291030 /// Returns a type name of this userdata (from a metatable field).
10301031 ///
1031- /// If no type name is set, returns `None `.
1032- pub fn type_name ( & self ) -> Result < Option < String > > {
1032+ /// If no type name is set, returns `userdata `.
1033+ pub fn type_name ( & self ) -> Result < LuaString > {
10331034 let lua = self . 0 . lua . lock ( ) ;
10341035 let state = lua. state ( ) ;
10351036 unsafe {
@@ -1046,8 +1047,8 @@ impl AnyUserData {
10461047 ffi:: luaL_getmetafield ( state, -1 , MetaMethod :: Type . as_cstr ( ) . as_ptr ( ) )
10471048 } ;
10481049 match name_type {
1049- ffi:: LUA_TSTRING => Ok ( Some ( LuaString ( lua. pop_ref ( ) ) . to_str ( ) ? . to_owned ( ) ) ) ,
1050- _ => Ok ( None ) ,
1050+ ffi:: LUA_TSTRING => Ok ( LuaString ( lua. pop_ref ( ) ) ) ,
1051+ _ => lua . create_string ( b"userdata" ) ,
10511052 }
10521053 }
10531054 }
@@ -1108,8 +1109,10 @@ impl AnyUserData {
11081109 match unsafe { self . invoke_tostring_dbg ( ) } {
11091110 Ok ( Some ( s) ) => write ! ( fmt, "{s}" ) ,
11101111 _ => {
1111- let name = self . type_name ( ) . ok ( ) . flatten ( ) ;
1112- let name = name. as_deref ( ) . unwrap_or ( "userdata" ) ;
1112+ let name = self . type_name ( ) . ok ( ) ;
1113+ let name = ( name. as_ref ( ) )
1114+ . map ( |s| Either :: Left ( s. display ( ) ) )
1115+ . unwrap_or ( Either :: Right ( "userdata" ) ) ;
11131116 write ! ( fmt, "{name}: {:?}" , self . to_pointer( ) )
11141117 }
11151118 }
0 commit comments