Add example to `LuaThread::resume`

Adapted from #8
This commit is contained in:
Jonas Schievink 2017-06-22 00:27:26 +02:00
parent 06cd2c8e68
commit 2e87556a1b
1 changed files with 28 additions and 0 deletions

View File

@ -569,6 +569,34 @@ impl<'lua> LuaThread<'lua> {
///
/// If the thread calls `coroutine.yield`, returns the values passed to `yield`. If the thread
/// `return`s values from its main function, returns those.
///
/// # Example
///
/// ```
/// # extern crate rlua;
/// # use rlua::*;
///
/// # fn main() {
/// let lua = Lua::new();
/// let thread: LuaThread = lua.eval(r#"
/// coroutine.create(function(arg)
/// assert(arg == 42)
/// local yieldarg = coroutine.yield(123)
/// assert(yieldarg == 43)
/// return 987
/// end)
/// "#).unwrap();
///
/// assert_eq!(thread.resume::<_, u32>(42).unwrap(), 123);
/// assert_eq!(thread.resume::<_, u32>(43).unwrap(), 987);
///
/// // The coroutine has now returned, so `resume` will fail
/// match thread.resume::<_, u32>(()) {
/// Err(LuaError(LuaErrorKind::CoroutineInactive, _)) => {},
/// unexpected => panic!("unexpected result {:?}", unexpected),
/// }
/// # }
/// ```
pub fn resume<A, R>(&self, args: A) -> LuaResult<R>
where
A: ToLuaMulti<'lua>,