Method implementations for callable objects (named functions, function pointers, closures)
Methods
impl callable { ... }
-
Call a function with arguments passed as a tuple.
Example
fn add(a: i32, b: i32) -> i32 { a + b } let args = (1, 2); let result = add.invoke(args); assert_eq!(result, 3);
Run this example -
fn as_callback<Args, Ret, F>(f: &mut F) -> (&mut void, fn(&mut void, Args...) -> Ret)
F: Closure + Fn(Args...) -> RetConverts a pointer to closure to a C-style state and function pointer pair.
This function is used to pass closures to C functions that expect a function pointer and a state pointer, that is passed as the first argument to the function when the callback is called.
Example
use libc::{c_int, c_char}; extern "C" fn sqlite3_exec( db: &mut sqlite3, sql: &c_char, callback: fn(&mut void, c_int, &mut &mut c_char, &mut &mut c_char) -> c_int, state: &mut void, // тое errmsg: &mut &mut c_char, ) -> libc::c_int; let db = /* ... */; let file = File::open(/* ... */); defer file.close(); // Prints the rows of a table to a file that's captured by the closure. let print = |&file, num_cols: c_int, values: &mut &mut c_char, _: &mut &mut c_char| { /// ... print values to file ... 0 }; let (state, callback) = print.as_callback(); sqlite3_exec( &mut db, "SELECT * FROM users\0".as_ptr() as &c_char, callback, state, null );
Run this example