From df778b7b33c9648ddd0ad20edc6ee9e12dffca31 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Thu, 25 Jan 2024 18:04:55 +0000 Subject: [PATCH] Impl Into/FromLua for `OwnedString` --- src/conversion.rs | 35 ++++++++++++++++++++++++++++++++++- tests/conversion.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/conversion.rs b/src/conversion.rs index 3d26b34..4542e2d 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -22,7 +22,8 @@ use crate::value::{FromLua, IntoLua, Nil, Value}; #[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))] use crate::{ - function::OwnedFunction, table::OwnedTable, thread::OwnedThread, userdata::OwnedAnyUserData, + function::OwnedFunction, string::OwnedString, table::OwnedTable, thread::OwnedThread, + userdata::OwnedAnyUserData, }; impl<'lua> IntoLua<'lua> for Value<'lua> { @@ -71,6 +72,38 @@ impl<'lua> FromLua<'lua> for String<'lua> { } } +#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))] +impl<'lua> IntoLua<'lua> for OwnedString { + #[inline] + fn into_lua(self, lua: &'lua Lua) -> Result> { + Ok(Value::String(String(lua.adopt_owned_ref(self.0)))) + } +} + +#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))] +impl<'lua> IntoLua<'lua> for &OwnedString { + #[inline] + fn into_lua(self, lua: &'lua Lua) -> Result> { + OwnedString::into_lua(self.clone(), lua) + } + + #[inline] + unsafe fn push_into_stack(self, lua: &'lua Lua) -> Result<()> { + Ok(lua.push_owned_ref(&self.0)) + } +} + +#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))] +impl<'lua> FromLua<'lua> for OwnedString { + #[inline] + fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result { + String::from_lua(value, lua).map(|s| s.into_owned()) + } +} + impl<'lua> IntoLua<'lua> for Table<'lua> { #[inline] fn into_lua(self, _: &'lua Lua) -> Result> { diff --git a/tests/conversion.rs b/tests/conversion.rs index d3ee8f8..4e011ed 100644 --- a/tests/conversion.rs +++ b/tests/conversion.rs @@ -22,6 +22,35 @@ fn test_string_into_lua() -> Result<()> { Ok(()) } +#[cfg(all(feature = "unstable", not(feature = "send")))] +#[test] +fn test_owned_string_into_lua() -> Result<()> { + let lua = Lua::new(); + + // Direct conversion + let s = lua.create_string("hello, world")?.into_owned(); + let s2 = (&s).into_lua(&lua)?; + assert_eq!(s.to_ref(), *s2.as_string().unwrap()); + + // Push into stack + let table = lua.create_table()?; + table.set("s", &s)?; + assert_eq!(s.to_ref(), table.get::<_, String>("s")?); + + Ok(()) +} + +#[cfg(all(feature = "unstable", not(feature = "send")))] +#[test] +fn test_owned_string_from_lua() -> Result<()> { + let lua = Lua::new(); + + let s = lua.unpack::(lua.pack("hello, world")?)?; + assert_eq!(s.to_ref(), "hello, world"); + + Ok(()) +} + #[test] fn test_table_into_lua() -> Result<()> { let lua = Lua::new();