Class in ffi::types compiler

Callback

class Callback
Import
use ffi::types::{Callback};
Source: 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#

  • int
  • ffi::types::Ptr
  • Int: FFI tag for a 64-bit signed integer (int64_t), passed from and returned as int.
  • Float: FFI tag for a 64-bit float (double), passed from and returned as float.
  • String: FFI tag for a NUL-terminated C string (const char *), passed from and returned as string.
  • Void: FFI tag for no value (void), valid only as a return type.
  • Bool: FFI tag for a C bool (one byte, 0 or 1), passed from and returned as bool.
  • Int8: FFI tag for an 8-bit signed integer (int8_t), passed from and returned as int.
↑ ↓ navigate ↵ open