diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2023-08-12 21:18:12 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2023-08-12 21:18:12 +0100 |
commit | d48a2b3f6ccc8f5b029654cd2611d5aee1cb5936 (patch) | |
tree | e72ef1137153660fe97cf97aba90e12385795e6b /src/thread.rs | |
parent | b3592bc23e08319f830c0c633c95fa8be51c4820 (diff) | |
download | mlua-d48a2b3f6ccc8f5b029654cd2611d5aee1cb5936.zip |
Add `OwnedThread` type (unstable)
Diffstat (limited to 'src/thread.rs')
-rw-r--r-- | src/thread.rs | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/src/thread.rs b/src/thread.rs index cdce77c..bf051fc 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -26,7 +26,7 @@ use { }, }; -/// Status of a Lua thread (or coroutine). +/// Status of a Lua thread (coroutine). #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum ThreadStatus { /// The thread was just created, or is suspended because it has called `coroutine.yield`. @@ -41,10 +41,34 @@ pub enum ThreadStatus { Error, } -/// Handle to an internal Lua thread (or coroutine). +/// Handle to an internal Lua thread (coroutine). #[derive(Clone, Debug)] pub struct Thread<'lua>(pub(crate) LuaRef<'lua>, pub(crate) *mut ffi::lua_State); +/// Owned handle to an internal Lua thread (coroutine). +/// +/// The owned handle holds a *strong* reference to the current Lua instance. +/// Be warned, if you place it into a Lua type (eg. [`UserData`] or a Rust callback), it is *very easy* +/// to accidentally cause reference cycles that would prevent destroying Lua instance. +/// +/// [`UserData`]: crate::UserData +#[cfg(feature = "unstable")] +#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))] +#[derive(Clone, Debug)] +pub struct OwnedThread( + pub(crate) crate::types::LuaOwnedRef, + pub(crate) *mut ffi::lua_State, +); + +#[cfg(feature = "unstable")] +impl OwnedThread { + /// Get borrowed handle to the underlying Lua table. + #[cfg_attr(feature = "send", allow(unused))] + pub const fn to_ref(&self) -> Thread { + Thread(self.0.to_ref(), self.1) + } +} + /// Thread (coroutine) representation as an async [`Future`] or [`Stream`]. /// /// Requires `feature = "async"` @@ -350,6 +374,14 @@ impl<'lua> Thread<'lua> { protect_lua!(state, 0, 0, |_| ffi::luaL_sandboxthread(thread_state)) } } + + /// Convert this handle to owned version. + #[cfg(all(feature = "unstable", any(not(feature = "send"), doc)))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "unstable", not(feature = "send")))))] + #[inline] + pub fn into_owned(self) -> OwnedThread { + OwnedThread(self.0.into_owned(), self.1) + } } impl<'lua> PartialEq for Thread<'lua> { @@ -358,6 +390,26 @@ impl<'lua> PartialEq for Thread<'lua> { } } +// Additional shortcuts +#[cfg(feature = "unstable")] +impl OwnedThread { + /// Resumes execution of this thread. + /// + /// See [`Thread::resume()`] for more details. + pub fn resume<'lua, A, R>(&'lua self, args: A) -> Result<R> + where + A: IntoLuaMulti<'lua>, + R: FromLuaMulti<'lua>, + { + self.to_ref().resume(args) + } + + /// Gets the status of the thread. + pub fn status(&self) -> ThreadStatus { + self.to_ref().status() + } +} + #[cfg(feature = "async")] impl<'lua, R> AsyncThread<'lua, R> { #[inline] |