Skip to content

Commit 83c075c

Browse files
committed
Implement IntoLua for RegistryKey
1 parent 270b98a commit 83c075c

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/conversion.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ impl<'lua> FromLua<'lua> for Error {
421421
}
422422
}
423423

424+
impl<'lua> IntoLua<'lua> for RegistryKey {
425+
#[inline]
426+
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
427+
lua.registry_value(&self)
428+
}
429+
430+
#[inline]
431+
unsafe fn push_into_stack(self, lua: &'lua Lua) -> Result<()> {
432+
<&RegistryKey>::push_into_stack(&self, lua)
433+
}
434+
}
435+
424436
impl<'lua> IntoLua<'lua> for &RegistryKey {
425437
#[inline]
426438
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {

tests/conversion.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,22 @@ fn test_owned_anyuserdata_into_lua() -> Result<()> {
232232
fn test_registry_value_into_lua() -> Result<()> {
233233
let lua = Lua::new();
234234

235-
let t = lua.create_table()?;
236-
let r = lua.create_registry_value(t)?;
237-
let f = lua.create_function(|_, t: Table| t.raw_set("hello", "world"))?;
235+
// Direct conversion
236+
let s = lua.create_string("hello, world")?;
237+
let r = lua.create_registry_value(&s)?;
238+
let value1 = lua.pack(&r)?;
239+
let value2 = lua.pack(r)?;
240+
assert_eq!(value1.as_str(), Some("hello, world"));
241+
assert_eq!(value2.to_pointer(), value2.to_pointer());
238242

239-
f.call(&r)?;
240-
let v = r.into_lua(&lua)?;
241-
let t = v.as_table().unwrap();
243+
// Push into stack
244+
let t = lua.create_table()?;
245+
let r = lua.create_registry_value(&t)?;
246+
let f = lua.create_function(|_, (t, k, v): (Table, Value, Value)| t.set(k, v))?;
247+
f.call((&r, "hello", "world"))?;
248+
f.call((r, "welcome", "to the jungle"))?;
242249
assert_eq!(t.get::<_, String>("hello")?, "world");
250+
assert_eq!(t.get::<_, String>("welcome")?, "to the jungle");
243251

244252
// Try to set nil registry key
245253
let r_nil = lua.create_registry_value(Value::Nil)?;

0 commit comments

Comments
 (0)