summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2017-07-26 01:54:40 +0200
committerJonas Schievink <jonasschievink@gmail.com>2017-07-26 16:38:10 +0200
commitf657d301daa29fd24ef102d8616d23328d44e160 (patch)
tree23040d7452bd66086ab39a591d67a0cd9ce0f67c /src
parentbb662e5a124d4e052b4ea6533a214501117941ef (diff)
downloadmlua-f657d301daa29fd24ef102d8616d23328d44e160.zip
Document Lua::create_function
Diffstat (limited to 'src')
-rw-r--r--src/lua.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/lua.rs b/src/lua.rs
index e4e296f..f6380d2 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -1457,6 +1457,51 @@ impl Lua {
}
/// Wraps a Rust function or closure, creating a callable Lua function handle to it.
+ ///
+ /// # Examples
+ ///
+ /// Create a function which prints its argument:
+ ///
+ /// ```
+ /// # extern crate rlua;
+ /// # use rlua::{Lua, Result};
+ /// # fn try_main() -> Result<()> {
+ /// let lua = Lua::new();
+ ///
+ /// let greet = lua.create_function(|lua, args| {
+ /// let name: String = lua.unpack(args)?;
+ /// println!("Hello, {}!", name);
+ /// lua.pack(())
+ /// });
+ /// # let _ = greet; // used
+ /// # Ok(())
+ /// # }
+ /// # fn main() {
+ /// # try_main().unwrap();
+ /// # }
+ /// ```
+ ///
+ /// Use the `hlist_macro` crate to use multiple arguments:
+ ///
+ /// ```
+ /// #[macro_use] extern crate hlist_macro;
+ /// # extern crate rlua;
+ /// # use rlua::{Lua, Result};
+ /// # fn try_main() -> Result<()> {
+ /// let lua = Lua::new();
+ ///
+ /// let print_person = lua.create_function(|lua, args| {
+ /// let hlist_pat![name, age]: HList![String, u8] = lua.unpack(args)?;
+ /// println!("{} is {} years old!", name, age);
+ /// lua.pack(())
+ /// });
+ /// # let _ = print_person; // used
+ /// # Ok(())
+ /// # }
+ /// # fn main() {
+ /// # try_main().unwrap();
+ /// # }
+ /// ```
pub fn create_function<'lua, F>(&'lua self, func: F) -> Function<'lua>
where
F: 'lua + for<'a> FnMut(&'a Lua, MultiValue<'a>) -> Result<MultiValue<'a>>,