diff options
author | Jonas Schievink <jonasschievink@gmail.com> | 2017-07-26 01:54:40 +0200 |
---|---|---|
committer | Jonas Schievink <jonasschievink@gmail.com> | 2017-07-26 16:38:10 +0200 |
commit | f657d301daa29fd24ef102d8616d23328d44e160 (patch) | |
tree | 23040d7452bd66086ab39a591d67a0cd9ce0f67c /src | |
parent | bb662e5a124d4e052b4ea6533a214501117941ef (diff) | |
download | mlua-f657d301daa29fd24ef102d8616d23328d44e160.zip |
Document Lua::create_function
Diffstat (limited to 'src')
-rw-r--r-- | src/lua.rs | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -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>>, |