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
// 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!
//! Safe callback wrapper macros for `DuckDB` extension callbacks.
//!
//! Every `DuckDB` callback is `unsafe extern "C" fn`. If a Rust panic unwinds
//! across the FFI boundary, it is **undefined behaviour**. These macros wrap
//! user-provided closures with `std::panic::catch_unwind`, converting panics
//! into error reporting via `duckdb_scalar_function_set_error` or by setting
//! the output chunk size to 0.
//!
//! # Macros
//!
//! - `scalar_callback!` — wraps a scalar function callback
//! - `table_scan_callback!` — wraps a table function scan callback
//!
//! # Estimated impact
//!
//! Eliminates ~60 `unsafe extern "C" fn` declarations across typical extensions.
//!
//! # Example: Scalar callback
//!
//! ```rust,no_run
//! use quack_rs::data_chunk::DataChunk;
//! use quack_rs::vector::VectorWriter;
//!
//! quack_rs::scalar_callback!(my_func, |info, input, output| {
//! let chunk = unsafe { DataChunk::from_raw(input) };
//! let mut writer = unsafe { VectorWriter::from_vector(output) };
//! for row in 0..chunk.size() {
//! let val = unsafe { chunk.reader(0).read_i64(row) };
//! unsafe { writer.write_i64(row, val * 2) };
//! }
//! });
//! ```
//!
//! # Example: Table scan callback
//!
//! ```rust,no_run
//! use quack_rs::data_chunk::DataChunk;
//!
//! quack_rs::table_scan_callback!(my_scan, |info, output| {
//! let chunk = unsafe { DataChunk::from_raw(output) };
//! let mut writer = unsafe { chunk.writer(0) };
//! unsafe { writer.write_i64(0, 42) };
//! unsafe { chunk.set_size(1) };
//! });
//! ```
/// Generates a panic-safe `unsafe extern "C"` scalar function callback.
///
/// The macro emits a function with signature:
/// ```text
/// unsafe extern "C" fn $name(
/// info: duckdb_function_info,
/// input: duckdb_data_chunk,
/// output: duckdb_vector,
/// )
/// ```
///
/// The body is wrapped in `std::panic::catch_unwind`. If the closure panics,
/// the error is reported via `duckdb_scalar_function_set_error` and the
/// function returns without unwinding across the FFI boundary.
///
/// # Parameters
///
/// - `$name` — the name of the generated function
/// - `$body` — a closure with signature `|info: duckdb_function_info, input: duckdb_data_chunk, output: duckdb_vector|`
///
/// # Example
///
/// ```rust,no_run
/// quack_rs::scalar_callback!(double_it, |info, input, output| {
/// let chunk = unsafe { quack_rs::data_chunk::DataChunk::from_raw(input) };
/// let mut writer = unsafe { quack_rs::vector::VectorWriter::from_vector(output) };
/// for row in 0..chunk.size() {
/// let val = unsafe { chunk.reader(0).read_i64(row) };
/// unsafe { writer.write_i64(row, val * 2) };
/// }
/// });
/// ```
/// Generates a panic-safe `unsafe extern "C"` table function scan callback.
///
/// The macro emits a function with signature:
/// ```text
/// unsafe extern "C" fn $name(
/// info: duckdb_function_info,
/// output: duckdb_data_chunk,
/// )
/// ```
///
/// The body is wrapped in `std::panic::catch_unwind`. If the closure panics,
/// the output chunk size is set to 0 (signaling end of stream) to prevent
/// undefined behaviour from unwinding across the FFI boundary.
///
/// # Parameters
///
/// - `$name` — the name of the generated function
/// - `$body` — a closure with signature `|info: duckdb_function_info, output: duckdb_data_chunk|`
///
/// # Example
///
/// ```rust,no_run
/// quack_rs::table_scan_callback!(my_scan, |info, output| {
/// let chunk = unsafe { quack_rs::data_chunk::DataChunk::from_raw(output) };
/// let mut writer = unsafe { chunk.writer(0) };
/// unsafe { writer.write_i64(0, 42) };
/// unsafe { chunk.set_size(1) };
/// });
/// ```