From 2e87556a1b6182f42bf3cb2463d91dcd3e4ebb4a Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 22 Jun 2017 00:27:26 +0200 Subject: [PATCH 1/3] Add example to `LuaThread::resume` Adapted from #8 --- src/lua.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lua.rs b/src/lua.rs index 3feb350..de04474 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -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(&self, args: A) -> LuaResult where A: ToLuaMulti<'lua>, From f75385305c9b7f9230f9f5b21ce93fb6bf295d38 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 22 Jun 2017 00:38:08 +0200 Subject: [PATCH 2/3] Add an example to `LuaFunction::bind` --- src/lua.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lua.rs b/src/lua.rs index de04474..4a7bec3 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -494,6 +494,26 @@ impl<'lua> LuaFunction<'lua> { /// return function() f(...) end /// end /// ``` + /// + /// # Example + /// + /// ``` + /// # extern crate rlua; + /// # use rlua::*; + /// + /// # fn main() { + /// let lua = Lua::new(); + /// let globals = lua.globals().unwrap(); + /// + /// // Bind the argument `123` to Lua's `tostring` function + /// let tostring: LuaFunction = globals.get("tostring").unwrap(); + /// let tostring_123: LuaFunction = tostring.bind(123i32).unwrap(); + /// + /// // Now we can call `tostring_123` without arguments to get the result of `tostring(123)` + /// let result: String = tostring_123.call(()).unwrap(); + /// assert_eq!(result, "123"); + /// # } + /// ``` pub fn bind>(&self, args: A) -> LuaResult> { unsafe extern "C" fn bind_call_impl(state: *mut ffi::lua_State) -> c_int { let nargs = ffi::lua_gettop(state); From 979e1d16756449b6a04e623b4f55561a363aaf2f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 22 Jun 2017 10:49:18 +0200 Subject: [PATCH 3/3] Add example to `LuaString::to_str` --- src/lua.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/lua.rs b/src/lua.rs index 4a7bec3..d949cbc 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -168,6 +168,23 @@ pub struct LuaString<'lua>(LuaRef<'lua>); impl<'lua> LuaString<'lua> { /// Get a `&str` slice if the Lua string is valid UTF-8. + /// + /// # Example + /// + /// ``` + /// # extern crate rlua; + /// # use rlua::*; + /// # fn main() { + /// let lua = Lua::new(); + /// let globals = lua.globals().unwrap(); + /// + /// let version: LuaString = globals.get("_VERSION").unwrap(); + /// assert!(version.to_str().unwrap().contains("Lua")); + /// + /// let non_utf8: LuaString = lua.eval(r#" "test\xff" "#).unwrap(); + /// assert!(non_utf8.to_str().is_err()); + /// # } + /// ``` pub fn to_str(&self) -> LuaResult<&str> { let lua = self.0.lua; unsafe {