quack-rs 0.12.0

Production-grade Rust SDK for building DuckDB loadable extensions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// SPDX-License-Identifier: MIT
// Copyright 2026 Tom F. <https://github.com/tomtom215/>
// My way of giving something small back to the open source community
// and encouraging more Rust development!

//! Generic `FfiState<T>` wrapper for safe aggregate state management.
//!
//! # Problem solved
//!
//! Writing the state init/destroy lifecycle using raw pointers is error-prone.
//! The canonical pattern is:
//!
//! ```rust,no_run
//! use libduckdb_sys::{duckdb_function_info, duckdb_aggregate_state, idx_t};
//!
//! #[derive(Default)]
//! struct MyState { count: u64 }
//!
//! #[repr(C)]
//! struct FfiState { inner: *mut MyState }
//!
//! unsafe extern "C" fn state_init(_: duckdb_function_info, state: duckdb_aggregate_state) {
//!     let ffi = &mut *(state as *mut FfiState);
//!     ffi.inner = Box::into_raw(Box::new(MyState::default()));
//! }
//!
//! unsafe extern "C" fn state_destroy(states: *mut duckdb_aggregate_state, count: idx_t) {
//!     for i in 0..count as usize {
//!         let state_ptr = *states.add(i);
//!         let ffi = &mut *(state_ptr as *mut FfiState);
//!         if !ffi.inner.is_null() {
//!             drop(Box::from_raw(ffi.inner));
//!             ffi.inner = std::ptr::null_mut();
//!         }
//!     }
//! }
//! ```
//!
//! [`FfiState<T>`] encapsulates this pattern. Your type `T` only needs to
//! implement [`AggregateState`] (which requires `Default`), and you call the
//! provided helper methods instead of writing raw pointer code.
//!
//! # Pitfalls prevented
//!
//! - **L1**: Combine propagates all fields because your type `T`'s `combine`
//!   method is responsible — the `FfiState` wrapper ensures `T`'s method is called.
//! - **L2**: No double-free — `destroy_callback` sets `inner` to null after freeing.
//! - **L3**: No panic across FFI — `with_state_mut` returns an `Option`, not a panic.

use libduckdb_sys::{duckdb_aggregate_state, duckdb_function_info, idx_t};

/// Trait for types that can be used as `DuckDB` aggregate state.
///
/// Implement this for your state struct. The only requirement is `Default`
/// (used to create the initial state in `state_init`) and `Send` (since `DuckDB`
/// may call `combine` across threads).
///
/// # Example
///
/// ```rust
/// use quack_rs::aggregate::AggregateState;
///
/// #[derive(Default)]
/// struct WordCount {
///     count: u64,
/// }
///
/// impl AggregateState for WordCount {}
///
/// // FfiState::<WordCount>::size_callback and other methods are now available.
/// ```
pub trait AggregateState: Default + Send + 'static {}

/// A generic FFI-compatible state wrapper for use with `DuckDB` aggregate functions.
///
/// `FfiState<T>` is a `#[repr(C)]` struct containing a single raw pointer to a
/// heap-allocated `T`. `DuckDB` allocates `size_of::<FfiState<T>>()` bytes per
/// aggregate group via [`size_callback`][FfiState::size_callback], then calls
/// [`init_callback`][FfiState::init_callback] to initialize each allocation.
///
/// # Memory layout
///
/// ```text
/// FfiState<T> = { inner: *mut T }  // exactly pointer-sized, repr(C)
/// ```
///
/// # Usage
///
/// ```rust
/// use quack_rs::aggregate::{AggregateState, FfiState};
/// use libduckdb_sys::{duckdb_function_info, duckdb_aggregate_state, idx_t};
///
/// #[derive(Default)]
/// struct MyState { sum: i64 }
/// impl AggregateState for MyState {}
///
/// // In your registration code:
/// // .state_size(FfiState::<MyState>::size_callback)
/// // .init(FfiState::<MyState>::init_callback)
/// // .destructor(FfiState::<MyState>::destroy_callback)
/// ```
#[repr(C)]
pub struct FfiState<T: AggregateState> {
    /// Raw pointer to the heap-allocated `T` value.
    ///
    /// - Set to non-null by [`init_callback`][FfiState::init_callback].
    /// - Set to null after freeing by [`destroy_callback`][FfiState::destroy_callback].
    pub inner: *mut T,
}

