File tree 2 files changed +14
-3
lines changed
2 files changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -623,10 +623,9 @@ impl AnyUserData {
623
623
/// Checks whether the type of this userdata is `T`.
624
624
#[ inline]
625
625
pub fn is < T : ' static > ( & self ) -> bool {
626
- let lua = self . 0 . lua . lock ( ) ;
627
- let type_id = lua. get_userdata_ref_type_id ( & self . 0 ) ;
626
+ let type_id = self . type_id ( ) ;
628
627
// We do not use wrapped types here, rather prefer to check the "real" type of the userdata
629
- matches ! ( type_id, Ok ( Some ( type_id) ) if type_id == TypeId :: of:: <T >( ) )
628
+ matches ! ( type_id, Some ( type_id) if type_id == TypeId :: of:: <T >( ) )
630
629
}
631
630
632
631
/// Borrow this userdata immutably if it is of type `T`.
@@ -918,6 +917,15 @@ impl AnyUserData {
918
917
self . 0 . to_pointer ( )
919
918
}
920
919
920
+ /// Returns [`TypeId`] of this userdata if it is registered and `'static`.
921
+ ///
922
+ /// This method is not available for scoped userdata.
923
+ #[ inline]
924
+ pub fn type_id ( & self ) -> Option < TypeId > {
925
+ let lua = self . 0 . lua . lock ( ) ;
926
+ lua. get_userdata_ref_type_id ( & self . 0 ) . ok ( ) . flatten ( )
927
+ }
928
+
921
929
/// Returns a type name of this `UserData` (from a metatable field).
922
930
pub ( crate ) fn type_name ( & self ) -> Result < Option < StdString > > {
923
931
let lua = self . 0 . lua . lock ( ) ;
Original file line number Diff line number Diff line change
1
+ use std:: any:: TypeId ;
1
2
use std:: collections:: HashMap ;
2
3
use std:: string:: String as StdString ;
3
4
use std:: sync:: Arc ;
@@ -23,9 +24,11 @@ fn test_userdata() -> Result<()> {
23
24
let userdata2 = lua. create_userdata ( UserData2 ( Box :: new ( 2 ) ) ) ?;
24
25
25
26
assert ! ( userdata1. is:: <UserData1 >( ) ) ;
27
+ assert ! ( userdata1. type_id( ) == Some ( TypeId :: of:: <UserData1 >( ) ) ) ;
26
28
assert ! ( !userdata1. is:: <UserData2 >( ) ) ;
27
29
assert ! ( userdata2. is:: <UserData2 >( ) ) ;
28
30
assert ! ( !userdata2. is:: <UserData1 >( ) ) ;
31
+ assert ! ( userdata2. type_id( ) == Some ( TypeId :: of:: <UserData2 >( ) ) ) ;
29
32
30
33
assert_eq ! ( userdata1. borrow:: <UserData1 >( ) ?. 0 , 1 ) ;
31
34
assert_eq ! ( * userdata2. borrow:: <UserData2 >( ) ?. 0 , 2 ) ;
You can’t perform that action at this time.
0 commit comments