Skip to main content

init_extension

Function init_extension 

Source
pub unsafe fn init_extension<F>(
    info: duckdb_extension_info,
    access: *const duckdb_extension_access,
    api_version: &str,
    register: F,
) -> bool
where F: FnOnce(duckdb_connection) -> Result<(), ExtensionError>,
Expand description

Core entry point helper — sets up a connection and calls your registration closure.

This function encapsulates the correct initialization sequence for a DuckDB loadable extension written in Rust:

  1. Calls duckdb_rs_extension_api_init with the given api_version.
  2. Extracts the duckdb_database via access.get_database.
  3. Opens a duckdb_connection via duckdb_connect.
  4. Calls register(connection).
  5. Disconnects with duckdb_disconnect.
  6. On any error, reports via access.set_error and returns false.

The closure runs in one transaction, so a failed LOAD leaves no catalog entry behind; casts, replacement scans and configuration options are not rolled back. See the module documentation.

§Return value

Returns true if initialization succeeded, false on any error.

§Safety

  • info must be the duckdb_extension_info passed by DuckDB to your entry point.
  • access must be the *const duckdb_extension_access passed by DuckDB.
  • Both pointers must remain valid for the duration of this call.

§Pitfall L3: No panic across FFI

This function never panics. All errors are reported via access.set_error (when access itself is null, there is nowhere to report one, and the function returns false).

§Example

use quack_rs::entry_point::init_extension;

#[unsafe(no_mangle)]
pub unsafe extern "C" fn my_ext_init_c_api(
    info: libduckdb_sys::duckdb_extension_info,
    access: *const libduckdb_sys::duckdb_extension_access,
) -> bool {
    unsafe {
        init_extension(info, access, quack_rs::DUCKDB_API_VERSION, |_con| {
            // Register your functions here
            Ok(())
        })
    }
}