Skip to content

Commit 908f376

Browse files
committed
Add to_pointer function to Function/Table/Thread
1 parent f4d783c commit 908f376

File tree

10 files changed

+100
-4
lines changed

10 files changed

+100
-4
lines changed

src/function.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,16 @@ impl<'lua> Function<'lua> {
494494
}
495495
}
496496

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+
497507
/// Convert this handle to owned version.
498508
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
499509
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]

src/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'lua> String<'lua> {
132132
}
133133
}
134134

135-
/// Converts the string to a generic C pointer.
135+
/// Converts this string to a generic C pointer.
136136
///
137137
/// There is no way to convert the pointer back to its original value.
138138
///

src/table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ impl<'lua> Table<'lua> {
591591
unsafe { ffi::lua_getreadonly(ref_thread, self.0.index) != 0 }
592592
}
593593

594-
/// Converts the table to a generic C pointer.
594+
/// Converts this table to a generic C pointer.
595595
///
596596
/// Different tables will give different pointers.
597597
/// There is no way to convert the pointer back to its original value.

src/thread.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::os::raw::c_int;
1+
use std::os::raw::{c_int, c_void};
22

33
use crate::error::{Error, Result};
44
#[allow(unused)]
@@ -375,6 +375,16 @@ impl<'lua> Thread<'lua> {
375375
}
376376
}
377377

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+
378388
/// Convert this handle to owned version.
379389
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
380390
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]

src/userdata.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt;
55
use std::hash::Hash;
66
use std::mem;
77
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};
99
use std::string::String as StdString;
1010

1111
#[cfg(feature = "async")]
@@ -1096,6 +1096,16 @@ impl<'lua> AnyUserData<'lua> {
10961096
}
10971097
}
10981098

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+
10991109
/// Convert this handle to owned version.
11001110
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
11011111
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]

tests/function.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ fn test_function_info() -> Result<()> {
231231
Ok(())
232232
}
233233

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+
234247
#[test]
235248
fn test_function_wrap() -> Result<()> {
236249
use mlua::Error;

tests/string.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ fn test_string_debug() -> Result<()> {
9999
Ok(())
100100
}
101101

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+
102115
#[cfg(all(feature = "unstable", not(feature = "send")))]
103116
#[test]
104117
fn test_owned_string() -> Result<()> {

tests/table.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ fn test_table_eq() -> Result<()> {
369369
Ok(())
370370
}
371371

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+
372385
#[test]
373386
fn test_table_error() -> Result<()> {
374387
let lua = Lua::new();

tests/thread.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ fn test_coroutine_panic() {
191191
}
192192
}
193193

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+
194207
#[cfg(all(feature = "unstable", not(feature = "send")))]
195208
#[test]
196209
fn test_owned_thread() -> Result<()> {

tests/userdata.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,20 @@ fn test_userdata_method_errors() -> Result<()> {
951951
Ok(())
952952
}
953953

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+
954968
#[cfg(all(feature = "unstable", not(feature = "send")))]
955969
#[test]
956970
fn test_owned_userdata() -> Result<()> {

0 commit comments

Comments
 (0)