Function function0

Source
pub fn function0<Z, R>(f: Z) -> Function<Z, (), R>
where Z: FnMut() -> R,
Expand description

Wraps a type that implements FnMut so that it can be used by hlua.

This is needed because of a limitation in Rust’s inferrence system. Even though in practice functions and closures always have a fixed number of parameters, the FnMut trait of Rust was designed so that it allows calling the same closure with a varying number of parameters. The consequence however is that there is no way of inferring with the trait alone many parameters a function or closure expects.

Examples found in repository?
examples/sound-api.rs (line 13)
4fn main() {
5    let mut lua = hlua::Lua::new();
6    lua.openlibs();
7
8    // we create a fill an array named `Sound` which will be used as a class-like interface
9    {
10        let mut sound_namespace = lua.empty_array("Sound");
11
12        // creating the `Sound.new` function
13        sound_namespace.set("new", hlua::function0(|| Sound::new()));
14    }
15
16    lua.execute::<()>(r#"
17        s = Sound.new();
18        s:play();
19
20        print("hello world from within lua!");
21        print("is the sound playing:", s:is_playing());
22
23        s:stop();
24        print("is the sound playing:", s:is_playing());
25
26    "#)
27        .unwrap();
28}