Skip to content

Commit df778b7

Browse files
committed
Impl Into/FromLua for OwnedString
1 parent 45299c0 commit df778b7

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/conversion.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use crate::value::{FromLua, IntoLua, Nil, Value};
2222

2323
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
2424
use crate::{
25-
function::OwnedFunction, table::OwnedTable, thread::OwnedThread, userdata::OwnedAnyUserData,
25+
function::OwnedFunction, string::OwnedString, table::OwnedTable, thread::OwnedThread,
26+
userdata::OwnedAnyUserData,
2627
};
2728

2829
impl<'lua> IntoLua<'lua> for Value<'lua> {
@@ -71,6 +72,38 @@ impl<'lua> FromLua<'lua> for String<'lua> {
7172
}
7273
}
7374

75+
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
76+
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
77+
impl<'lua> IntoLua<'lua> for OwnedString {
78+
#[inline]
79+
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
80+
Ok(Value::String(String(lua.adopt_owned_ref(self.0))))
81+
}
82+
}
83+
84+
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
85+
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
86+
impl<'lua> IntoLua<'lua> for &OwnedString {
87+
#[inline]
88+
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
89+
OwnedString::into_lua(self.clone(), lua)
90+
}
91+
92+
#[inline]
93+
unsafe fn push_into_stack(self, lua: &'lua Lua) -> Result<()> {
94+
Ok(lua.push_owned_ref(&self.0))
95+
}
96+
}
97+
98+
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
99+
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
100+
impl<'lua> FromLua<'lua> for OwnedString {
101+
#[inline]
102+
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedString> {
103+
String::from_lua(value, lua).map(|s| s.into_owned())
104+
}
105+
}
106+
74107
impl<'lua> IntoLua<'lua> for Table<'lua> {
75108
#[inline]
76109
fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {

tests/conversion.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@ fn test_string_into_lua() -> Result<()> {
2222
Ok(())
2323
}
2424

25+
#[cfg(all(feature = "unstable", not(feature = "send")))]
26+
#[test]
27+
fn test_owned_string_into_lua() -> Result<()> {
28+
let lua = Lua::new();
29+
30+
// Direct conversion
31+
let s = lua.create_string("hello, world")?.into_owned();
32+
let s2 = (&s).into_lua(&lua)?;
33+
assert_eq!(s.to_ref(), *s2.as_string().unwrap());
34+
35+
// Push into stack
36+
let table = lua.create_table()?;
37+
table.set("s", &s)?;
38+
assert_eq!(s.to_ref(), table.get::<_, String>("s")?);
39+
40+
Ok(())
41+
}
42+
43+
#[cfg(all(feature = "unstable", not(feature = "send")))]
44+
#[test]
45+
fn test_owned_string_from_lua() -> Result<()> {
46+
let lua = Lua::new();
47+
48+
let s = lua.unpack::<mlua::OwnedString>(lua.pack("hello, world")?)?;
49+
assert_eq!(s.to_ref(), "hello, world");
50+
51+
Ok(())
52+
}
53+
2554
#[test]
2655
fn test_table_into_lua() -> Result<()> {
2756
let lua = Lua::new();

0 commit comments

Comments
 (0)