pub fn resume<A, R>(&self, args: A) -> Result<R>
    where
        A: ToLuaMulti<'lua>,
        R: FromLuaMulti<'lua>,
    {
        let lua = self.0.lua;
        let mut args = args.to_lua_multi(lua)?;
        let nargs = args.len() as c_int;
        let results = unsafe {
            let _sg = StackGuard::new(lua.state);
            check_stack(lua.state, cmp::max(nargs + 1, 3))?;

            let thread_state =
                lua.ref_thread_exec(|ref_thread| ffi::lua_tothread(ref_thread, self.0.index));

            let status = ffi::lua_status(thread_state);
            if status != ffi::LUA_YIELD && ffi::lua_gettop(thread_state) == 0 {
                return Err(Error::CoroutineInactive);
            }

            check_stack(thread_state, nargs)?;
            for arg in args.drain_all() {
                lua.push_value(arg)?;
            }
            ffi::lua_xmove(lua.state, thread_state, nargs);

            let mut nresults = 0;

            let ret = ffi::lua_resume(thread_state, lua.state, nargs, &mut nresults as *mut c_int);
            if ret != ffi::LUA_OK && ret != ffi::LUA_YIELD {
                protect_lua!(lua.state, 0, 0, |_| error_traceback(thread_state))?;
                return Err(pop_error(thread_state, ret));
            }

            let mut results = args; // Reuse MultiValue container
            check_stack(lua.state, nresults + 2)?; // 2 is extra for `lua.pop_value()` below
            ffi::lua_xmove(thread_state, lua.state, nresults);

            for _ in 0..nresults {
                results.push_front(lua.pop_value());
            }
            results
        };
        R::from_lua_multi(results, lua)
    }
/// Resumes execution of this thread.
///
/// Equivalent to `coroutine.resume`.
///
/// Passes `args` as arguments to the thread. If the coroutine has called `coroutine.yield`, it
/// will return these arguments. Otherwise, the coroutine wasn't yet started, so the arguments
/// are passed to its main function.
///
/// If the thread is no longer in `Active` state (meaning it has finished execution or
/// encountered an error), this will return `Err(CoroutineInactive)`, otherwise will return `Ok`
/// as follows:
///
/// If the thread calls `coroutine.yield`, returns the values passed to `yield`. If the thread
/// `return`s values from its main function, returns those.
///
/// # Examples
///
/// ```
/// # use mlua::{Error, Lua, Result, Thread};
/// # fn main() -> Result<()> {
/// # let lua = Lua::new();
/// let thread: Thread = lua.load(r#"
///     coroutine.create(function(arg)
///         assert(arg == 42)
///         local yieldarg = coroutine.yield(123)
///         assert(yieldarg == 43)
///         return 987
///     end)
/// "#).eval()?;
///
/// assert_eq!(thread.resume::<_, u32>(42)?, 123);
/// assert_eq!(thread.resume::<_, u32>(43)?, 987);
///
/// // The coroutine has now returned, so `resume` will fail
/// match thread.resume::<_, u32>(()) {
///     Err(Error::CoroutineInactive) => {},
///     unexpected => panic!("unexpected result {:?}", unexpected),
/// }
/// # Ok(())
/// # }
/// ```
Resumes execution of this thread.

Passes `args` as arguments to the thread. If the coroutine has called `coroutine.yield`, it
will return these arguments. Otherwise, the coroutine wasn't yet started, so the arguments
are passed to its main function.



