luau 0.732.0

Safe lifetime-bound Rust embedding API for the Luau runtime
use luau_vm::Thread as VmThread;

use super::Thread;
use crate::error::Error;
use crate::value::{FromLua, IntoLua, LuaType, Value};

impl AsRef<VmThread> for Thread<'_> {
    fn as_ref(&self) -> &VmThread {
        self.as_vm()
    }
}

impl LuaType for Thread<'_> {
    fn push_type_key(thread: impl AsRef<VmThread>) -> Result<(), Error> {
        let thread = thread.as_ref();
        unsafe {
            thread
                .push_thread()
                .map(drop)
                .map_err(|exit| Error::from_thread_exit(thread, exit))
        }
    }
}

impl<'lua, 'thread> IntoLua<'lua> for Thread<'thread>
where
    'thread: 'lua,
{
    fn into_lua(self, _: crate::LuaRef<'lua>) -> Result<Value<'lua>, Error> {
        Ok(Value::Thread(self))
    }

    unsafe fn push_into_stack(self, thread: &Thread<'lua>) -> Result<(), Error> {
        self.push_to(thread)
    }
}

impl<'lua, 'thread> IntoLua<'lua> for &Thread<'thread>
where
    'thread: 'lua,
{
    fn into_lua(self, _: crate::LuaRef<'lua>) -> Result<Value<'lua>, Error> {
        self.try_clone().map(Value::Thread)
    }

    unsafe fn push_into_stack(self, thread: &Thread<'lua>) -> Result<(), Error> {
        self.push_to(thread)
    }
}

impl<'lua> FromLua<'lua> for Thread<'lua> {
    fn from_lua(value: Value<'lua>, _: crate::LuaRef<'lua>) -> Result<Self, Error> {
        match value {
            Value::Thread(thread) => Ok(thread),
            value => Err(Error::from_lua_conversion(
                value.type_name(),
                "thread",
                None,
            )),
        }
    }
}