Skip to content

Commit 727f99e

Browse files
committed
Implement IntoLua for &RegistryKey
This would allow just passing registry keys to arguments with fasttrack to push directly into stack.
1 parent 3c801e7 commit 727f99e

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/conversion.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::lua::Lua;
1616
use crate::string::String;
1717
use crate::table::Table;
1818
use crate::thread::Thread;
19-
use crate::types::{LightUserData, MaybeSend};
19+
use crate::types::{LightUserData, MaybeSend, RegistryKey};
2020
use crate::userdata::{AnyUserData, UserData, UserDataRef, UserDataRefMut};
2121
use crate::value::{FromLua, IntoLua, Nil, Value};
2222

@@ -240,6 +240,26 @@ impl<'lua> FromLua<'lua> for Error {
240240
}
241241
}
242242

243+
impl<'lua> IntoLua<'lua> for &RegistryKey {
244+
#[inline]
245+
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
246+
lua.registry_value(self)
247+
}
248+
249+
unsafe fn push_into_stack(self, lua: &'lua Lua) -> Result<()> {
250+
if !lua.owns_registry_value(self) {
251+
return Err(Error::MismatchedRegistryKey);
252+
}
253+
254+
if self.is_nil() {
255+
ffi::lua_pushnil(lua.state());
256+
} else {
257+
ffi::lua_rawgeti(lua.state(), ffi::LUA_REGISTRYINDEX, self.registry_id as _);
258+
}
259+
Ok(())
260+
}
261+
}
262+
243263
impl<'lua> IntoLua<'lua> for bool {
244264
#[inline]
245265
fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {

tests/tests.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use std::sync::Arc;
88
use std::{error, f32, f64, fmt};
99

1010
use mlua::{
11-
ChunkMode, Error, ExternalError, Function, Lua, LuaOptions, Nil, Result, StdLib, String, Table,
12-
UserData, Value, Variadic,
11+
ChunkMode, Error, ExternalError, Function, IntoLua, Lua, LuaOptions, Nil, Result, StdLib,
12+
String, Table, UserData, Value, Variadic,
1313
};
1414

1515
#[cfg(not(feature = "luau"))]
@@ -779,6 +779,35 @@ fn test_registry_value() -> Result<()> {
779779
Ok(())
780780
}
781781

782+
#[test]
783+
fn test_registry_value_into_lua() -> Result<()> {
784+
let lua = Lua::new();
785+
786+
let t = lua.create_table()?;
787+
let r = lua.create_registry_value(t)?;
788+
let f = lua.create_function(|_, t: Table| t.raw_set("hello", "world"))?;
789+
790+
f.call(&r)?;
791+
let v = r.into_lua(&lua)?;
792+
let t = v.as_table().unwrap();
793+
assert_eq!(t.get::<_, String>("hello")?, "world");
794+
795+
// Try to set nil registry key
796+
let r_nil = lua.create_registry_value(Value::Nil)?;
797+
t.set("hello", &r_nil)?;
798+
assert_eq!(t.get::<_, Value>("hello")?, Value::Nil);
799+
800+
// Check non-owned registry key
801+
let lua2 = Lua::new();
802+
let r2 = lua2.create_registry_value("abc")?;
803+
assert!(matches!(
804+
f.call::<_, ()>(&r2),
805+
Err(Error::MismatchedRegistryKey)
806+
));
807+
808+
Ok(())
809+
}
810+
782811
#[test]
783812
fn test_drop_registry_value() -> Result<()> {
784813
struct MyUserdata(Arc<()>);

0 commit comments

Comments
 (0)