cel_memory_sqlite/vec_extension.rs
1//! Registration helper for the sqlite-vec extension.
2//!
3//! Centralises the `unsafe transmute` so every callsite gets the same
4//! typed signature (clippy demands an explicit type annotation on raw
5//! transmutes; doing it once here is cleaner than four-way copy-paste).
6
7use rusqlite::ffi;
8
9/// Pointer signature SQLite expects for an extension init function.
10type SqliteExtensionInit = unsafe extern "C" fn(
11 *mut ffi::sqlite3,
12 *mut *const i8,
13 *const ffi::sqlite3_api_routines,
14) -> i32;
15
16/// Register sqlite-vec as an auto-extension. Every subsequent
17/// `Connection::open*` picks it up, gaining access to the `vec0` virtual
18/// table.
19///
20/// Calling this multiple times is safe — SQLite dedupes by function
21/// pointer. The provider calls it on every `open` so callers don't have
22/// to remember.
23pub fn register() {
24 // SAFETY: sqlite-vec's `sqlite3_vec_init` has the C signature SQLite
25 // requires for an extension init function. The transmute reshapes the
26 // crate's exported symbol (an `extern "C" fn` whose Rust type carries
27 // no `unsafe`) into the type SQLite's FFI expects. SQLite stores the
28 // pointer and invokes it on connection open.
29 unsafe {
30 let init_ptr: SqliteExtensionInit = std::mem::transmute::<*const (), SqliteExtensionInit>(
31 sqlite_vec::sqlite3_vec_init as *const (),
32 );
33 ffi::sqlite3_auto_extension(Some(init_ptr));
34 }
35}