impl<T: AggregateState> FfiState<T> {
    /// Returns the size of `FfiState<T>` in bytes, for use as the `state_size` callback.
    ///
    /// # Example
    ///
    /// ```rust
    /// use quack_rs::aggregate::{AggregateState, FfiState};
    ///
    /// #[derive(Default)]
    /// struct MyState { val: i64 }
    /// impl AggregateState for MyState {}
    ///
    /// let size = FfiState::<MyState>::size();
    /// assert_eq!(size, std::mem::size_of::<FfiState<MyState>>());
    /// ```
    #[inline]
    #[must_use]
    pub const fn size() -> usize {
        core::mem::size_of::<Self>()
    }

    /// The `state_size` callback function for use in the builder.
    ///
    /// Returns the number of bytes `DuckDB` must allocate per aggregate group.
    ///
    /// # Safety
    ///
    /// This is an `unsafe extern "C"` function pointer. It is safe to pass to
    /// [`AggregateFunctionBuilder::state_size`][crate::aggregate::AggregateFunctionBuilder::state_size].
    pub const unsafe extern "C" fn size_callback(_info: duckdb_function_info) -> idx_t {
        core::mem::size_of::<Self>() as idx_t
    }

    /// The `state_init` callback function for use in the builder.
    ///
    /// Allocates a `T::default()` on the heap and stores the raw pointer in
    /// the `FfiState` at `state`.
    ///
    /// # Safety
    ///
    /// - `state` must point to `size_of::<FfiState<T>>()` bytes of writable memory
    ///   allocated by `DuckDB`.
    /// - This function must only be called once per state allocation.
    pub unsafe extern "C" fn init_callback(
        _info: duckdb_function_info,
        state: duckdb_aggregate_state,
    ) {
        // SAFETY: DuckDB allocated `size_of::<FfiState<T>>()` bytes at `state`.
        // We cast it to `*mut FfiState<T>` and write the inner pointer.
        let ffi = unsafe { &mut *(state.cast::<Self>()) };
        ffi.inner = Box::into_raw(Box::<T>::default());
    }

    /// The `state_destroy` callback function for use in the builder.
    ///
    /// Frees the heap-allocated `T` for each state in `states[0..count]`.
    /// Sets `inner` to null after freeing to prevent double-free.
    ///
    /// # Pitfall L2: No double-free
    ///
    /// After `Box::from_raw`, we set `inner = null` so that if `destroy_callback`
    /// is accidentally called twice, the second call is a no-op.
    ///
    /// # Count conversion
    ///
    /// If `count` (an `idx_t`) cannot be converted to `usize`, the loop iterates
    /// zero times — no states are freed. This is a defensive choice to avoid
    /// panicking across FFI. In practice, `idx_t` is `u64` and `usize` is at
    /// least 64 bits on all `DuckDB`-supported platforms, so this path is
    /// unreachable on supported targets.
    ///
    /// # Safety
    ///
    /// - `states` must point to an array of `count` valid `duckdb_aggregate_state`
    ///   pointers, each previously initialized by [`init_callback`][Self::init_callback].
    /// - Each state must not have been freed already (or have `inner == null`).
    pub unsafe extern "C" fn destroy_callback(states: *mut duckdb_aggregate_state, count: idx_t) {
        for i in 0..usize::try_from(count).unwrap_or(0) {
            // SAFETY: `states` is a valid array of `count` pointers.
            let state_ptr = unsafe { *states.add(i) };
            // SAFETY: Each element was initialized by `init_callback` as `*mut Self`.
            let ffi = unsafe { &mut *(state_ptr.cast::<Self>()) };
            if !ffi.inner.is_null() {
                // SAFETY: `inner` was created by `Box::into_raw(Box::new(T::default()))`.
                // We are the only owner; dropping it here is correct.
                unsafe { drop(Box::from_raw(ffi.inner)) };
                // Null out the pointer to prevent double-free if called again.
                ffi.inner = core::ptr::null_mut();
            }
        }
    }

