Attribute Macro pg_extern_attr::pg_extern

source ·
#[pg_extern]
Expand description

An attribute macro for wrapping Rust functions with boiler plate for defining and calling conventions between Postgres and Rust.

This mimics the C macro for defining functions

#define PG_FUNCTION_INFO_V1(funcname) \
extern Datum funcname(PG_FUNCTION_ARGS); \
extern PGDLLEXPORT const Pg_finfo_record * CppConcat(pg_finfo_,funcname)(void); \
const Pg_finfo_record * \
CppConcat(pg_finfo_,funcname) (void) \
{ \
    static const Pg_finfo_record my_finfo = { 1 }; \
    return &my_finfo; \
} \

Returns

The result of this macro will be to produce a new function wrapping the one annotated but prepended with pg_ to distinquish them and also declares a function for Postgres to get the Function information;

For example: if the signature fn add_one(value: i32) -> i32 is annotated, two functions will be produced, the wrapper function with a signature of:

 #[no_mangle]
 pub extern "C" fn pg_add_one(func_call_info: pg_sys::FunctionCallInfo) -> pg_sys::Datum

and the info function with a signature of:

#[no_mangle]
pub extern "C" fn pg_finfo_pg_add_one() -> &'static Pg_finfo_record