| Crates.io | duckdb-ext-macros |
| lib.rs | duckdb-ext-macros |
| version | 0.1.0 |
| created_at | 2025-12-01 05:21:49.742171+00 |
| updated_at | 2025-12-01 05:21:49.742171+00 |
| description | A modern, Rust 2024 Edition compatible procedural macro for creating DuckDB loadable extensions. |
| homepage | |
| repository | https://github.com/redraiment/duckdb-ext-macros |
| max_upload_size | |
| id | 1959325 |
| size | 24,107 |
A modern, Rust 2024 Edition compatible procedural macro for creating DuckDB loadable extensions.
This crate provides the #[duckdb_extension] attribute macro that simplifies the creation of DuckDB loadable extensions in Rust. It serves as a modern alternative to DuckDB's official duckdb-loadable-macros crate, which only supports Rust 2021 Edition.
duckdb-loadable-macrosdarling, syn, quote, and proc-macro2Add this to your Cargo.toml:
[dependencies]
duckdb = { version = "1.4.2", features = ["vtab-loadable"] } # or your preferred version
duckdb-ext-macros = "0.1.0"
libduckdb-sys = { version = "1.4.2", features = ["loadable-extension"] }
use duckdb_ext_macros::duckdb_extension;
#[duckdb_extension(name = "my_extension", api_version = "v1.2.0")]
fn my_extension_init(connection: duckdb::Connection) -> Result<(), String> {
// Your extension initialization logic here
// Register functions, types, or perform setup
println!("My extension loaded successfully!");
Ok(())
}
The simplest usage with default values:
#[duckdb_extension]
fn my_extension(connection: duckdb::Connection) -> Result<(), String> {
// Extension logic
Ok(())
}
This will:
{package_name}_init_c_api#[duckdb_extension(name = "custom_extension")]
fn init(connection: duckdb::Connection) -> Result<(), String> {
Ok(())
}
#[duckdb_extension(api_version = "v1.1.0")]
fn init(connection: duckdb::Connection) -> Result<(), String> {
Ok(())
}
You can also configure the extension via environment variables, which is useful for build scripts or CI/CD:
# Set extension name
export DUCKDB_EXTENSION_NAME="my_extension"
# Set minimum API version
export DUCKDB_MINIMUM_API_VERSION="v1.2.0"
Environment variables are used as fallbacks when macro attributes are not provided.
The #[duckdb_extension] macro transforms your Rust function into a DuckDB-compatible C entrypoint. Here's what happens under the hood:
name, api_version) or reads environment variables{extension_name}_init_c_apiFor this input:
#[duckdb_extension(name = "my_ext")]
fn init(conn: duckdb::Connection) -> Result<(), String> { Ok(()) }
The macro generates:
#[unsafe(no_mangle)]
pub extern "C" fn my_ext_init_c_api(
info: libduckdb_sys::duckdb_extension_info,
access: *const libduckdb_sys::duckdb_extension_access
) -> bool {
// FFI and error handling code
}
duckdb-loadable-macros| Feature | duckdb-ext-macros |
duckdb-loadable-macros |
|---|---|---|
| Rust Edition | 2024 Edition | 2021 Edition only |
| Dependencies | Modern versions | Older versions |
duckdb-loadable-macrosIf you're currently using duckdb-loadable-macros, migration is straightforward:
Update Cargo.toml:
# Remove or replace:
# duckdb-loadable-macros = "..."
# Add:
duckdb-ext-macros = "0.1.0"
Update imports:
// Change from:
// use duckdb_loadable_macros::duckdb_entrypoint;
// To:
use duckdb_ext_macros::duckdb_extension;
Update attribute:
// Change from:
// #[duckdb_entrypoint]
// To:
#[duckdb_extension]
The API is designed to be compatible, so your existing extension logic should work without changes.
name: The extension name (used in the C entrypoint function)api_version: Minimum DuckDB C API version required (default: "v1.2.0")DUCKDB_EXTENSION_NAME: Extension name (fallback if name attribute not provided)DUCKDB_MINIMUM_API_VERSION: Minimum API version (fallback if api_version attribute not provided)The extension name is determined in this order:
name attribute in the macroDUCKDB_EXTENSION_NAME environment variableCARGO_PKG_NAME environment variable (cargo package name)The minimum API version is determined in this order:
api_version attribute in the macroDUCKDB_MINIMUM_API_VERSION environment variableThe macro provides comprehensive error handling:
Errors are propagated to DuckDB with descriptive messages. If error string allocation fails, a fallback message is used.
This project is licensed under the same terms as DuckDB (MIT License).
duckdb-loadable-macros