    /// Provides safe mutable access to the inner `T` value.
    ///
    /// Returns `None` if `inner` is null (which should not happen after a
    /// successful `init_callback`, but is checked defensively).
    ///
    /// # Pitfall L3: No panic across FFI
    ///
    /// This method returns `Option<&mut T>` rather than unwrapping, so callers
    /// can use `if let Some(state) = ...` patterns without panicking.
    ///
    /// # Safety
    ///
    /// - `state` must point to a valid `FfiState<T>` allocated by `DuckDB` and
    ///   initialized by [`init_callback`][Self::init_callback].
    /// - No other reference to the same `T` must exist simultaneously.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use quack_rs::aggregate::{AggregateState, FfiState};
    ///
    /// #[derive(Default)]
    /// struct Counter { n: u64 }
    /// impl AggregateState for Counter {}
    ///
    /// // Inside your update callback:
    /// // let ffi_state: duckdb_aggregate_state = ...;
    /// // if let Some(state) = unsafe { FfiState::<Counter>::with_state_mut(ffi_state) } {
    /// //     state.n += 1;
    /// // }
    /// ```
    pub unsafe fn with_state_mut<'a>(state: duckdb_aggregate_state) -> Option<&'a mut T> {
        // SAFETY: Caller guarantees `state` points to a valid `FfiState<T>`.
        let ffi = unsafe { &mut *state.cast::<Self>() };
        if ffi.inner.is_null() {
            return None;
        }
        // SAFETY: `inner` is non-null and was allocated by Box::into_raw.
        // Caller guarantees no other references exist.
        Some(unsafe { &mut *ffi.inner })
    }

    /// Provides safe immutable access to the inner `T` value.
    ///
    /// See [`with_state_mut`][Self::with_state_mut] for safety requirements.
    ///
    /// # Safety
    ///
    /// Same as `with_state_mut`, but only borrows immutably.
    pub unsafe fn with_state<'a>(state: duckdb_aggregate_state) -> Option<&'a T> {
        // SAFETY: Same invariants as with_state_mut.
        let ffi = unsafe { &*state.cast::<Self>() };
        if ffi.inner.is_null() {
            return None;
        }
        // SAFETY: inner is non-null.
        Some(unsafe { &*ffi.inner })
    }
}

