Skip to content

Commit 2ac7b23

Browse files
committed
Impl Into/FromLua for OwnedThread
1 parent 8200bee commit 2ac7b23

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/conversion.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ use crate::userdata::{AnyUserData, UserData, UserDataRef, UserDataRefMut};
2121
use crate::value::{FromLua, IntoLua, Nil, Value};
2222

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

2628
impl<'lua> IntoLua<'lua> for Value<'lua> {
2729
#[inline]
@@ -232,6 +234,38 @@ impl<'lua> FromLua<'lua> for Thread<'lua> {
232234
}
233235
}
234236

237+
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
238+
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
239+
impl<'lua> IntoLua<'lua> for OwnedThread {
240+
#[inline]
241+
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
242+
Ok(Value::Thread(Thread(lua.adopt_owned_ref(self.0), self.1)))
243+
}
244+
}
245+
246+
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
247+
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
248+
impl<'lua> IntoLua<'lua> for &OwnedThread {
249+
#[inline]
250+
fn into_lua(self, lua: &'lua Lua) -> Result<Value<'lua>> {
251+
OwnedThread::into_lua(self.clone(), lua)
252+
}
253+
254+
#[inline]
255+
unsafe fn push_into_stack(self, lua: &'lua Lua) -> Result<()> {
256+
Ok(lua.push_owned_ref(&self.0))
257+
}
258+
}
259+
260+
#[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))]
261+
#[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))]
262+
impl<'lua> FromLua<'lua> for OwnedThread {
263+
#[inline]
264+
fn from_lua(value: Value<'lua>, lua: &'lua Lua) -> Result<OwnedThread> {
265+
Thread::from_lua(value, lua).map(|s| s.into_owned())
266+
}
267+
}
268+
235269
impl<'lua> IntoLua<'lua> for AnyUserData<'lua> {
236270
#[inline]
237271
fn into_lua(self, _: &'lua Lua) -> Result<Value<'lua>> {

tests/conversion.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,36 @@ fn test_thread_into_lua() -> Result<()> {
112112
Ok(())
113113
}
114114

115+
#[cfg(all(feature = "unstable", not(feature = "send")))]
116+
#[test]
117+
fn test_owned_thread_into_lua() -> Result<()> {
118+
let lua = Lua::new();
119+
120+
// Direct conversion
121+
let f = lua.create_function(|_, ()| Ok::<_, Error>(()))?;
122+
let th = lua.create_thread(f)?.into_owned();
123+
let th2 = (&th).into_lua(&lua)?;
124+
assert_eq!(&th.to_ref(), th2.as_thread().unwrap());
125+
126+
// Push into stack
127+
let table = lua.create_table()?;
128+
table.set("th", &th)?;
129+
assert_eq!(th.to_ref(), table.get::<_, Thread>("th")?);
130+
131+
Ok(())
132+
}
133+
134+
#[cfg(all(feature = "unstable", not(feature = "send")))]
135+
#[test]
136+
fn test_owned_thread_from_lua() -> Result<()> {
137+
let lua = Lua::new();
138+
139+
let th = lua.unpack::<mlua::OwnedThread>(Value::Thread(lua.current_thread()))?;
140+
assert_eq!(th.to_ref(), lua.current_thread());
141+
142+
Ok(())
143+
}
144+
115145
#[test]
116146
fn test_anyuserdata_into_lua() -> Result<()> {
117147
let lua = Lua::new();

0 commit comments

Comments
 (0)