File tree Expand file tree Collapse file tree 10 files changed +100
-4
lines changed Expand file tree Collapse file tree 10 files changed +100
-4
lines changed Original file line number Diff line number Diff line change @@ -494,6 +494,16 @@ impl<'lua> Function<'lua> {
494
494
}
495
495
}
496
496
497
+ /// Converts this function to a generic C pointer.
498
+ ///
499
+ /// There is no way to convert the pointer back to its original value.
500
+ ///
501
+ /// Typically this function is used only for hashing and debug information.
502
+ #[ inline]
503
+ pub fn to_pointer ( & self ) -> * const c_void {
504
+ self . 0 . to_pointer ( )
505
+ }
506
+
497
507
/// Convert this handle to owned version.
498
508
#[ cfg( all( feature = "unstable" , any( not( feature = "send" ) , doc) ) ) ]
499
509
#[ cfg_attr( docsrs, doc( cfg( all( feature = "unstable" , not( feature = "send" ) ) ) ) ) ]
Original file line number Diff line number Diff line change @@ -132,7 +132,7 @@ impl<'lua> String<'lua> {
132
132
}
133
133
}
134
134
135
- /// Converts the string to a generic C pointer.
135
+ /// Converts this string to a generic C pointer.
136
136
///
137
137
/// There is no way to convert the pointer back to its original value.
138
138
///
Original file line number Diff line number Diff line change @@ -591,7 +591,7 @@ impl<'lua> Table<'lua> {
591
591
unsafe { ffi:: lua_getreadonly ( ref_thread, self . 0 . index ) != 0 }
592
592
}
593
593
594
- /// Converts the table to a generic C pointer.
594
+ /// Converts this table to a generic C pointer.
595
595
///
596
596
/// Different tables will give different pointers.
597
597
/// There is no way to convert the pointer back to its original value.
Original file line number Diff line number Diff line change 1
- use std:: os:: raw:: c_int;
1
+ use std:: os:: raw:: { c_int, c_void } ;
2
2
3
3
use crate :: error:: { Error , Result } ;
4
4
#[ allow( unused) ]
@@ -375,6 +375,16 @@ impl<'lua> Thread<'lua> {
375
375
}
376
376
}
377
377
378
+ /// Converts this thread to a generic C pointer.
379
+ ///
380
+ /// There is no way to convert the pointer back to its original value.
381
+ ///
382
+ /// Typically this function is used only for hashing and debug information.
383
+ #[ inline]
384
+ pub fn to_pointer ( & self ) -> * const c_void {
385
+ self . 0 . to_pointer ( )
386
+ }
387
+
378
388
/// Convert this handle to owned version.
379
389
#[ cfg( all( feature = "unstable" , any( not( feature = "send" ) , doc) ) ) ]
380
390
#[ cfg_attr( docsrs, doc( cfg( all( feature = "unstable" , not( feature = "send" ) ) ) ) ) ]
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ use std::fmt;
5
5
use std:: hash:: Hash ;
6
6
use std:: mem;
7
7
use std:: ops:: { Deref , DerefMut } ;
8
- use std:: os:: raw:: { c_char, c_int} ;
8
+ use std:: os:: raw:: { c_char, c_int, c_void } ;
9
9
use std:: string:: String as StdString ;
10
10
11
11
#[ cfg( feature = "async" ) ]
@@ -1096,6 +1096,16 @@ impl<'lua> AnyUserData<'lua> {
1096
1096
}
1097
1097
}
1098
1098
1099
+ /// Converts this userdata to a generic C pointer.
1100
+ ///
1101
+ /// There is no way to convert the pointer back to its original value.
1102
+ ///
1103
+ /// Typically this function is used only for hashing and debug information.
1104
+ #[ inline]
1105
+ pub fn to_pointer ( & self ) -> * const c_void {
1106
+ self . 0 . to_pointer ( )
1107
+ }
1108
+
1099
1109
/// Convert this handle to owned version.
1100
1110
#[ cfg( all( feature = "unstable" , any( not( feature = "send" ) , doc) ) ) ]
1101
1111
#[ cfg_attr( docsrs, doc( cfg( all( feature = "unstable" , not( feature = "send" ) ) ) ) ) ]
Original file line number Diff line number Diff line change @@ -231,6 +231,19 @@ fn test_function_info() -> Result<()> {
231
231
Ok ( ( ) )
232
232
}
233
233
234
+ #[ test]
235
+ fn test_function_pointer ( ) -> Result < ( ) > {
236
+ let lua = Lua :: new ( ) ;
237
+
238
+ let func1 = lua. load ( "return function() end" ) . into_function ( ) ?;
239
+ let func2 = func1. call :: < _ , Function > ( ( ) ) ?;
240
+
241
+ assert_eq ! ( func1. to_pointer( ) , func1. clone( ) . to_pointer( ) ) ;
242
+ assert_ne ! ( func1. to_pointer( ) , func2. to_pointer( ) ) ;
243
+
244
+ Ok ( ( ) )
245
+ }
246
+
234
247
#[ test]
235
248
fn test_function_wrap ( ) -> Result < ( ) > {
236
249
use mlua:: Error ;
Original file line number Diff line number Diff line change @@ -99,6 +99,19 @@ fn test_string_debug() -> Result<()> {
99
99
Ok ( ( ) )
100
100
}
101
101
102
+ #[ test]
103
+ fn test_string_pointer ( ) -> Result < ( ) > {
104
+ let lua = Lua :: new ( ) ;
105
+
106
+ let str1 = lua. create_string ( "hello" ) ?;
107
+ let str2 = lua. create_string ( "hello" ) ?;
108
+
109
+ // Lua uses string interning, so these should be the same
110
+ assert_eq ! ( str1. to_pointer( ) , str2. to_pointer( ) ) ;
111
+
112
+ Ok ( ( ) )
113
+ }
114
+
102
115
#[ cfg( all( feature = "unstable" , not( feature = "send" ) ) ) ]
103
116
#[ test]
104
117
fn test_owned_string ( ) -> Result < ( ) > {
Original file line number Diff line number Diff line change @@ -369,6 +369,19 @@ fn test_table_eq() -> Result<()> {
369
369
Ok ( ( ) )
370
370
}
371
371
372
+ #[ test]
373
+ fn test_table_pointer ( ) -> Result < ( ) > {
374
+ let lua = Lua :: new ( ) ;
375
+
376
+ let table1 = lua. create_table ( ) ?;
377
+ let table2 = lua. create_table ( ) ?;
378
+
379
+ assert_eq ! ( table1. to_pointer( ) , table1. clone( ) . to_pointer( ) ) ;
380
+ assert_ne ! ( table1. to_pointer( ) , table2. to_pointer( ) ) ;
381
+
382
+ Ok ( ( ) )
383
+ }
384
+
372
385
#[ test]
373
386
fn test_table_error ( ) -> Result < ( ) > {
374
387
let lua = Lua :: new ( ) ;
Original file line number Diff line number Diff line change @@ -191,6 +191,19 @@ fn test_coroutine_panic() {
191
191
}
192
192
}
193
193
194
+ #[ test]
195
+ fn test_thread_pointer ( ) -> Result < ( ) > {
196
+ let lua = Lua :: new ( ) ;
197
+
198
+ let func = lua. load ( "return 123" ) . into_function ( ) ?;
199
+ let thread = lua. create_thread ( func. clone ( ) ) ?;
200
+
201
+ assert_eq ! ( thread. to_pointer( ) , thread. clone( ) . to_pointer( ) ) ;
202
+ assert_ne ! ( thread. to_pointer( ) , lua. current_thread( ) . to_pointer( ) ) ;
203
+
204
+ Ok ( ( ) )
205
+ }
206
+
194
207
#[ cfg( all( feature = "unstable" , not( feature = "send" ) ) ) ]
195
208
#[ test]
196
209
fn test_owned_thread ( ) -> Result < ( ) > {
Original file line number Diff line number Diff line change @@ -951,6 +951,20 @@ fn test_userdata_method_errors() -> Result<()> {
951
951
Ok ( ( ) )
952
952
}
953
953
954
+ #[ test]
955
+ fn test_userdata_pointer ( ) -> Result < ( ) > {
956
+ let lua = Lua :: new ( ) ;
957
+
958
+ let ud1 = lua. create_any_userdata ( "hello" ) ?;
959
+ let ud2 = lua. create_any_userdata ( "hello" ) ?;
960
+
961
+ assert_eq ! ( ud1. to_pointer( ) , ud1. clone( ) . to_pointer( ) ) ;
962
+ // Different userdata objects with the same value should have different pointers
963
+ assert_ne ! ( ud1. to_pointer( ) , ud2. to_pointer( ) ) ;
964
+
965
+ Ok ( ( ) )
966
+ }
967
+
954
968
#[ cfg( all( feature = "unstable" , not( feature = "send" ) ) ) ]
955
969
#[ test]
956
970
fn test_owned_userdata ( ) -> Result < ( ) > {
You can’t perform that action at this time.
0 commit comments