// Note: The pointer-size invariant is verified in unit tests using a concrete
// type that implements AggregateState (see tests::ffi_state_is_pointer_sized).
// A const assertion is not possible here because const fn cannot use trait bounds.

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Default, Debug, PartialEq)]
    struct Counter {
        value: u64,
    }
    impl AggregateState for Counter {}

    #[test]
    fn ffi_state_is_pointer_sized() {
        assert_eq!(
            core::mem::size_of::<FfiState<Counter>>(),
            core::mem::size_of::<*mut Counter>()
        );
    }

    #[test]
    fn size_returns_pointer_size() {
        assert_eq!(FfiState::<Counter>::size(), core::mem::size_of::<usize>());
    }

    #[test]
    fn init_and_destroy_lifecycle() {
        // Simulate what DuckDB does:
        // 1. Allocate state_size() bytes
        // 2. Call init_callback
        // 3. Use with_state_mut
        // 4. Call destroy_callback

        // Step 1: allocate
        let mut raw: FfiState<Counter> = FfiState {
            inner: core::ptr::null_mut(),
        };
        let state_ptr = std::ptr::addr_of_mut!(raw) as duckdb_aggregate_state;

        // Step 2: init
        unsafe { FfiState::<Counter>::init_callback(core::ptr::null_mut(), state_ptr) };
        assert!(!raw.inner.is_null());

        // Step 3: access
        // SAFETY: state_ptr is valid and inner is initialized.
        let s = unsafe { FfiState::<Counter>::with_state_mut(state_ptr) };
        assert!(s.is_some());
        if let Some(counter) = s {
            counter.value = 42;
        }

        // Verify the value was set
        let s2 = unsafe { FfiState::<Counter>::with_state(state_ptr) };
        assert_eq!(s2.map(|c| c.value), Some(42));

        // Step 4: destroy
        let mut state_arr: [duckdb_aggregate_state; 1] = [state_ptr];
        unsafe {
            FfiState::<Counter>::destroy_callback(state_arr.as_mut_ptr(), 1);
        }
        // After destroy, inner must be null (double-free prevention).
        assert!(raw.inner.is_null());
    }

    #[test]
    fn destroy_null_inner_is_noop() {
        let mut raw: FfiState<Counter> = FfiState {
            inner: core::ptr::null_mut(),
        };
        let state_ptr = std::ptr::addr_of_mut!(raw) as duckdb_aggregate_state;
        let mut state_arr: [duckdb_aggregate_state; 1] = [state_ptr];
        // Calling destroy on an uninitialized (null inner) state must not crash.
        unsafe {
            FfiState::<Counter>::destroy_callback(state_arr.as_mut_ptr(), 1);
        }
        assert!(raw.inner.is_null());
    }

    #[test]
    fn with_state_mut_null_inner_returns_none() {
        let mut raw: FfiState<Counter> = FfiState {
            inner: core::ptr::null_mut(),
        };
        let state_ptr = std::ptr::addr_of_mut!(raw) as duckdb_aggregate_state;
        // SAFETY: state_ptr is valid, inner is null.
        let result = unsafe { FfiState::<Counter>::with_state_mut(state_ptr) };
        assert!(result.is_none());
    }

    #[test]
    fn with_state_null_inner_returns_none() {
        let raw: FfiState<Counter> = FfiState {
            inner: core::ptr::null_mut(),
        };
        let state_ptr = std::ptr::addr_of!(raw) as duckdb_aggregate_state;
        // SAFETY: state_ptr is valid, inner is null.
        let result = unsafe { FfiState::<Counter>::with_state(state_ptr) };
        assert!(result.is_none());
    }

    #[test]
    fn size_callback_returns_pointer_size() {
        // SAFETY: size_callback takes a null-ok info pointer and only reads sizeof.
        let size = unsafe { FfiState::<Counter>::size_callback(core::ptr::null_mut()) };
        assert_eq!(
            usize::try_from(size).unwrap(),
            core::mem::size_of::<usize>()
        );
    }

    #[test]
    fn multiple_state_destroy() {
        // Test destroy_callback with multiple states
        let mut states: Vec<FfiState<Counter>> = (0..4)
            .map(|_| FfiState {
                inner: core::ptr::null_mut(),
            })
            .collect();

        let mut ptrs: Vec<duckdb_aggregate_state> = states
            .iter_mut()
            .map(|s| std::ptr::from_mut::<FfiState<Counter>>(s) as duckdb_aggregate_state)
            .collect();

        // Initialize all
        for &ptr in &ptrs {
            unsafe { FfiState::<Counter>::init_callback(core::ptr::null_mut(), ptr) };
        }
        for s in &states {
            assert!(!s.inner.is_null());
        }

        // Destroy all
        unsafe {
            FfiState::<Counter>::destroy_callback(ptrs.as_mut_ptr(), 4);
        }

        // All should be null
        for s in &states {
            assert!(s.inner.is_null());
        }
    }
}