If the thread calls `coroutine.yield`, returns the values passed to `yield`. If the thread
`return`s values from its main function, returns those.
Resumes execution of this thread . Passes ` args ` as arguments to the thread . If the coroutine has called ` coroutine . yield ` it will return these arguments . Otherwise the coroutine wasn ' t yet started so the arguments are passed to its main function . If the thread calls ` coroutine . yield ` returns the values passed to ` yield ` . If the thread ` return ` s values from its main function returns those .
pub fn resume<A, R>(&self, args: A) -> Result<R>
    where
        A: ToLuaMulti<'lua>,
        R: FromLuaMulti<'lua>,
    {
        let lua = self.0.lua;
        let mut args = args.to_lua_multi(lua)?;
        let nargs = args.len() as c_int;
        let results = unsafe {
            let _sg = StackGuard::new(lua.state);
            check_stack(lua.state, cmp::max(nargs + 1, 3))?;
            let thread_state =
                lua.ref_thread_exec(|ref_thread| ffi::lua_tothread(ref_thread, self.0.index));
            let status = ffi::lua_status(thread_state);
            if status != ffi::LUA_YIELD && ffi::lua_gettop(thread_state) == 0 {
                return Err(Error::CoroutineInactive);
            }
            check_stack(thread_state, nargs)?;
            for arg in args.drain_all() {
                lua.push_value(arg)?;
            }
            ffi::lua_xmove(lua.state, thread_state, nargs);
            let mut nresults = 0;
            let ret = ffi::lua_resume(thread_state, lua.state, nargs, &mut nresults as *mut c_int);
            if ret != ffi::LUA_OK && ret != ffi::LUA_YIELD {
                protect_lua!(lua.state, 0, 0, |_| error_traceback(thread_state))?;
                return Err(pop_error(thread_state, ret));
            }
            let mut results = args; // Reuse MultiValue container
            check_stack(lua.state, nresults + 2)?; // 2 is extra for `lua.pop_value()` below
            ffi::lua_xmove(thread_state, lua.state, nresults);
            for _ in 0..nresults {
                results.push_front(lua.pop_value());
            }
            results
        };
        R::from_lua_multi(results, lua)
    }
pub fn resume < A , R > ( & self , args : A ) -> Result < R > where A : ToLuaMulti < ' lua > , R : FromLuaMulti < ' lua > , { let lua = self . 0 . lua ; let mut args = args . to_lua_multi ( lua ) ? ; let nargs = args . len ( ) as c_int ; let results = unsafe { let _sg = StackGuard :: new ( lua . state ) ; check_stack ( lua . state , cmp :: max ( nargs + 1 , 3 ) ) ? ; let thread_state = lua . ref_thread_exec ( | ref_thread | ffi :: lua_tothread ( ref_thread , self . 0 . index ) ) ; let status = ffi :: lua_status ( thread_state ) ; if status != ffi :: LUA_YIELD && ffi :: lua_gettop ( thread_state ) == 0 { return Err ( Error :: CoroutineInactive ) ; } check_stack ( thread_state , nargs ) ? ; for arg in args . drain_all ( ) { lua . push_value ( arg ) ? ; } ffi :: lua_xmove ( lua . state , thread_state , nargs ) ; let mut nresults = 0 ; let ret = ffi :: lua_resume ( thread_state , lua . state , nargs , & mut nresults as * mut c_int ) ; if ret != ffi :: LUA_OK && ret != ffi :: LUA_YIELD { protect_lua ! ( lua state 0 0 error_traceback ( thread_state ) ) ? ; return Err ( pop_error ( thread_state , ret ) ) ; } let mut results = args ; // Reuse MultiValue container check_stack ( lua . state , nresults + 2 ) ? ; // 2 is extra for `lua.pop_value()` below ffi :: lua_xmove ( thread_state , lua . state , nresults ) ; for _ in 0 .. nresults { results . push_front ( lua . pop_value ( ) ) ; } results } ; R :: from_lua_multi ( results , lua ) }
Resumes execution of this thread.
Resumes execution of this thread .
&self
args
A
&self
args
A
examples
None
use mlua::{error, lua, result, thread};
None
fn main() -> result<()> {
None
let lua = lua::new();
let thread: Thread = lua.load(r#"
coroutine.create(function(arg)
assert(arg == 42)
local yieldarg = coroutine.yield(123)
assert(yieldarg == 43)
return 987
end)
"#).eval()?.



The coroutine has now returned, so `resume` will fail
match thread.resume::<_, u32>(()) {
Err(Error::CoroutineInactive) => {},
unexpected => panic!("unexpected result {:?}", unexpected),
}
let thread : Thread = lua . load ( r# " coroutine . create ( function ( arg ) assert ( arg == 42 ) local yieldarg = coroutine . yield ( 123 ) assert ( yieldarg == 43 ) return 987 end ) " # ) . eval () ? . The coroutine has now returned so ` resume ` will fail match thread . resume :: <_ u32 > (( )) { Err ( Error :: CoroutineInactive ) = > {} unexpected = > panic! ( " unexpected result { : ? } " unexpected ) }
ok(())
None
}
None