Callback
class Callback Import
Source: use ffi::types::{Callback}; compiler/src/builtin_docs/ffi/types.hy FFI tag for a C function pointer, letting C call back into a Coil function.
As an argument, pass a named Coil function; C receives a pointer it can
call. The callback signature is currently fixed to int64_t (*)(int64_t),
so the Coil function must take one int and return int. As a return
type, the function pointer comes back as an opaque int address, like
Ptr.
Examples#
use ffi::{dload, declare, invoke};
use ffi::types::{Callback, Int};
fn doubler(int x) -> int {
return x * 2;
}
// int64_t apply_cb(int64_t (*f)(int64_t), int64_t x) { return f(x); }
let id = declare(lib, "apply_cb", (Callback, Int), Int)?;
let n = invoke(lib, id, (doubler, 21))?; // 42
See also#
intffi::types::PtrInt: FFI tag for a 64-bit signed integer (int64_t), passed from and returned asint.Float: FFI tag for a 64-bit float (double), passed from and returned asfloat.String: FFI tag for a NUL-terminated C string (const char *), passed from and returned asstring.Void: FFI tag for no value (void), valid only as a return type.Bool: FFI tag for a Cbool(one byte,0or1), passed from and returned asbool.Int8: FFI tag for an 8-bit signed integer (int8_t), passed from and returned asint.