Skip to content

Commit

Permalink
Add to_pointer function to Function/Table/Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Feb 2, 2024
1 parent f4d783c commit 908f376
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ impl<'lua> Function<'lua> {
}
}

/// Converts this function to a generic C pointer.
///
/// There is no way to convert the pointer back to its original value.
///
/// Typically this function is used only for hashing and debug information.
#[inline]
pub fn to_pointer(&self) -> *const c_void {
self.0.to_pointer()
}

/// Convert this handle to owned version.
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
Expand Down
2 changes: 1 addition & 1 deletion src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'lua> String<'lua> {
}
}

/// Converts the string to a generic C pointer.
/// Converts this string to a generic C pointer.
///
/// There is no way to convert the pointer back to its original value.
///
Expand Down
2 changes: 1 addition & 1 deletion src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ impl<'lua> Table<'lua> {
unsafe { ffi::lua_getreadonly(ref_thread, self.0.index) != 0 }
}

/// Converts the table to a generic C pointer.
/// Converts this table to a generic C pointer.
///
/// Different tables will give different pointers.
/// There is no way to convert the pointer back to its original value.
Expand Down
12 changes: 11 additions & 1 deletion src/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::os::raw::c_int;
use std::os::raw::{c_int, c_void};

use crate::error::{Error, Result};
#[allow(unused)]
Expand Down Expand Up @@ -375,6 +375,16 @@ impl<'lua> Thread<'lua> {
}
}

/// Converts this thread to a generic C pointer.
///
/// There is no way to convert the pointer back to its original value.
///
/// Typically this function is used only for hashing and debug information.
#[inline]
pub fn to_pointer(&self) -> *const c_void {
self.0.to_pointer()
}

/// Convert this handle to owned version.
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
Expand Down
12 changes: 11 additions & 1 deletion src/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
use std::hash::Hash;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::os::raw::{c_char, c_int};
use std::os::raw::{c_char, c_int, c_void};
use std::string::String as StdString;

#[cfg(feature = "async")]
Expand Down Expand Up @@ -1096,6 +1096,16 @@ impl<'lua> AnyUserData<'lua> {
}
}

/// Converts this userdata to a generic C pointer.
///
/// There is no way to convert the pointer back to its original value.
///
/// Typically this function is used only for hashing and debug information.
#[inline]
pub fn to_pointer(&self) -> *const c_void {
self.0.to_pointer()
}

/// Convert this handle to owned version.
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
Expand Down
13 changes: 13 additions & 0 deletions tests/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ fn test_function_info() -> Result<()> {
Ok(())
}

#[test]
fn test_function_pointer() -> Result<()> {
let lua = Lua::new();

let func1 = lua.load("return function() end").into_function()?;
let func2 = func1.call::<_, Function>(())?;

assert_eq!(func1.to_pointer(), func1.clone().to_pointer());
assert_ne!(func1.to_pointer(), func2.to_pointer());

Ok(())
}

#[test]
fn test_function_wrap() -> Result<()> {
use mlua::Error;
Expand Down
13 changes: 13 additions & 0 deletions tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ fn test_string_debug() -> Result<()> {
Ok(())
}

#[test]
fn test_string_pointer() -> Result<()> {
let lua = Lua::new();

let str1 = lua.create_string("hello")?;
let str2 = lua.create_string("hello")?;

// Lua uses string interning, so these should be the same
assert_eq!(str1.to_pointer(), str2.to_pointer());

Ok(())
}

#[cfg(all(feature = "unstable", not(feature = "send")))]
#[test]
fn test_owned_string() -> Result<()> {
Expand Down
13 changes: 13 additions & 0 deletions tests/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ fn test_table_eq() -> Result<()> {
Ok(())
}

#[test]
fn test_table_pointer() -> Result<()> {
let lua = Lua::new();

let table1 = lua.create_table()?;
let table2 = lua.create_table()?;

assert_eq!(table1.to_pointer(), table1.clone().to_pointer());
assert_ne!(table1.to_pointer(), table2.to_pointer());

Ok(())
}

#[test]
fn test_table_error() -> Result<()> {
let lua = Lua::new();
Expand Down
13 changes: 13 additions & 0 deletions tests/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ fn test_coroutine_panic() {
}
}

#[test]
fn test_thread_pointer() -> Result<()> {
let lua = Lua::new();

let func = lua.load("return 123").into_function()?;
let thread = lua.create_thread(func.clone())?;

assert_eq!(thread.to_pointer(), thread.clone().to_pointer());
assert_ne!(thread.to_pointer(), lua.current_thread().to_pointer());

Ok(())
}

#[cfg(all(feature = "unstable", not(feature = "send")))]
#[test]
fn test_owned_thread() -> Result<()> {
Expand Down
14 changes: 14 additions & 0 deletions tests/userdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,20 @@ fn test_userdata_method_errors() -> Result<()> {
Ok(())
}

#[test]
fn test_userdata_pointer() -> Result<()> {
let lua = Lua::new();

let ud1 = lua.create_any_userdata("hello")?;
let ud2 = lua.create_any_userdata("hello")?;

assert_eq!(ud1.to_pointer(), ud1.clone().to_pointer());
// Different userdata objects with the same value should have different pointers
assert_ne!(ud1.to_pointer(), ud2.to_pointer());

Ok(())
}

#[cfg(all(feature = "unstable", not(feature = "send")))]
#[test]
fn test_owned_userdata() -> Result<()> {
Expand Down

0 comments on commit 908f376

Please sign in to comment.